Skip to content

Instantly share code, notes, and snippets.

@tel
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tel/29eb767c7cb331104537 to your computer and use it in GitHub Desktop.
Save tel/29eb767c7cb331104537 to your computer and use it in GitHub Desktop.
Bidirectional, open-recursion AST
data Ty = Bool | Arr Ty Ty deriving (Eq, Show)
data IExpr c i
= Var Int
| App i c
| Annot c Ty
| If c i i
| ETrue
| EFalse
data CExpr i c
= Lam c
| CI i
x :: Weave CExpr IExpr
x = Weave (App (Weave (Var 0)) (Weave (CI (Weave (Var 1)))))
data Fix f = Fix (f (Fix f))
data Weave f g = Weave (g (Weave g f) (Weave f g))
-- interesting eliminator
weave :: (Bifunctor f, Bifunctor g) => (f a a -> a) -> (g a a -> a) -> (Weave f g -> a)
weave f g = go where go (Weave x) = g (bimap (weave g f) go x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment