Skip to content

Instantly share code, notes, and snippets.

@pedrofurla
Created January 20, 2021 03:48
Show Gist options
  • Save pedrofurla/1d71ca9515dd6a8bd661cffc25e24ad3 to your computer and use it in GitHub Desktop.
Save pedrofurla/1d71ca9515dd6a8bd661cffc25e24ad3 to your computer and use it in GitHub Desktop.
reddit l0z4cz
{-# OPTIONS_GHC -fforce-recomp #-}
module V where
data LExpr = Var String -- Variable
| App LExpr LExpr -- Funktionsapplikation
| Lam String LExpr -- Lambda-Abstraktion
newVar :: [String] -> String
newVar ls = go "z"
where go s | filter (==s) ls == [] = s
| otherwise = go (s++"'")
subst :: LExpr -> String -> LExpr -> LExpr
subst m x (App e1 e2) = App(subst m x e1) (subst m x e2)
subst m x (Var str) | str == x = m
| otherwise = Var str
subst m x (Lam str e) | str == x = Lam str e
| str/= x && not (elem str (free m)) = Lam str (subst m x e)
| otherwise = subst m x ((\y-> Lam y (subst (Var y) str e)) (newVar (free m)))
free :: LExpr -> [String]
free = error ""
e = Lam "y" (App (Var "f") (App (Var "x") (Var "y")))
m = App (Var "g") (Var "y")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment