1 module Language.Pointwise.Syntax where
3 import Data.Generics hiding (Unit,Inl,Inr)
6 import Generics.Pointless.Combinators
10 | Const String -- Is this really necessary?
15 | Match Term [(Term,Term)]
23 deriving (Eq,Show,Data,Typeable)
25 isPair :: Term -> Bool
26 isPair (_ :&: _) = True
43 isInlr :: Term -> Bool
44 isInlr t = isInl t || isInr t
52 -- only works correctly when there are no Match constructors in Term
54 free :: Term -> [String]
56 free (Lam v x) = delete v (free x)
57 free e = nub (concat (gmapQ (mkQ [] free) e))
60 -- only works correctly when there are no Match constructors in Term
62 subst :: [(String, Term)] -> Term -> Term
68 let x = concat (concatMap free (e : map snd s))
69 in Lam x (subst ((y, Var x) : s) e)
71 gmapT (mkT (subst s)) t
73 -- single traversal (bottom-up) beta reduction
76 step = everywhere (mkT aux)
77 where aux ((Lam v t) :@: m) = subst [(v,m)] t
80 -- Replacing constant by definition
82 replace :: [(String, Term)] -> Term -> Term
83 replace defs = everywhere (mkT aux)
84 where aux (Const x) = fromMaybe (Const x) (lookup x defs)
90 distr = Lam "x" (Match (Var "x") [(Var "y" :&: Inl (Var "z"),Inl (Var "y" :&: Var "z")),(Var "y" :&: Inr (Var "z"),Inr (Var "y" :&: Var "z"))])
93 swap = Lam "x" (Match (Var "x") [(Var "w" :&: Var "y", Var "y" :&: Var "w")])
96 coswap = Lam "x" (Match (Var "x") [(Inl (Var "y"), Inr (Var "y")),(Inr (Var "y"), Inl (Var "y"))])
99 assocr = Lam "w" (Match (Var "w") [((Var "x" :&: Var "y") :&: Var "z", Var "x" :&: (Var "y" :&: Var "z"))])
102 coassocr = Lam "w" (Match (Var "w") [(Inl (Inl (Var "x")), Inl (Var "x")),(Inl (Inr (Var "x")), Inr (Inl (Var "x"))),(Inr (Var "x"), Inr (Inr (Var "x")))])
105 test = Lam "x" (Match (Var "x") [(Var "y", Var "y")])