[英]Capture-avoiding substitution function -- Lambda calculus
我正在嘗試編寫一個在 Lambda 演算中執行避免捕獲替換的函數。 代碼編譯但沒有吐出正確的答案。 我已經寫了我期望代碼做的事情,我的理解正確嗎?
例如,我應該得到這個輸入的以下輸出( numeral 0
是教堂數字 0)
*Main> substitute "b" (numeral 0) example -- \a. \x. ((\y. a) x) b \c. \a. (\a. c) a (\f. \x. x) -- The incorrect result I actually got \c. \c. (\f. \x. x) (x (\b. a))
注意\y
被重命名為\a
由於替換(\ya)[N/b]
(我想我已經在我編寫的代碼中涵蓋了這一點,但如果我錯了請告訴我。)
import Data.Char
import Data.List
type Var = String
data Term =
Variable Var
| Lambda Var Term
| Apply Term Term
-- deriving Show
instance Show Term where
show = pretty
example :: Term -- \a. \x. ((\y. a) x) b
example = Lambda "a"
(Lambda "x" (Apply (Apply (Lambda "y" (Variable "a"))
(Variable "x"))
(Variable "b")))
pretty :: Term -> String
pretty = f 0
where
f i (Variable x) = x
f i (Lambda x m) = if i /= 0 then "(" ++ s ++ ")" else s
where s = "\\" ++ x ++ ". " ++ f 0 m
f i (Apply n m) = if i == 2 then "(" ++ s ++ ")" else s
where s = f 1 n ++ " " ++ f 2 m
substitute :: Var -> Term -> Term -> Term
substitute x n (Variable y)
--if y = x, then leave n alone
| y == x = n
-- otherwise change to y
| otherwise = Variable y
substitute x n (Lambda y m)
--(\y.M)[N/x] = \y.M if y = x
| y == x = Lambda y m
--otherwise \z.(M[z/y][N/x]), where `z` is a fresh variable name
--generated by the `fresh` function, `z` must not be used in M or N,
--and `z` cannot be equal `x`. The `used` function checks if a
--variable name has been used in `Lambda y m`
| otherwise = Lambda newZ newM
where newZ = fresh(used(Lambda y m))
newM = substitute x n m
substitute x n (Apply m2 m1) = Apply newM2 newM1
where newM1 = substitute x n m2
newM2 = substitute x n m1
used :: Term -> [Var]
used (Variable n) = [n]
used (Lambda n t) = merge [n] (used t)
used (Apply t1 t2) = merge (used t1) (used t2)
variables :: [Var]
variables = [l:[] | l <- ['a'..'z']] ++
[l:show x | x <- [1..], l <- ['a'..'z']]
filterFreshVariables :: [Var] -> [Var] -> [Var]
filterFreshVariables lst = filter ( `notElem` lst)
fresh :: [Var] -> Var
fresh lst = head (filterFreshVariables lst variables)
recursiveNumeral :: Int -> Term
recursiveNumeral i
| i == 0 = Variable "x"
| i > 0 = Apply(Variable "f")(recursiveNumeral(i-1))
numeral :: Int -> Term
numeral i = Lambda "f" (Lambda "x" (recursiveNumeral i))
merge :: Ord a => [a] -> [a] -> [a]
merge (x : xs) (y : ys)
| x < y = x : merge xs (y : ys)
| otherwise = y : merge (x : xs) ys
merge xs [] = xs
merge [] ys = ys
substitute xn (Lambda ym)
中的這部分不正確:
z
不得在M
或N
中使用”,但沒有什么能阻止這一點。 newZ
可能是n
中的一個變量,這會導致捕獲問題z/y
尚未完成 | otherwise = Lambda newZ newM
where newZ = fresh(used(Lambda y m))
newM = substitute x n m
使固定:
z
不得用於M
或N
”:newZ = fresh(used m `merge` used n)
M[z/y][N/x]
”:newM = substitute x n (substitute y (Variable newZ) m)
放在一起:
| otherwise = Lambda newZ newM
where
newZ = fresh(used m `merge` used n)
newM = substitute x n (substitute y (Variable newZ) m)
請注意,如上所述刷新所有綁定會導致難以理解結果和調試替換。 實際上y
只有在y
在n
時才需要刷新。 否則,您可以保留y
,添加以下子句:
| y `notElem` used n = Lambda y (substitute x n m)
另一種想法是修改fresh
以選擇與舊名稱相似的名稱,例如,通過附加數字直到不發生沖突。
我仍然錯過了一個錯誤: newZ
也不應該等於x
(最初被替換的變量)。
-- substitute [a -> \f. \x. x] in (\g. g), should be (\g. g)
ghci> substitute "a" (numeral 0) (Lambda "g" (Variable "g"))
\a. \g. \x. x
解決這個問題的兩種方法:
將x
添加到變量集以排除newZ
:
newZ = fresh ([x] `merge` used m `merge` used n)
如果你想一想,這個錯誤只會在x
不在m
中時才會出現,在這種情況下沒有什么可以替代的,所以另一種方法是添加一個更多的分支來跳過工作:
| x `notElem` used m = Lambda ym
放在一起:
substitute x n (Lambda y m)
--(\y.M)[N/x] = \y.M if y = x
| y == x = Lambda y m
| x `notElem` used m = Lambda y m
| y `notElem` used n = Lambda y (substitute x n m)
| otherwise = Lambda newZ newM
where newZ = fresh(used m `merge` used n)
newM = substitute x n (substitute y (Variable newZ) m)
輸出
ghci> example
\a. \x. (\y. a) x b
ghci> numeral 0
\f. \x. x
ghci> substitute "b" (numeral 0) example
\a. \c. (\y. a) c (\f. \x. x)
注意:我沒有試圖證明這段代碼是正確的(讀者練習:定義“正確”),可能仍然有我錯過的錯誤。 必須有一些關於 lambda 演算的課程,其中包含所有細節和陷阱,但我沒有費心去看。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.