Haskell ã§ã¯ãY ã³ã³ããã¼ã¿ãä½ããªãã¨èª¤è§£ãã¦ãã人ãããã®ã§ãã§ãããã¨ã示ãã¨åæã«ãããã¾ã§å¦ãã ãã¨ãã¾ã¨ãã¦ã¿ã¾ãã
é 延è©ä¾¡ãæ´»ããã Y ã³ã³ããã¼ã¿
é¢æ°åãç¨ããå帰ã使ã£ã¦ãããªããHaskell ã§ã¯é 延è©ä¾¡ã®ãããã§ãY ã³ã³ããã¼ã¿ãå®ç¾©ã§ãã Y x = x (Y x) ã®éãã«æ¸ãã¾ãã
y :: (a -> a) -> a y x = x (y x)
Y ã³ã³ããã¼ã¿ç¨ã®éä¹ãå®ç¾©ãã¦ã¿ã¾ãããã
fact :: Num a => (a -> a) -> a -> a fact = \f n -> if n == 0 then 1 else n * f (n-1)
以ä¸ã®ããã«åãã¾ãã
y fact 4 â 24
ã§ãããã®éä¹ã¯ Haskell ã£ã½ããªãã®ã§ãå ¥ãå£ã§åå²ããããã«æ¸ãç´ãã¦ã¿ã¾ãã
fact :: Num a => (a -> a) -> a -> a fact _ 0 = 1 fact f n = n * f (n-1)
fact ã¨ããååã§ã¯ãå帰ãã¦ã¾ãããããããæå¾ éãã«åãã¾ãã
y fact 4 â 24
åæ¨è«ããã¾ãã Y ã³ã³ããã¼ã¿
å®ç¾©éãã®å®è£ ã§ã¯ãY ã³ã³ããã¼ã¿èªèº«ã¯å帰ã使ã£ã¦å®ç¾©ãã¾ãããã©ã ãè¨ç®ããã¥ã¼ãªã³ã°å®å ¨ã§ãããã¨ã示ãã®ãç®çã§ããã°ããã®å®ç¾©ã¯ã¤ã³ããã§ããã©ã ãè¨ç®ã«ã¯ãååã使ã£ãå帰ããªãããã§ããååã使ã£ãå帰ããªãæ¡ä»¶ã®ä¸ã«ãY ã³ã³ããã¼ã¿ãä½ããªãã¨ããã¾ããã
ãã Scheme ã¨ãã§å®è£ ããã Y ã³ã³ããã¼ã¿ã¯ãã´ãã£ã´ãã£ãã¦ãã¦åããã«ããã¨æãã®ã§ãåå²ãã¦å®è£ ãã¦ã¿ã¾ãããã
import Unsafe.Coerce s :: (a -> b -> c) -> (a -> b) -> a -> c s x y z = x z (y z) l :: (a -> b) -> (c -> a) -> b l x y = x (y (unsafeCoerce y)) y :: (a -> a) -> a y = s l l
Y ã³ã³ããã¼ã¿ã®å®è£ æ¹æ³ã¯ããããããã¾ãããã㯠SLL ã¨è¡¨ç¾ã§ãã Y ã³ã³ããã¼ã¿ã§ããã©ã®é¢æ°ãå帰ã使ã£ã¦ããªããã¨ã«æ³¨æãã¦ä¸ããã
L ã®å®ç¾©ã¯ãæ¬å½ã¯ L x y = x (y y) ã§ãã(y y)ã®é¨åãèªå·±è¨åã«ãªã£ã¦ãGHC ã§ã¯ãã®é¨åã®åããã¾ãå¦çã§ãã¾ãããããã§ãunsafeCoerce ã§åæ¨è«ããã¾ããã¦ãã¾ãã
å®éã«åããã¨ã確ããã¦ããã¾ãããã
y fact 4 â 24
ä¸è¨ã®å®ç¾©ã§ã¯ããããªã L ã使ãã¾ãããããã£ã¨æ ¹æºç㪠S 㨠K ã®ã¿ããåºçºãã¦ãY ã«ãã©ãçããã¨ãã§ãã¾ãã
s :: (a -> b -> c) -> (a -> b) -> a -> c s x y z = x z (y z) k :: a -> b -> a k x y = x -- aka const i :: a -> a i = s k k -- aka id c :: (a -> b -> c) -> b -> a -> c c = s (b b s) (k k) -- aka flip b :: (b -> c) -> (a -> b) -> a -> c b = s (k s) k -- aka (.) m :: (a -> b) -> b m = s i (unsafeCoerce i) l :: (a -> b) -> (c -> a) -> b l = c b m y :: (a -> a) -> a y = s l l
(ãã®ã¾ãé³¥) M ãèªå·±è¨åããã®ã§ãunsafeCoerce ã§åããã¾ããã¦ãã¾ãã(ã¡ãªã¿ã«ãy = b m l ã§ã OK)
åæ¨è«ããã¾ãããªã Y ã³ã³ããã¼ã¿
åæ¨è«ããã¾ãããªãã¦ããnewtype ã使ã£ã¦åãå®ç¾©ããinline åããã©ã°ãã§æ¢ããã°ãY ã³ã³ããã¼ã¿ãå®è£ ã§ãã¾ããä¸è¨ã®ã³ã¼ãã® m 以éã以ä¸ã§ç½®ãæãã¦ä¸ããã
newtype I a = I (I a -> a) unI :: I a -> I a -> a unI (I x) = x {-# NOINLINE unI #-} m :: I a -> a m = s unI i l :: (a -> b) -> I a -> b l = c b m y :: (a -> a) -> a y = s l (b I l) -- s l (I . l)
ã¡ããã¨åãã¾ããã
y fact 4 â 24
ãã®æ¹æ³ã¯ãOleg ããã«æãã¦ããã ãã¾ããããããã¨ããããã¾ãã
ãã¾ã
Sxyz = xz(yz) -- <*> Kxy = x -- const Ix = x -- id, I = SKK Bxyz = x(yz) -- (.), B = S(KS)K Cxyz = xzy -- flip, C = S(BBS)(KK) Txy = yx -- T = CI Wxy = xyy -- W = SS(KI) = ST Mx = xx -- M = SII = STT = WI = WT Lxy = x(yy) -- L = CBM = BWB = QM Qxyz = y(xz) -- Q = CB Oxy = y(xy) -- O = SI Uxy = y(xxy) -- U = LO Yx = x(Yx) -- Y = SLL = BML = UU