簡體   English   中英

在SKI組合器中表達XOR

[英]Express XOR in SKI combinators

我試圖解決確定但你可以在代碼戰上滑雪。 它將在SKI組合器中表達lambda。 來源位於https://repl.it/@delta4d/SKI

經過一些研究,特別是組合邏輯 ,我能夠解決除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源代碼編譯,但出現運行時錯誤。

代碼報告報告:

 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 = \\xy -> x true y ,它工作得很好。


  1. https://www.codewars.com/kata/sure-but-can-you-ski-i
  2. https://en.wikipedia.org/wiki/Combinatory_logic
  3. http://ski.aditsu.net

這個問題是從雙用途的xλxy.y (x false true) x 它被迫同時擁有兩種類型。 因為y使用x ,所以y必須返回一些類似a東西。 現在這意味着x false true也是a類型。 所以類型a必須是(b -> b -> a) (對於某些b )。 但是,這是不可能的,因為這意味着a必須包含本身。

值得注意的是,您的解決方案是正確的。 SK,不是Haskell的類型系統。 所以為了解決這個問題,我們不需要在不同的類型中使用x兩次 那么為什么不用λxy.y(x false true)(x true false)使它們成為相同的類型λxy.y(x false true)(x true false)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM