ãã£ã¨ãYã³ã³ããã¼ã¿ãä½ãæå³ãããã®ãªã®ããã©ãããæ義ãããã®ãããããã¾ããã
ååã使ããå帰ãã§ãã¾ããï¼ã¨ããã ãã®ãã®ãããªãã£ãã®ã§ããã
ã¾ãλããã
é¢æ°ã®è©±ããããã®ã§ãã
ãã®ã¨ãããã¡ãã¡
hoge(x) = x * 2
ã¨ãã¦hogeãã»ã»ã»ãã¨ãååãã¤ãã¦è©±ãé²ããã®ãããã©ããªã®ã§ãé¢æ°ãå¤ã¨ãã¦ããããã¨ä¾¿å©ã¨ãããã¨ã§ãλã¨ããå¤ãå®ç¾©ããã®ã§ãã
ããããã¨ãä¸ã®hogeé¢æ°ãªããã¯Î»(x)(x*2)ãªã©ã¨ããããã¾ãããå¼æ°ãããããã®ã«()ã使ãã¨ããããã¾ãããããã®ã§ã
λx.x*2
ã®ããã«è¡¨è¨ãã¾ãã
ã¨ããã®ãλã
ãã®ã¨ããλã«ãªã«ããããããããå¼æ°ã¨ãã¦ããããããé¨åãåç´ã«ããããã¾ãã
(λx.x*2)y
ã¨ãã£ãããxã®é¨åãyã§ããããã¦
(λx.x*2)y â y * 2
ã¨ãªãã¾ããλã®å¼æ°é¨åãä¸ããããå¼æ°ã§ç½®ãæãããã¨ãç°¡ç´ã¨ããã¾ãã
(λx.xã¯æãã)ä»æ¥ âä»æ¥ã¯æãã
ã§ããå¼ãè¤éã«ãªã£ã¦ããå·éã«ç½®ãæããã°å¤§ä¸å¤«ã§ãã
ãããåãã³ã¼ãã§æ¸ãã®ã§ãJavaã§å¿åã¯ã©ã¹ä½¿ãã¾ãã£ã¦æ¸ãã®ããããã ãã©ãç¡æå³ã«ããããããªããªãã®ã§ãä»åã¯Groovy使ãã¾ãã
Groovyã ã¨ãããªæã
{x -> x * 2}
å¼æ°ãªãã¦ã²ã¨ã¤ã§ååã ãã
é¢æ°ã«ã¯è¤æ°ã®å¼æ°ãã¨ããã®ãããã¾ãã
ãã¨ãã°ãããªæãã
def func(x, y){x + y}
ããã§ããã¨ãã°yã3ã«åºå®ããã¨ãå¼æ°ã²ã¨ã¤ã®é¢æ°ãã¤ããã¾ãã
def add3(x){func(x, 3)}
ãã©ããã®3ã®é¨åãæå®ã§ããããã«ãããã®ã§ããã¨ã§æå®ãã¦ãã¨ããæãã§ãå¼æ°ã²ã¨ã¤ã¨ãé¢æ°ãè¿ãã¦ãã¾ãã¾ãã
def add(x){ {y -> func(x, y)}}
ããããã¨ãadd(5)ã¨ããã¨ãã«å¼æ°ãã²ã¨ã¤ã¨ãé¢æ°ãè¿ãã¦ãã¾ãããªã®ã§(add(5))(3)ã¨ããã¨x+yãè¨ç®ãããããã«ãªãã¾ãã
ã¯ãã¼ã¸ã£ã§æ¸ãç´ãã¨ããã§ãã
def add = {x -> {y -> func(x, y)}}
ãããã£ã¦ãè¤æ°ã®å¼æ°ãã¨ãé¢æ°ã1å¼æ°ã®é¢æ°ã«å¤æãããã¨ãã«ãªã¼åã¨ããã¾ããã«ãªã¼åã®ãããã§ãé¢æ°ã®ãã¨ãèããã¨ãã¯1å¼æ°ã®é¢æ°ã ããèããã°ããã¨ãããã¨ã«ãªãã¾ãã
ãã£ããï¼
λã§æ¡ä»¶åå²ã§ãããã
ã«ãªã¼åã®ãããã§ãλã¯ã²ã¨ã¤ã®å¼æ°ãã¨ãå ´åã ããèããã°ãããã¨ã«ãªãã¾ããã
ããã§ããããªé¢æ°ãå°å
¥ãã¦ã¿ã¾ãã
true=λx.λy.x false=λx.λy.y
ããããã¨ãå¤æ°bãtrueãfalseãã¨ãã¨ãã¦
b(0)(1)
ã¨ããã¨ãbãtrueã®ã¨ã0ãfalseã®ã¨ã1ãè¿ãã¾ãã
λã§æ¡ä»¶åå²ãã§ããï¼
Groovyã§æ¸ãã¨ããã
def tr = {x -> {y -> x}} def fl = {x -> {y -> y}}
ã¤ãã§ã«ãbooleanããλçtrue/falseã«å¤æããboolã¨ããé¢æ°ãä½ã£ã¦ããã¾ãã
def bool = {b -> b ? tr : fl}
ããããã¨ããããªæãã§æ¡ä»¶åå²ã確èªã§ãã¾ãã
> bool(3 < 4)(2)(5) 2 > bool(3 < 1)(2)(5) 5
æ¡ä»¶åå²ãã¾ããï¼
è£è¶³(2011/11/16)
> tr(2)(5)
ã¨ããã¨2ã
> fl(2)(5)
ã¨ããã¨5ãè¿ãã¾ããã
ããããã¨
> var b=tr > b(2)(5) 2 > var b=fl > b(2)(5) 5
ã®ããã«bãtrãªã2ããflãªã5ãè¿ãã¾ãã
ãã¨ã¯ãæ¯è¼æ¼ç®åãããã§ã©ã ãã§å®ç¾©ããã®ã¯é¢åãªã®ã§ãGroobyã®booleanãtr/flã«å¤æããé¢æ°boolãä½ã£ã¦ãã¾ãã
æ¯è¼æ¼ç®åã¯ããã¨ã§åºãå¼ãç®ã¨åãæãã§å®ç¾©ãããã¨ãã§ãã¾ãã
λã§ã«ã¼ãã¯ï¼
æ¡ä»¶åå²ãã§ããããã«ã¼ããããããï¼ããã§åºã¦ããã®ãYã³ã³ããã¼ã¿ã§ãã
å®ç¾©ããã®ã¾ã¾æ¸ãã¨ããããæ·±ã追ããªãã®ãåã
Y=λf.(λx.f(xx))(λx.f(xx))
ã§ãããã«é¢æ°Mãèãã¦Yãé©ç¨ãããã¨ãããªãã¾ãã
YM = λf.(λx.f(xx))(λx.f(xx))M â (λx.M(xx))(λx.M(xx)) â M(λx.M(xx)(λx.M(xx)))
MãYMã«é©ç¨ãããã¨ãããªãã¾ããYMãä¸ã§ã§ã¦ããï¼çªç®ã®å¼ã«ç½®ãæãã¦ã¿ã¾ãã
M(YM) = M(λx.M(xx))(λx.M(xx))
ããã¯ãä¸ã®YMã®å¤æã¨åãã§ãããã®ãã¨ããããããªãã¾ãã
M(YM)=YM
é¢æ°ã«ãã®é¢æ°èªèº«ãé©ç¨ãããã¨ãã«Yãé©ç¨ããã¦ããã¨ããã®ã¾ã¾åããã®ã帰ã£ã¦ããã®ã§ãYãä¸åç¹æ¼ç®åã¨ããã¾ãã
ããã使ãã¨Î»ã§å帰é¢æ°ã表ç¾ã§ããããã«ãªãã¾ããå帰é¢æ°ã表ç¾ã§ããã¨ãããã¨ã¯ãã«ã¼ããã§ããã¨ãããã¨ã§ããæ¡ä»¶åå²ã¨ã«ã¼ããã§ããã¨ãããã¨ã¯ãããããè¨ç®ãã§ããã¨ãããã¨ã§ãã
λã§è¨ç®ãã§ãããã¨ããããã¾ããï¼
Yã³ã³ããã¼ã¿ãGroovyã§æ¸ãã¨ãããªãã¾ãã
def Y = {f -> {x -> f(x(x))}({x -> f(x(x))})}
ããã¤ã使ã£ã¦ãã£ããããããã£ã¦ã¿ã¾ãããã
> println Y({f -> {n -> n < 2 ? n : f(n - 1) + f(n - 2)}})(7) Exception in thread "main" java.lang.StackOverflowError
ã¬ã¬ãã¹ã¿ãã¯ãªã¼ãã¼ããã¼ã
ããã¯ãå¼ããã®ã¾ã¾è©ä¾¡ããã¦ãã¾ããã¨ã«åé¡ãããããã§ããããã§é
延è©ä¾¡ããããã®å¤æãããã¦ã¿ã¾ãã
mããã¾ãã¦ã¿ã¾ãã
def Z = {f -> {x -> {m -> f(x(x))(m)}}({x -> {m -> f(x(x))(m)}})}
ããããã¨ãç¡äºãã£ãããããã§ãã¾ãã
> println Z({f -> {n -> n < 2 ? n : f(n - 1) + f(n - 2)}})(7) 13
ãã®å¤æãè¡ã£ããã®ã¯Zã³ã³ããã¼ã¿ã¨ãããããã§ããããã°ã§ã¿ããããYã³ã³ããã¼ã¿ã§ãããï¼ãã¯ãã ãããZã³ã³ããã¼ã¿ãã§ãã¦ãã¾ãã
ãã£ããboolãä½ã£ããã¨ãªã®ã§ãããã使ã£ã¦ã¿ã¾ãããã
> println YY({f -> {n -> bool(n < 2)(n)(f(n - 1) + f(n - 2))}})(7) Exception in thread "main" java.lang.StackOverflowError
ã¬ã¬ãã¾ããã¦ããªã¼ãã¼ããã¼
ããã¯ãboolã§çã®å ´åãå½ã®å ´åã®å¼ããã®å ´ã§è©ä¾¡ããã¦ãã¾ã£ã¦ç¡éã«ã¼ãã«ãªã£ã¦ãã¾ãããã§ããã£ã±ãé 延è©ä¾¡ããªãã¨ããã¾ããã
> println YY({f -> {n -> bool(n < 2)({n})({f(n - 1) + f(n - 2)})()}})(7) 13
ã¨ãããã¨ã§ãæ¡ä»¶åå²ã¨ã«ã¼ããé¢æ°ã ãã§ã§ãã¦ãã¾ãã¾ãããããã¾ãããï¼
æ°åãλã§ããããã
ãã¦ãå¶å¾¡æ§é ãλã§ãããããã®ã§ãããã¾ã ããããã¨Î»ãããªããã®ãæ®ã£ã¦ãã¾ããã¨ãããã¨ã§ãæ°åãλã§è¡¨ãã¾ãããã
0=λf.λx.x 1=λf.λx.f(x) 2=λf.λx.f(f(x))
ã®ããã«ãé¢æ°fãå¤xã«ä½åé©ç¨ããããã§èªç¶æ°ã表ãã¾ãããã£ã¼ãã®æ°åã¨ããã¾ãããã£ã¼ããããï¼
ãããGroovyã§æ¸ãã¦ã¿ã¾ãã
def zero = {f -> {x -> x}} def one = {f -> {x -> f(x)}} def two = {f -> {x -> f(f(x))}}
ãããæ´æ°ã«ç½®ãæããã«ã¯ã0ã«+1ãä½åé©ç¨ããããã¨ãã風ã«èããã°ããã®ã§ããããªæãã§ãã
> println zero({x -> x + 1})(0) 0 > println one({x -> x + 1})(0) 1 > println two({x -> x + 1})(0) 2
ãã®ããã£ã¼ãæ°ããæ´æ°ã¸ã®å¤æã®é¢æ°ãä½ã£ã¦ããã¾ãããã
def toInt = {n -> n({x -> x + 1})(0)}
ã§ããã¹ã¦ã®æ°åããã¡ãã¡å®ç¾©ããããã«ããããªãã®ã§ã1å¢ããæ¼ç®ããã¦ã¿ã¾ãã
succ = λx.λf.λx.f(n f x)
ã¨ããããã¾ãã
Groovyã§æ¸ãã¨ããã
def succ = {n -> {f -> {x -> f(n(f)(x))}}}
試ãã¦ã¿ã¾ãã
> println toInt(succ(two)) 3
å¢ãã¾ããï¼
ãã£ãããããããã®ã§ã足ãç®ãããã§ããã足ãç®ã¯ããã§ãã
add = λm.λn.λf.λx.nf(m f x)
Groovyã§æ¸ãã°ããã
def add = {m -> {n -> {f -> {x -> n(f(m(f)(x)))}}}}
試ãã¦ã¿ã¾ã
> println toInt(add(two)(two)) 4
足ãã¾ããï¼
ãã¦Î»ã ãã§ãã£ãããã
ãã£ããããããã«ã¯ãæ¯è¼ãå¼ãç®ãλã§ããå¿
è¦ãããã¾ãã
ã¨ãããã¨ã§ãã¾ãåå¤æ¯è¼ããã ãåå¤æ¯è¼ã¯Groovyçλã ãã§ã¯ã ãã§ãä¸æ¦æ´æ°ã«å¤æãã¾ãããé¢æ°ãåå¤ãã©ãããGroovyã§å¤å®ã§ããªãã®ã§ä»æ¹ãªããã§ããã®ããªï¼Î»é
ã®åå¤é¢ä¿ãå¤å®ããã¢ã«ã´ãªãºã ãé£ãããã
ã¨ãããã¨ã§ããããªé¢æ°ãå°å
¥ãã¾ãã
def eq = {x -> {y -> toInt(x) == toInt(y) ? tr : fl}}
ããããã¨ããã使ãã¾ãã
> println eq(one)(two)("ãã")("ã¤ã¼") ã¤ã¼ > println eq(succ(one))(two)("ãã")("ã¤ã¼") ãã
ãã¦ãåé¡ã¯å¼ãç®ã§ãã1å¼ãé¢æ°ãå®ç¾©ãã¾ããä»åã¯Zã³ã³ããã¼ã¿ä½¿ã£ã¦ã«ã¼ããã¾ããã
def pred = Z({g -> {n -> {m -> eq(m)(succ(n)) ({n}) ({g(succ(n))(m)}) ()}}})(zero)
使ã£ã¦ã¿ã¾ãã
> println toInt(pred(add(two)(two))) 3
ãã¼ãå¼ãç®ãã§ããããã§ãã
ã¨ãããã¨ã§ããã£ããããã®æºåãã§ããã®ã§ããã£ã¦ã¿ã¾ãã
def fib = Z({f -> {n -> (eq(n)(zero) ({zero}) (eq(n)(one) ({one}) ({plus(f(pred(n)))(f(pred(pred(n))))}) ) )() }})
ã§ããï¼ã«ãã³ã ããã§LISPã¿ããï¼
ãããã¦ã¿ã¾ãã
> println toInt(fib( add(two)(add(two)(two)) )) 8 > println toInt(fib( succ(add(two)(add(two)(two))) )) 13
ãã¼ãããã§åºæ¬çã«Î»ã ãã§ãã£ãããããã§ãã¾ããï¼
ã¤ãã§ã«å¯¾ã§
æå¾ã«ã対ãå°å
¥ãã¦ã¿ã¾ãã対ããå°å
¥ã§ããã°ãã©ããªãã¼ã¿æ§é ãå®ç¾ã§ãã¾ãã
対ã¯ã次ã®ããã«å®ç¾©ã§ãã¾ãã
(M, N) = λz. if z then M else N
ããããã¨ãããããã®é ãããã®ããã«ãã¦åãã¾ãã
first = λp.p true second = λp.p false
ã¨ãããã¨ã§ãGroovyã§ãããªå®ç¾©ããã£ã¦ã¿ã¾ãã
def pair = {m -> {n -> {c -> c(m)(n)}}} def first = {p -> p(tr)} def second = {p -> p(fl)}
ãããã¦ã¿ã¾ãã
> def a = pair(two)(succ(two)) lambda.YComb$_run_closure9_closure42_closure43@74c3aa > println toInt(first(a)) 2 > println toInt(second(a)) 3
ããã¾ããï¼
ããã§ããã¨ã¯ããã°ãã°Î»ã ãã§ã¯ã¤ãã¯ã½ã¼ãã¨ãã§ãããã§ãã
λãããï¼ä¸ã®ä¸ãå
¨é¨é¢æ°ã§ãã
åèæç®:
è«çã¨Î»è¨ç®ã®é¢ä¿ããã«ãªã¼ãã¯ã¼ãã®å¯¾å¿ã§ãããããã¨ããããããã®ãã¾ã¨ããããæ¬ã
- ä½è : è©è°·æå·±,西å´çä¹
- åºç社/ã¡ã¼ã«ã¼: 岩波æ¸åº
- çºå£²æ¥: 2007/06/27
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 14人 ã¯ãªãã¯: 442å
- ãã®ååãå«ãããã° (36件) ãè¦ã