在 SKI 组合器中表达 XOR
Express XOR in SKI combinators
我正在尝试解决 sure but can you SKI on codewars. It is about to express lambda in SKI combinators. Source is at https://repl.it/@delta4d/SKI。
经过一些研究,尤其是 Combinatory Logic,我能够解决除 xor
.
以外的所有情况
我先把xor翻译成
xor x y = if y
then if x
then false
else true
else x
也就是
xor = \x y -> y (x false true) x
-- false = K I
-- true = K
将 lambda 应用于 SKI 规则,我得到了
\x.\y.y (x false true) x
\x.S (\y.y (x false true)) (K x)
\x.S (S I (K (x false true))) (K x)
S (\x.S (S I (K (x false true)))) K
S (S (K S) (\x.S I (K (x false true)))) K
S (S (K S) (S (K (S I)) (\x.K (x false true)))) K
S (S (K S) (S (K (S I)) (S (K K) (\x.x false true)))) K
S (S (K S) (S (K (S I)) (S (K K) (S (\x.x false) (K true))))) K
S (S (K S) (S (K (S I)) (S (K K) (S (S I (K false)) (K true))))) K
我查看了 http://ski.aditsu.net 上的 SKI 演示文稿,效果很好。
Haskell 源编译,但出现运行时错误。
Codewars 报告:
Couldn't match type `a' with `Bool' a'
`a' is a rigid type variable bound by
a type expected by the context: a -> a -> a at Kata.hs:66:9
Expected type: SKI
(Bool' a -> (Bool' a -> Bool' a -> Bool' a) -> a -> a -> a)
Actual type: SKI (Bool' (Bool' a))
In the second argument of `xorF', namely `true'
In the second argument of `($)', namely `xorF true true'
我用 prettyPrintSKI $ Ap (Ap xor' false) true
在本地进行了测试,它报告:
• Occurs check: cannot construct the infinite type: a20 ~ Bool' a20
Expected type: SKI
(Bool' a20 -> (Bool' a20 -> Bool' a20 -> Bool' a20) -> Bool' a20)
Actual type: SKI (Bool' (Bool' a20))
• In the second argument of ‘Ap’, namely ‘true’
In the second argument of ‘($)’, namely ‘Ap (Ap xor' false) true’
In the expression: prettyPrintSKI $ Ap (Ap xor' false) true
什么是无限类型?什么是刚性类型?
我在 or
上做的事情和 or = \x y -> x true y
一样,而且效果很好。
问题来自 λxy.y (x false true) x
中 x
的双重使用。它被迫同时具有两种类型。由于 y
使用 x
,y
必须 return 比方说类型 a
。现在这意味着 x false true
也是 a
类型。所以 a
类型的东西必须是 (b -> b -> a)
(对于某些 b
)。但那是不可能的,因为这意味着 a
必须包含它自己。
值得注意的是,您的解决方案是正确的。 SK,只是不是 Haskell 的类型系统。所以要修复我们不需要对不同类型使用 x
两次。那么,为什么不将它们设为与 λxy.y(x false true)(x true false)
相同的类型呢?
我正在尝试解决 sure but can you SKI on codewars. It is about to express lambda in SKI combinators. Source is at https://repl.it/@delta4d/SKI。
经过一些研究,尤其是 Combinatory Logic,我能够解决除 xor
.
我先把xor翻译成
xor x y = if y
then if x
then false
else true
else x
也就是
xor = \x y -> y (x false true) x
-- false = K I
-- true = K
将 lambda 应用于 SKI 规则,我得到了
\x.\y.y (x false true) x
\x.S (\y.y (x false true)) (K x)
\x.S (S I (K (x false true))) (K x)
S (\x.S (S I (K (x false true)))) K
S (S (K S) (\x.S I (K (x false true)))) K
S (S (K S) (S (K (S I)) (\x.K (x false true)))) K
S (S (K S) (S (K (S I)) (S (K K) (\x.x false true)))) K
S (S (K S) (S (K (S I)) (S (K K) (S (\x.x false) (K true))))) K
S (S (K S) (S (K (S I)) (S (K K) (S (S I (K false)) (K true))))) K
我查看了 http://ski.aditsu.net 上的 SKI 演示文稿,效果很好。
Haskell 源编译,但出现运行时错误。
Codewars 报告:
Couldn't match type `a' with `Bool' a' `a' is a rigid type variable bound by a type expected by the context: a -> a -> a at Kata.hs:66:9 Expected type: SKI (Bool' a -> (Bool' a -> Bool' a -> Bool' a) -> a -> a -> a) Actual type: SKI (Bool' (Bool' a)) In the second argument of `xorF', namely `true' In the second argument of `($)', namely `xorF true true'
我用 prettyPrintSKI $ Ap (Ap xor' false) true
在本地进行了测试,它报告:
• Occurs check: cannot construct the infinite type: a20 ~ Bool' a20 Expected type: SKI (Bool' a20 -> (Bool' a20 -> Bool' a20 -> Bool' a20) -> Bool' a20) Actual type: SKI (Bool' (Bool' a20)) • In the second argument of ‘Ap’, namely ‘true’ In the second argument of ‘($)’, namely ‘Ap (Ap xor' false) true’ In the expression: prettyPrintSKI $ Ap (Ap xor' false) true
什么是无限类型?什么是刚性类型?
我在 or
上做的事情和 or = \x y -> x true y
一样,而且效果很好。
问题来自 λxy.y (x false true) x
中 x
的双重使用。它被迫同时具有两种类型。由于 y
使用 x
,y
必须 return 比方说类型 a
。现在这意味着 x false true
也是 a
类型。所以 a
类型的东西必须是 (b -> b -> a)
(对于某些 b
)。但那是不可能的,因为这意味着 a
必须包含它自己。
值得注意的是,您的解决方案是正确的。 SK,只是不是 Haskell 的类型系统。所以要修复我们不需要对不同类型使用 x
两次。那么,为什么不将它们设为与 λxy.y(x false true)(x true false)
相同的类型呢?