Skip to content

Instantly share code, notes, and snippets.

@Superstar64
Last active November 24, 2020 22:32
Show Gist options
  • Save Superstar64/31d62d23b95ff1c1eb96b86acb9081ac to your computer and use it in GitHub Desktop.
Save Superstar64/31d62d23b95ff1c1eb96b86acb9081ac to your computer and use it in GitHub Desktop.
Extendable Lambda Calculus Interpreter
{- Copyright (C) Freddy Angel Cubas "Superstar64"
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
-}
{-# Language DeriveTraversable, TypeFamilies #-}
module Lambda where
import Data.List
import Data.Maybe
import Data.Foldable
import Data.Void
import Data.Functor.Const
import Data.Functor.Foldable
names prefix = prefix : (map (prefix ++) $ map show [0..])
inside list item = isJust $ find (==item) list
data Term e = TVariable String | TAbstraction String (Term e) | TApplication (Term e) (Term e) | TMisc (e (Term e))
data TermF e x = TVariableF String | TAbstractionF String x | TApplicationF x x | TMiscF (e x) deriving (Functor, Foldable, Traversable)
type instance Base (Term e) = TermF e
instance Functor e => Recursive (Term e) where
project (TVariable x) = TVariableF x
project (TAbstraction x e) = TAbstractionF x e
project (TApplication e e') = TApplicationF e e'
project (TMisc m) = TMiscF m
instance Functor e => Corecursive (Term e) where
embed (TVariableF x) = TVariable x
embed (TAbstractionF x e) = TAbstraction x e
embed (TApplicationF e e') = TApplication e e'
embed (TMiscF m) = TMisc m
data Normal e = BAbstraction String (Normal e) | BApplication String [Normal e] | BMisc (e (Normal e))
data NormalF e x = BAbstractionF String x | BApplicationF String [x] | BMiscF (e x) deriving (Functor, Foldable, Traversable)
type instance Base (Normal e) = NormalF e
instance Functor e => Recursive (Normal e) where
project (BAbstraction x e) = BAbstractionF x e
project (BApplication x e) = BApplicationF x e
project (BMisc m) = BMiscF m
instance Functor e => Corecursive (Normal e) where
embed (BAbstractionF x e) = BAbstraction x e
embed (BApplicationF x e) = BApplication x e
embed (BMiscF m) = BMisc m
vanilla :: Functor e => Normal e -> Term e
vanilla (BAbstraction x e) = TAbstraction x (vanilla e)
vanilla (BApplication x e) = foldl TApplication (TVariable x) (map vanilla e)
vanilla (BMisc m) = TMisc (fmap vanilla m)
freeVariables :: Foldable t => Term t -> [String]
freeVariables (TVariable x) = [x]
freeVariables (TAbstraction x e) = filter (/= x) (freeVariables e)
freeVariables (TApplication e e') = freeVariables e ++ freeVariables e'
freeVariables (TMisc m) = foldMap freeVariables m
substitute :: (Foldable e, Functor e) => Term e -> String -> Term e -> Term e
substitute r x (TVariable y) | x == y = r
substitute r x (TVariable y) = TVariable y
substitute r x (TAbstraction y e) | x == y = TAbstraction y e
substitute r x (TAbstraction y e) = TAbstraction fresh (substitute r x e') where
fresh = fromJust $ find (not . inside (freeVariables r) ) (names y)
e' = substitute (TVariable fresh) y e
substitute r x (TApplication t s) = TApplication (substitute r x t) (substitute r x s)
substitute r x (TMisc m) = TMisc $ fmap (substitute r x) m
beta :: (Traversable e) => Term e -> Maybe (Normal e)
beta (TVariable x) = Just $ BApplication x []
beta (TAbstraction x e) = beta e >>= \e' -> Just $ BAbstraction x e'
beta (TApplication e1 e2) = beta e1 >>= \e1' -> case e1' of
(BAbstraction x e) -> beta (substitute e2 x (vanilla e))
(BApplication x e) -> beta e2 >>= \e2' -> Just $ BApplication x (e ++ [e2'])
(BMisc m) -> Nothing
beta (TMisc m) = fmap BMisc $ traverse beta m
extract :: Traversable e => Normal e -> Maybe (Fix e)
extract (BMisc m) = fmap Fix (traverse extract m)
extract _ = Nothing
pretty :: (e (Term e) -> String) -> Term e -> String
pretty misc (TVariable x) = x
pretty misc (TAbstraction x e) = x ++ " => " ++ pretty misc e
pretty misc (TApplication e e') = "(" ++ pretty misc e ++ ")" ++ "(" ++ pretty misc e' ++ ")"
pretty misc (TMisc m) = misc m
test = pretty (absurd . getConst) . vanilla . fromJust . beta
twoLevel :: Term (TermF (Const Void))
twoLevel = TApplication (TAbstraction "x" (TMisc (TAbstractionF "y" (TVariable "x")) )) (TMisc (TVariableF "f"))
sample :: Term (Const Void)
sample = let (Just e) = beta twoLevel >>= extract in ana unfix e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment