ä¸å¹´ã»ã©åã«ããã¢ãããã¯å¤ç¸ vs ãã©ã¡ããªãã¯å¤ç¸ãããã¸ã¡ã«èãã¦ã¯ãããªããã¨ããè¨äºãæ¸ãã¾ãããå¤ç¸æ§ãäºç¨®é¡ã«åé¡ã§ããããã§ã¯ãªãã¦ããã©ã¡ããªãã¯æ§ï¼ãããã¯ã¢ãããã¯æ§ï¼ã¯ããããç¨åº¦åé¡ã ãã¨ãã話ã§ãããé¢æ°å®ç¾©ãã²ã¨ã¤ã§æ¸ãå¤ç¸ã¯ãã©ã¡ããªãã¯å¤ç¸ããªãã¦ããææ§ãªå®ç¾©ã¯ãã¾ãæå³ãããã¾ããã
ããããç¨åº¦åé¡ã ã¨ããã¨ããã®âç¨åº¦âã測ã£ã¦æ¯è¼ãããã¨ã¯æãã¾ããããã¢ãããã¯å¤ç¸ vs ãã©ã¡ããªãã¯å¤ç¸ãããã¸ã¡ã«èãã¦ã¯ãããªã // ããã§ããã¸ã¡ã«èãããã®ãªããã«æ¬¡ã®ããã«æ¸ãã¾ããã
å¤ç¸é¢æ°ãèªç¶å¤æã¨ãã¦å®å¼åãã¦ãèªç¶å¤æã®ããã ã®é¢ä¿ãã¢ãããã¯æ§ãè¨ã尺度ã«ç¨ãã¾ããå®éãããã¤ãã®æ¹æ³ã§èªç¶å¤æã®ããã ã®é åºãå®ç¾©ã§ãã¾ãããã®é åºã¯ãã¢ãããã¯æ§ã®ç¨åº¦ã¨ã¿ãªãã¾ãã
ä¸è¨ã®æ¹æ³ã§ãã©ã¡ããªãã¯æ§ï¼ã¢ãããã¯æ§ï¼ãå®éã«æ¸¬ã£ã¦æ¯è¼ãã¦ã¿ã¾ãã
å 容ï¼
- ä¾é¡ã¯lengthé¢æ°
- èªç¶å¤æã¨ãã¦ã®lengthé¢æ°
- Haskell風 vs åè«é¢¨
- 3人ã®ããã°ã©ãã«lengthãä½ã£ã¦ããã
- é¨ååã¨èªç¶æ§
- é¨ååã«ç¸å¯¾çãªãã©ã¡ããªãã¯æ§
ç¶ãã®è¨äºï¼
ä¾é¡ã¯lengthé¢æ°
ä¾é¡ã¨ãã¦ããªã¹ãã®é·ããæ±ããé¢æ°lengthãèãã¾ããå¤ç¸é¢æ°ã¨ãã¦ã®lengthããåçè«ã§ã¯å²ã¨æ¨æºçï¼ã§Haskell風ã®ï¼æ¸ãæ¹ã§å°å ¥ãã¾ãã
lengthã®å ¥åºåã®ä»æ§ããããã¡ã¤ã«ãã¯æ¬¡ã®ããã«å®£è¨ããã¾ãã
ã³ãã³2åã¯Haskellã®æ§æã§ãæ®éã®ï¼å¤æ°æ´¾ã®ï¼è¨æ³ãªãã³ãã³1åã§ããã¾ããåçè«ã®ç¿æ £ã¨ãã¦ãåã表ãå¤æ°ã¯ã®ãªã·ã£å°æåã«ãã¾ãããªã®ã§ãαã¯åå¤æ°ãåãã©ã¡ã¼ã¿ãã§ããé¢æ°ããåä¸ã®åã§ã¯ãªãã¦è²ã ãªåã«é©ç¨å¯è½ã§ãããã¨ãè¨å·'â'ã使ã£ã¦è¡¨ãã¾ãã'â'ã¯ããã¨ãã¨ã¯è¨å·è«çã®å ¨ç§°ééåãuniversal quantifierãã®è¨å·ã§ããåã¯ãããã«'â'ã使ãã®ã¯å¥½ãã§ã¯ããã¾ãããã ã£ã¦è«çã®'â'ã¨ããã¾ãä¼¼ã¦ãªãããããã¾ã¼ãæå³ã§ã¯ãªãã¦åã«è¨å·ã ããããã¼ã«åç¨ãã¦ããã¨è§£éãã¾ãããããâα. â¥â¥ãã¯ããè²ã ãªåαã«é¢ã㦠â¥â¥ãã¨èªã¿ã¾ãã
ã¯ãåαã®é ç®ãæå | è¦ç´ ããããªããªã¹ãã®åã§ããåæ§æåãtype constructorãã§ããListã¯ãã¼ãã³ä½ã§æ¸ãã¾ãï¼ããããç¿æ £ããããã¨ããã ãï¼ãIntã¯æ´æ°åã§ãï¼å ·ä½çãªååã¯ã©ãã³å¤§æåå§ã¾ãã®ååãã¨ããç´æï¼ããã£ã¦ãä¸ã®è¨å·ç表ç¾ã¯æ¬¡ã®ããã«èªãã¾ãã
- length ã¯ãè²ã ãªåαã«é¢ãã¦ãåαã®é ç®ãããªããªã¹ãã®åããæ´æ°åã¸ã®é¢æ°ã§ããã
ç¹å®ã®åξã«å¯¾ããlengthã®ã¤ã³ã¹ã¿ã³ã¹ï¼åãã©ã¡ã¼ã¿ã®å ·ä½åï¼ã¯ lengthξ ã®ããã«æ¸ãã¾ããä¸ä»ããæ¸ããªãã¨ãã¯ãlength<ξ>, length[ξ] ãªã©ã¨ãã¾ãã
å¤ç¸é¢æ°lengthããã©ã¡ããªãã¯ã§ãããã¨ãparametricity propertyãã¯æ¬¡ã®çå¼ã§è¡¨ç¾ãã¾ãã
ããã§ãmapï¼fmapã¨æ¸ããããã¨ãå¤ãï¼ã¯ãListåæ§æåã«ã¨ããªããããåé«éé¢æ°ã§ãä»»æã®é¢æ° f::α â βï¼æ®éã®æ¸ãæ¹ãªã f:α â βï¼ãå¼æ°ã¨ãã¦æ°ããé¢æ° map f = map(f) : List α â List β ãè¿ãã¾ãã
ä¸ã®çå¼ã¯ããããåé«éé¢æ°ã§ãããåããé¢æ°ãé©ç¨ããå¾ã§ããªã¹ãã®é·ãã¯å¤ãããªããã¨ã主張ãã¦ãã¾ãã
èªç¶å¤æã¨ãã¦ã®lengthé¢æ°
Liståæ§æåã®å¼æ°ã¨ãªãåã¯éåã¨è§£éãã¾ãããããã使ãåã¯ï¼
å | 対å¿ããéå |
---|---|
Bool | {True, False} |
Int | Z |
Real*1 | R |
Char | Ch = (Unicodeã®ãã¹ã¦ã®æåãããªãéå) |
éååãSetã¨ããã¨ãListåæ§æå㯠List:|Set| â |Set| ã¨ã¿ãªãã¾ããList(f) := map(f) ã¨ããã°ãListã¯Setä¸ã®èªå·±é¢æã«ãªãã¾ãã
- List:Set â Set ã¯é¢æã
å¥ãªé¢æKZã次ã®ããã«å®ç¾©ãã¾ãã
- For Xâ|Set|, KZ(X) := Z
- For X, Yâ|Set|, f:X â Y in Set, KZ(f) := idZ
å¤ç¸é¢æ°lengthã¯ãé¢æListããé¢æKZã¸ã®èªç¶å¤æã¨ãã¦å®ç¾©ãã¾ãã
- length::List â KZ:Set â Set
ããã§ã®ã³ãã³2åã¯ãHaskellã®æ§æã¨ã¯é¢ä¿ããã¾ãããèªç¶å¤æã¯ãâåã®åâã®2-å°ãªã®ã§ã³ãã³ã2åï¼ç¢å°ãäºéï¼ã«ãã¦ãã¾ããã¾ã£ããéã£ãç¿æ £ããã®ã³ãã³2åã§ããlengthãèªç¶å¤æã§ããããã«ã¯ã次ã®å¯æå³å¼ãè¦æ±ããã¾ãã
é¢æKZã®å®ç¾©ããç°¡ç¥åã§ãã¦ï¼
ãã®å¯æå³å¼ãçå¼ã§æ¸ãã°ï¼
ããã¯ãæ¸ãæ¹ãå¤å°éãã ãã§ããã©ã¡ããªãã¯æ§ã表ãçå¼ï¼ä¸ã«åæ²ï¼ã¨åãã§ãã
ã¤ã¾ãï¼
- å¤ç¸é¢æ°ã¨ã¯èªç¶å¤æã®ãã¨ã§ããã
- ãã©ã¡ããªãã¯æ§ã¨ã¯èªç¶æ§ãnaturalityãã®ãã¨ã§ããã
ããã§ãå¤ç¸é¢æ°ã¨ãã®ãã©ã¡ããªãã¯æ§ãåè«ã®æ çµã¿ã§è°è«ããæºåãã§ãã¾ããã
Haskell風 vs åè«é¢¨
äºç¨®é¡ã®æ¸ãæ¹ãæ··ãã£ã¦ããã®ã§ãããããã®ç¿»è¨³ã示ãã¦ããã¾ãã
Haskell風 | åè«é¢¨ |
---|---|
A, B, ..., X, Y | |
Z | |
R | |
f:X â Y ã¾ã㯠f:1 â [X, Y] | |
f:X â [Y, Z] ã¾ã㯠f:1 â [X, [Y, Z]] | |
f(x) ã¾ã㯠x;f ã¾ã㯠ev(f, x) | |
List(X) ï¼Xã¯å¯¾è±¡ï¼ | |
List(f) ï¼fã¯å°ï¼ | |
Ï::List â KY ï¼èªç¶å¤æï¼ | |
ÏA:List(A) â Y ï¼èªç¶å¤æã®æåï¼ |
åç¯ã§ã注æããããã«åè«é¢¨ã®ã³ãã³2åã¯èªç¶å¤æã ããã§ããæ£ç¢ºã«ã¯ãÏ::List â KY:Set â Set ï¼éååã®èªå·±é¢æã®ããã ã®èªç¶å¤æï¼ã§ããããã°ã©ãã³ã°è¨èªã ã¨é¢æï¼åæ§æåã¨ãããåé¢æ°ï¼ã®åã»ä½åãè¨è¿°ã§ããæ©è½ã¯ãã¾ãè¦ãããªãã§ã*2ã
å®æ°ã« Float, Double ãããã®ã¯ã³ã³ãã¥ã¼ã¿ç¹æã®äºæ ãªã®ã§ãRã«å¯¾å¿ããåã¯ï¼å®éã¨ã¯éãï¼ Real ã¨ãããã¨ã«ãã¾ãï¼åç¯ã§Realãæ¢ã«ä½¿ã£ã¦ããï¼ã以ä¸ã§ã¯ã主ã«åè«é¢¨è¨æ³ã使ããã¨ã«ãã¾ããããããã°ã©ãã³ã°çæèã§ã¯ããããã¼ãªçä¼¼ã³ã¼ãã使ãã¾ãã
3人ã®ããã°ã©ãã«lengthãä½ã£ã¦ããã
3人ã®ããã°ã©ããAåãBåãCåããã¾ããä¸å¸ã®Xããã次ã®æ示ãåºãã¾ããã
- Aåã«ï¼ length : List<Int> -> Int ã¨ããé¢æ°ãæ¸ãã
- Båã«ï¼ length : List<Char> -> Int ã¨ããé¢æ°ãæ¸ããï¼List<Char> ã¯æåååï¼
- Cåã«ï¼ length : List<Real> -> Int ã¨ããé¢æ°ãæ¸ãã
Aåã¨Båã¯ããªã¹ãã®é ç®ãè¦ç´ | æåãã®åæ°ãåå®ããé¢æ°ãæ¸ãã¾ãããCåã¯æ¬¡ã®å®ç¾©ã«åºã¥ãlengthé¢æ°ãæ¸ããã¨ãã¾ãã
ã¤ã¾ããList<Real>ï¼List(R)ï¼ã®è¦ç´ ãn次å ã¦ã¼ã¯ãªãã空éã®ãã¯ãã«ã ã¨è§£éãããã®é·ãï¼ã¦ã¼ã¯ãªããã»ãã«ã ï¼ãæ±ããã®ã§ããã¦ã¼ã¯ãªããã»ãã«ã ãã®ã¾ã¾ã ã¨å®æ°ãªã®ã§åæ¨äºå ¥ãã¦æ´æ°ã«ãã¦ãã¾ãã
AåãBåãCåãä½ã£ãlengthé¢æ°ããä¸æ¦ããããå¥ãªååã«ãã¦ãXããã1ã¤ã®é¢æ°ã«ã¾ã¨ããã¨ãã¾ãããã
function length<Int> = lengthByA function length<Char> = lengthByB function length<Real> = lengthByC
åå¼æ°ã®å ·ä½åã¯ãå¼ã³åºãæã«æ示çã«æå®ï¼length<Char>("hello") ã®ããã«ï¼ãã¦ãããããã³ã³ãã¤ã©ãåæ¨è«ãã¦è£ã£ã¦ãããã¨èãã¦ãããã§ãããããã¯ãå®è¡æã®åå ´ååããtype caseãã使ããã¨ãã¦ï¼
function length = lambda(x) { typecase (x) { List<Int> => lengthByA(x) List<Char> => lengthByB(x) List<Real> => lengthByC(x) } }
ãããã«ãã¦ããé ç®ã®åã Int, Char, Real ã§ãããªã¹ãã«é©ç¨å¯è½ãªå¤ç¸é¢æ°lengthãã§ãã¾ããããã®å¤ç¸é¢æ°ã¯ããã®ä½ãæ¹ãããã£ã¦ã¢ãããã¯ã§ããåãã¨ã®æ å½è ã¯ãé¢æ°åã®å°è±¡ã ãããåæã«å®è£ ãã¦ãã¾ããAåã¨Båãå ±éã®ä»æ§ã®é¢æ°ãå®è£ ããã®ã¯å¶ç¶ã§ã*3ã
ä»å®ç¾©ããlengthã¯ããã©ã¡ããªãã¯æ§ -- ã¤ã¾ãèªç¶æ§ãæã¤ã§ããããï¼
é¨ååã¨èªç¶æ§
å¤ç¸é¢æ°lengthã®èªç¶æ§ã®æ¡ä»¶ãä¸ã«åæ²ãã¾ãã
æåã«åºããæ¡ä»¶ã¨ä¸ç®æã ãéãããã£ã¦ãæåã®è¡ã®ãåSetãããåCãã«å¤ãã£ã¦ãã¾ããèããã¹ãé¢æ List, KZ ããSet â Set ã§ã¯ãªãã¦ãC â Set ã§ãããã¨ãã¦ãã¾ãããã ããCã¯Setã®é¨ååã§ããããã¨ã® List, KZ ãé¨ååã«å¶éãã¦èãã¾ãã
Setã®é¨ååS1ã次ã®ããã«å®ç¾©ãã¾ããChã¯åCharã«å¯¾å¿ããéåã§ããã
- |S1| := {Z, Ch, R}
- For X, Yâ|S1|, S1(X, Y) := Set(X, Y)
- å°ã®çµåã¨æçå°ã¯Setã¨åãã
S1ã¯3ã¤ã®å¯¾è±¡ãæã¤Setã®é¨ååã§ãï¼å æºé¨ååã¨å¼ã°ãã¾ãï¼ãããä¸ã®èªç¶æ§ã®æ¡ä»¶ã«ããã¦ãC = S1 ã¨ç½®ããã¨ããèªç¶æ§ã¯æç«ããã§ããããï¼ ãã¡ã§ãããä¾ãã°ãf:Z â R ã¨ãã¦åç´ã»å¸¸èçãªåãè¾¼ã¿ååãã¨ãã¨ã length<Int>( (3, 3) ) 㨠length<Real>( (3, 3) ) ã¯ä¸è´ãã¾ããã
ããã¼ããã£ã±ãã¢ãããã¯ã ãªãã¨æãã§ããããããããé¨ååS2ã次ã®ããã«å®ç¾©ãããã¨ãã§ãã¾ãã
- |S2| := {Z, Ch, R}
- For X, Yâ|S2|,
S2(X, X) := {idX}
X â Y ãªãã° S2(X, X) := {} - å°ã®çµåã¨æçå°ã¯Setã¨åãã
S2ã3ã¤ã®å¯¾è±¡ãæã¤Setã®é¨ååã§ãï¼é¢æ£é¨ååã¨å¼ã°ãã¾ãï¼ãC = S2 ã¨ç½®ããã¨ããèªç¶æ§ã¯æç«ãã¾ãã ã«ããã¦é¸ã¹ãfããidZ, idCh, idR ãããªããå³å¼ãå¯æã¨ãªãã®ã¯èªæã§ãã
S1ã¨S2ã®ä¸éã®åã¨ãã¦ãé¨ååS3ãå®ç¾©ãã¦ã¿ã¾ãã
- |S3| := {Z, Ch, R}
- ãã ã»ãã
- X, Yâ{Z, Ch} ãªãã°ãS3(X, Y) = Set(X, Y)
- S3(R, R) = {idR}
- ãã®ä»ã® S3(X, Y) = {}
- å°ã®çµåã¨æçå°ã¯Setã¨åãã
S2 ã§ãlengthã®èªç¶æ§ã¯æç«ãã¾ãã
å¤ç¸é¢æ°ãæ§æããé¢æã¨ååã®æã«å¯¾ãã¦ãå®ç¾©åã¨ãªãåã®åãæ¹ãå¤ããã¨ãèªç¶æ§ï¼ï¼ãã©ã¡ããªãã¯æ§ï¼ã¯æç«ãããããªãã£ãããã¾ãã
é¨ååã«ç¸å¯¾çãªãã©ã¡ããªãã¯æ§
次ã®ç¶æ³ãèãã¾ãããã
- F, G:Set â Set ã¯é¢æã
- Kã¯éåã®éåãåããã¨ã ã Kâ|Set| ã
- Ï:K â Mor(Set) ã¤ã¾ããÏã¯ãKã®è¦ç´ ï¼ããã¯éåï¼ã§ã¤ã³ããã¯ã¹ãããé¢æ°ã®æã
- dom(ÏX) = F(X), cod(ÏX) = G(X) ã
- C, D ãªã©ã¯Setã®é¨ååã§ã|C| = |D| = K ã
åç¯ã®è©±ã¯ãã®ç¶æ³ã«ãªã£ã¦ãã¾ãã
- List, KZ ã¯é¢æã
- {Z, Ch, R} ã¯éåã®éåã
- length:{Z, Ch, R} â Mor(Set) ã¯ã{Z, Ch, R} ã®è¦ç´ ã§ã¤ã³ããã¯ã¹ãããé¢æ°ã®æã
- dom(lengthX) = List(X), cod(lengthX) = KZ(X) ã
- S1, S2, S3 ã¯Setã®é¨ååã§ã|S1| = |S2| = |S3| = {Z, Ch, R} ã
ä¸è¬çã«ãÏãèªç¶æ§ãæã¤ãï¼èªç¶å¤æã«ãªã£ã¦ãããï¼ã¯ãé¨ååCã®é¸ã³æ¹ã«ããã¾ããããé¨ååCã«å¯¾ãã¦Ïãèªç¶æ§ãæã¤ã¨ã次ã®ããã«æ¸ãã¾ãããã
- Ï is-natural-on C
ããã¢ãããã¯å¤ç¸ vs ãã©ã¡ããªãã¯å¤ç¸ãããã¸ã¡ã«èãã¦ã¯ãããªããã®è注ã¨ãã¦æ¬¡ã®ããã«æ¸ãã¾ããã
ã¢ãããã¯å¤ç¸é¢æ°ã¯èªç¶å¤æã«ãªããªããã¨æã£ã¦ãã人ãããããç¥ãã¾ããããããªãã¨ã¯ããã¾ãããã¢ãããã¯å¤ç¸é¢æ°ï¼ã¨å¼ã°ãã¦ãããã®ï¼ã§ãèªç¶å¤æã«ä»ç«ã¦ããã¨ãã§ãã¾ãã
é¨ååCãé¢æ£åã«åãã°ãé¢æ£åCã«å¯¾ãã¦ãÏ is-natural-on C ã¯å¿ ãæç«ãã¾ããã©ããªã«ã¢ãããã¯ãªå¤ç¸é¢æ°ã§ããé¢æ£åã«å¯¾ãã¦ã¯ãã©ã¡ããªãã¯ãèªç¶ããªã®ã§ããããããç¨åº¦åé¡ã§ãã
ä»è¿°ã¹ãã»ããã£ã³ã°ã§ã¯ããã©ã¡ããªãã¯ãªç¨åº¦ï¼ã¢ãããã¯ãªç¨åº¦ï¼ã¯ãÏ is-natural-on C ã¨ãªãé¨ååCã®â大ããâã§æ¸¬ãã¾ããããã¯ãããããåºæºã§è¨ãã¨ç´æããããã§ãããã©ã¡ããªãã¯æ§ï¼ã¢ãããã¯æ§ï¼ãå¥ãªåºæºã§è¨ããã¨ãããã§ããããåè«ã使ããªãåºæºãããããç¥ãã¾ãããã§ãããåºæºãããããªã¨æ±ºããªãã§ãã¢ããã㯠vs ãã©ã¡ããªãã¯ãã¨è¨ã£ã¦ããããã¯ç¡æå³ã§ãã
*1:Realã«ã¤ãã¯æ¬¡ã®ç¯ã«æ¸ãã¦ããã¾ãã
*2:èªç¶å¤æã®çæ¯å°ãhabitat | ã¢ãã¿ãã¯ãåã®åCATã§ããèªç¶å¤æã®è¨è¿°ãããã«æ£ç¢ºã«ããã¨ãÏ::List â KY:Set â Set in CATãããã°ã©ãã³ã°è¨èªã®ãã¼ã¿åã®ä¸çãCATã¾ã§åºããã®ã¯å¤§å¤ã§ã¯ããã¾ãã
*3:ããã¢ãããã¯å¤ç¸ vs ãã©ã¡ããªãã¯å¤ç¸ãããã¸ã¡ã«èãã¦ã¯ãããªããã§ã¯ãããã¾ãã¾"square"ã¨ããåãååã§å¼ã°ãã3ã¤ã®é¢æ°ãããã¾ãã¾"increase"ã¨ããåãååã§å¼ã°ãã2ã¤ã®é¢æ°ãã®ä¾ãåºãã¦ãã¾ãã