åãµãã¯ãåâ代æ°ââ¦ããããï¼åçè«å ¥éï¼ã®ååï¼ãã«ããã¾ããããã¯æ¬ç·¨ï¼å¾åï¼ã1åèªã¿åãï¼é·ããã©ï¼ã§ãæ¯è¼çæ°ãã*1åçè«ãç´¹ä»ãã¾ãããå ¥éï¼éã«å ¥ãï¼ãã¨ããããã¯éã®å¤ããä¸ãè¦ãã¦ã¿ãç¨åº¦ã
説æç¨ã³ã¼ãã¯Javaã®æ§æã使ãã¾ãããã ããããã±ã¼ã¸å®£è¨ã¯æ¸ããªãããpublic
ã¯ãªãã¹ãçç¥ã
å 容ï¼
- ã¤ã³ã¿ã¼ãã§ã¼ã¹ãªãã¦ãæè©®ãããªãã®
- å¿ççå¹æã¨ãã人é-人éã³ãã¥ãã±ã¼ã·ã§ã³ã¨ãã¯ãå¥åé¡
- ããããããã¤ã³ã¿ã¼ãã§ã¼ã¹ã«å¶ç´ãä»å ãã
- ãã£ã¨å¶ç´ã足ãã¦ã¿ã
- è¬ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«æå³ããããã®
- ã§ããããåçè«ã«ã©ãã¤ãªããã®ï¼
ã¤ã³ã¿ã¼ãã§ã¼ã¹ãªãã¦ãæè©®ãããªãã®
ã¾ãã次ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãè¦ã¦ãã ããã
interface AB { int a(); void b(); }
ããã¹ã´ã¤ã§ãããä½ãã¹ã´ã¤ã£ã¦ããããè¦ã¦ããªãã®ãã¨ãããµãããªããããªãã¨ãããã¹ã´ã¤ã
ã¤ã³ã¿ã¼ãã§ã¼ã¹ã£ã¦ã®ã¯ãæ¬æ¥çã»åççã«ãè¦ã¦ãä½ãããããªãããã®ã§ãããã£ã¦ãã©ãå®è£ ããããã¾ã£ããåæã§ããä¾ãã°ï¼
class MyABImpl implements AB { public int a() { return 0; } public void b() { } }
ããã¯å½ç¶ã«ãªãããªå®è£ ã§ãããªããæå¥ããï¼ å°ãªãã¨ãã³ã³ãã¤ã©ã¯æå¥è¨ãã¾ããã
大äºãªãã¨ã ããç¹°ãè¿ããã©ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã¯å®è£ ãä½ãè¦å®ããªãã®ã§ãããã¡ãããã¡ã½ããï¼é¢æ°ï¼ã®ååãå¼æ°ã®åãæ»ãå¤ã®åãã¨ãã«ä¾å¤ã¨ãã®æ§æçãªå¶éã¯èª²ããã©ããã以ä¸ã®ãã®ã§ã¯ããã¾ããã
å¿ççå¹æã¨ãã人é-人éã³ãã¥ãã±ã¼ã·ã§ã³ã¨ãã¯ãå¥åé¡
ä»åº¦ã¯æ¬¡ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ï¼
import java.io.*; interface TotalCalc { int calcTotal(int[] items); void writeResult(int result, String label, Writer out); }
ããã¤ã¯ãå®è£ è ã«ç¸å½ã®ãã¬ãã·ã£ã¼ãããã¾ããæ®éã®äººã¯ãã¡ã½ããcalcTotalã«ããã¦ã渡ãããæ´æ°é åã®å ¨é ç®ã足ãåããã¦æ»ãã¹ãã ããã¨æ³åããããããããªãã¨ãããªãæ°åã«ããªãã§ããããwriteResultãåæ§ãªå¿ççãªå¹æãããã¾ãã
ããåæ°ãã人ãªã次ã®å®è£ ã«èºèºã¯ããªãã¯ãã§ããï¼â»[追è¨]ã³ã¼ãå ã«æ¶ãå¿ããã´ãããã£ãã®åé¤[/追è¨]ï¼
import java.io.*; class MyTotalCalcImpl implements TotalCalc { public int calcTotal(int[] items) { return 0; } public void writeResult(int result, String label, Writer out) { } }
ãã£ã¨ããããããåæ°ã¯è®åã¨ãå¼ã°ãã社ä¼çï¼ãããã¯ä¼ç¤¾çï¼å¶è£ãåãããã¨ãããã¾ããç¹ã«ãã¤ã³ã¿ã¼ãã§ã¼ã¹ã¨ã¯å¥ã«ææ¸ããã£ãã¨ãã¯ãããã¾ãã¯ããã®ææ¸ãèªãã§ãªãã®ããã¨æããã¾ããã俺ã¯ããã°ã©ãã ããã³ã³ãã¤ã©ã«èªããªããããªææ¸ã¯ä¿ºãèªããï¼ãã¨éã®ã¬ããã®ãã²ã¨ã¤ã®å¯¾å¦ã§ããããããè¯ãæ¹æ³ãã©ããã¯ä¿è¨¼ãã¾ããã
ãããã«ãã¦ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ å 容ãè¦å®ããããã«æããã®ã¯ãååããã®é£æ³ã«ããå¿ççå¹æã¨ããã¤ã³ã¿ã¼ãã§ã¼ã¹ãã®ãã®ã¨ã¯å¥ã«ææ¸ããããå£é ã§è¨ã渡ããããªãã¦äºæ ã§ãã£ã¦ããããã¯ãã¹ã¦ãè¨ç®ç¾è±¡ãããã°ã©ãã³ã°è¨èªæ©æ§ã¨ã¯ä½ã®é¢ä¿ãããã¾ããã
ããããããã¤ã³ã¿ã¼ãã§ã¼ã¹ã«å¶ç´ãä»å ãã
ãå¦çç³»ï¼ã³ã³ãã¤ã©ããåºãæå³ï¼ã«èªããªããããªææ¸ã¯ä¿ºãèªããï¼ãã貫ãéãç²¾ç¥ããå½¢å¼ææ³ï¼formal methodï¼ã¨è¨ãã¾ããåç¯ã§è§¦ããããã«ãããã¯ç¤¾ä¼çï¼ä¼ç¤¾çï¼å¶è£ãåãããã®ã§ãããããå½ç¶ã«ãé·ããã¨å¼¾å§ããå¿é¿ããã¦ããã®ã§ãï¼ååã¦ã½ï¼ã
å½¢å¼ææ³ã«ã®ã£ã¨ããæåã«æããâããããããã¤ã³ã¿ã¼ãã§ã¼ã¹ABâï¼interface ABï¼ã«ä»æ§ï¼å½¢å¼ä»æ§ï¼ãæ¸ãã¾ãããããã®ä»æ§ã¯ãæ©æ¢°å¯èªï¼å¦çç³»ãä½ããï¼ã§ããã人éã«ãåãããã®ã§ãªãã¦ã¯ãªãã¾ãããããã§ã¯ã次ã®2種é¡ã®å¶ç´å¼ã使ã£ã¦ä»æ§ãæ¸ããã¨ã«ãã¾ãã
- ä¸å¤å¶ç´ï¼ä¸å¤æ¡ä»¶ï¼ï¼ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã常ã«æºããã¹ãå¶ç´ã1ã¤ã®è«çå¼ã¨ãã¦æ¸ãã
- ãã¼ã¢å¶ç´ ï¼ âäºåæ¡ä»¶ãå®è¡æãäºå¾æ¡ä»¶âã®3ã¤çµãããäºåæ¡ä»¶ãæºãããã¦ããã°ãå®è¡å¾ã«äºå¾æ¡ä»¶ãæºãããããã¨ãè¦è«ãããäºåæ¡ä»¶ãäºå¾æ¡ä»¶ã¯è«çå¼ãå®è¡æã¯ã¡ã½ããå¼ã³åºãã®åã
ãã¼ã¢å¶ç´ã¯ãï¼ãªãªã¸ãã«ã®è¨æ³ã¨ã¯æ³¢æ¬å¼§ã®ä½¿ãæ¹ãéã ãã©ï¼ã(äºåæ¡ä»¶){å®è¡æ}(äºå¾æ¡ä»¶)ãã¨ããå½¢ã«ãã¾ãã
ã¾ãã¯ä¸å¤å¶ç´ãä¸çºï¼
- å¶ç´1
(-1 <= a() && a() <= 255)
ãã®å¶ç´ï¼æ¡ä»¶ï¼ã«ãããa()ã®å¤ã-10ã«ãªã£ãã300ã«ãªããããªå®è£
ã¯å³å»æé¤ã§ãã¾ããï¼&&
ã使ããã«ã2ã¤ã®å¶ç´ã«åãã¦ããã¾ãã¾ãããããã®ã»ããæã¾ãããã¨ãããã¾ããï¼
ããã«ãã¡ã½ããa()ã«å¯¾ãã¦æ¬¡ã®ãã¼ã¢å¶ç´ã課ãã¾ãã
- å¶ç´2
(a() == x) {} (a() == x)
å®è¡æã¯ç©ºæãªã®ã§ãããã¯ã(a() == x)
ã®ç¶æ
ããä½ãããªããªãããã£ã±ã(a() == x)
ã§ãããã¨ãæå³ãã¾ããããã§åºã¦ããå¤æ°xã¯ãå¶ç´ãæ¸ãããã«å°å
¥ããå¤æ°ã§ãè«çå¦ã§ã¯å
¨ç§°æç¸å¤æ°ã¨ãé£ããå¼ã³åãããã¾ãããæ°ã«ããªãã¦ããã§ããé常ã®ããã°ã©ã ã³ã¼ãã§ãã®å¶ç´ãæ¸ããªãï¼
boolean constraint2() { int x; x = a(); return (a() == x); }
ãã£ã¨å¶ç´ã足ãã¦ã¿ã
åç¯ã¾ã§ã®å¶ç´ã§æ¬¡ã®ãã¨ãä¿è¨¼ã§ãã¾ãã
- a()ã®å¤ã¯ã-1以ä¸255以ä¸ã§ããã
- a()ãç¶ãã¦2åå¼ã³åºãã¨åãå¤ãè¿ãããããã£ã¦ãä½åº¦ç¶ãã¦å¼ã³åºãã¦ãåãå¤ãè¿ãã
ããã§æ³¨æãã¹ãã¯ã(a() == x) {b();} (a() == x)
ã¨ããå¶ç´ã¯ãªããã¨ã§ãããã£ã¦ãb()ã®å¼ã³åºãã«ãã£ã¦a()ã®å¤ãå¤åããå¯è½æ§ã¯ããã¾ãã
a()ã®å¤ã¨b()ã®å®è¡ã®ããã ã®é¢ä¿ã¨ãã¦ã¯ã次ã®å¶ç´ãå ¥ãã¦ããã¾ãããã
- å¶ç´3:
(a() == -1) {b();} (a() == -1)
ã¤ã¾ããä¸åº¦a()ã®å¤ã-1ã«ãªã£ã¦ãã¾ãã¨ãb()ãå¼ã³åºãã¦ã-1ããè¿ã£ã¦ããªããªãã®ã§ãã念ã®ãããã¾ã¨ãã¦ããã¨ï¼
- a()ã-1以å¤ã®ã¨ãã¯ãb()å¼ã³åºãå¾ã®a()ã®å¤ã¯äºåã«äºæ¸¬ã§ããªãã
- a()ã-1ã®ã¨ãã¯ãb()å¼ã³åºãå¾ã®a()ã®å¤ã¯-1ã§ããã
è¬ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã«æå³ããããã®
ãã¦ãå¶ç´å¼ãæ¸ãã¦ãããã¡ã«ãâããããããã¤ã³ã¿ã¼ãã§ã¼ã¹ABâãã ãã¶âããããã£ã¦âãã¾ãããããã«ãå¿ççã«ããããã£ãæ°åã«ããããã«ãaãpeekã«bãnextã«ãªãã¼ã ãã¾ããããããã§å¶ç´ãããä¸åº¦åæãå¶ç´å¼ã®å½¢ããããããããä¸å¤å¶ç´ããã¼ã¢å¶ç´ã®å½¢ã§æ¸ãã¾ãã
/* å¶ç´1 */ (true) {} (-1 <= peek() && peek() <= 255); /* å¶ç´2 */ Any(int x) // ãæ´æ°xã®å¤ãä½ã§ãããã¨ããæå³ (peek() == x) {} (peek() == x); /* å¶ç´3 */ (peek() == -1) {next();} (peek() == -1);
ããã«æ°åãé«ãã(?)ããã«ãreadã¨ãã便å©ã¡ã½ãããæ¸ãã¦ããã¾ãããã
int read() { int x = peek(); next(); return x; }
ãã種æãããã¾ãããåã®âã¤ããâã¨ãã¦ã¯ãâè¬ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ABâã¯å ¥åå¯è½ãªãã¤ãã¹ãªã¼ã ã§ããpeek()ï¼ãã¨ã®ååã¯a()ï¼ã-1ãè¿ãã¨End Of Streamã§ãã-1以å¤ã®å¤ã¯ããã¤ãã®ç¬¦å·ãªãæ´æ°è¡¨ç¾ã ã¨è§£éã§ãã¾ãã
ããã§æ³¨ç®ãã¦æ¬²ããã®ã¯ãåï¼ä»æ§ãæ¸ã人éï¼ãããªãï¼å®è£ è ã¨ãã¾ãããï¼ã«âã¤ããâãä¼ããããã«ãã¯ã©ã¹åï¼ã¡ã½ããåã§ãã¬ãã·ã£ã¼ããããããèªç¶è¨èªã®ææ¸ãå£é æ示ãä¸å使ã£ã¦ãªããã¨ã§ãã使ã£ã¦ããã®ã¯ãè«çå¼ã¨å®è¡æã§ããããããã¯ããã°ã©ãã³ã°è¨èªã®æ§æã®ä¸é¨ããã®ã¾ã¾åç¨ãã¦ãã¾ãã
ã§ããããåçè«ã«ã©ãã¤ãªããã®ï¼
åçè«ã«ã¯ãå®ã«ãããããªæµåã»æµæ´¾ã»ã¹ã¿ã¤ã«ãããã¾ããããã®ã²ã¨ã¤ã«ããåã¨ã¯ãã¤ã³ã¿ã¼ãã§ã¼ã¹ï¼å¶ç´ã ãã¨ã¿ãªããã®ãããã¾ããåã¯ããã®èãæ¹ãå²ã¨å®æ ï¼ç¾å¨ã®ããã°ã©ãã³ã°è¨èªï¼å®è¡ç°å¢ï¼ã«ãã£ã¦ããã¨æã£ã¦ãã¾ãã
ããã§ãå¶ç´ã¨ã¯âããã¤ãã®å¶ç´å¼ã®éã¾ãâã§è¡¨ç¾ããã¾ãããããã¤ãã®ãã«ã¯0åãå«ã¾ããã®ã§ãå¶ç´ãä¸åæããªãåãªãã¤ã³ã¿ã¼ãã§ã¼ã¹ãåã¨ã¿ãªãã¾ãï¼ãã¡ããããã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®å®è£ ã¯å¥½ãåæã«ãªãï¼ããã¤ã³ã¿ã¼ãã§ã¼ã¹ï¼å¶ç´ãã¯ä»æ§ã¨å¼ãã§ãããã®ã§ããã®æµåã¯"types as specifications"ã®ç«å ´ã¨å¼ã°ãã¾ããã¾ããâå¶ç´å¼ï¼è«çå¼ï¼ã®éã¾ãâã®ãã¨ããè«çå¦ã§ã¯ã»ãªãªã¼ã¨å¼ã¶ã®ã§ã"types as theories"ã®ç«å ´ã¨ãå¼ã³ã¾ãã
ãã®ãããªåæ¦å¿µï¼types as specifications/theoriesï¼ã«åºã¥ãããµãã¿ã¤ãï¼ã¹ã¼ãã¼ã¿ã¤ããåå¼·å¶ï¼åå¤æãªã©ã®è°è«ãã§ãã¾ãããã®ãããªç«å ´ããã¯ãåã¨ã¯ã©ã¹ã¯ã¾ã£ããã®å¥ç©ã§ããã¯ã©ã¹ããæ¨æºçã«åãå®ç¾©ããæ¹æ³ã¯ç¢ºãã«ããã¾ããããããªãã®æ®µåããå¿ è¦ã§ããé常ã®ç¶æ¿ãå§è²ããã¯ãæ£ããåé層ãä½ããªãï¼æ£ãããä¿è¨¼ã§ããªãï¼ãã¨ããããã¾ãããã©ã¡ã¼ã¿åãããåï¼genericsï¼ã®ï¼ããç¨åº¦ã¯ç´å¾ã§ããï¼æå³è«ãæ§æã§ãã¾ãã
ãããã«ãã¦ããä»æ§ï¼å¶ç´ï¼ãè«çå¼ã§æ¸ãä¸ããã¨ãåºæ¬çã»æ¬è³ªçã§ããç¾å®ã«ãããåºæ¥ãï¼åºæ¥ãªãã¯å¥ã«ãã¦ããè«çå¼ãéãã¦åãå®ç¾©ããããã¨æ³å®ããªãã¨ãè«çå¦ã®ææããªã«ãé©ç¨ã§ããªãã®ã§ãåã«ææ§ã»æ¦æ§ãªç¹°ãè¨ã«çµå§ããã ãã§ãä¸æ©ãåã«é²ãã¾ãããç²¾å¯ãªè°è«ã¯å ¨ç¶ã§ãã¾ãããä»æ§ãè«çå¼ã§æ¸ããªãã¨ãæåéãããã·ããã©ãã¤ã®ã§ããèªåã®è¶£å³å好ã主義主張ããããã¯ä¿¡ä»°åç½ï¼ä¾ï¼å¤éç¶æ¿ä¸æ³ï¼ å¤éç¶æ¿æ»ãï¼ï¼ã声ãèãã¦è¨ãã¤ã®ã£ã¦ããã©ããä½ã®çµè«ãåºã¾ãããï¼å°ãªãã¨ãããã¸ã«ã«ã«æå³ã®ããçµè«ãåºãè¦è¾¼ã¿ã¯ãªãï¼ã
ã¾ã¼ãã£ã¨ããå½¢å¼åãåã®è¶£å³å好ã«åè´ãã¦ããã®ã§ãããããããããå¯è½ãªãå½¢å¼åãã¹ããã¨ã®ä¸»ç¾©ä¸»å¼µã«è³ãããå½¢å¼åãææ°æ§ããããããã¨ããä¿¡ä»°ãæ±ãã¦ããããã ããã天ã«å¾ãåãã¦ããã®ã¯åã§ããã©ãã