Created
February 13, 2022 15:27
-
-
Save opqdonut/4c48c4e41589d18ae72a0e872b9e29d2 to your computer and use it in GitHub Desktop.
ASTs with Functor fixpoints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DeriveFunctor, StandaloneDeriving #-} | |
import Data.Functor.Classes (Show1) | |
data ExprFunctor a = Literal Int | Plus a a | Times a a | |
deriving (Show, Functor) | |
data LineNumber a = LineNumber Int a | |
deriving (Show, Functor) | |
data Fix f = Fix (f (Fix f)) | |
data Comp f g x = Comp (f (g x)) | |
deriving Functor | |
type Annotated = Fix (Comp LineNumber ExprFunctor) | |
type Expr = Fix ExprFunctor | |
annotated :: Annotated | |
annotated = Fix (Comp (LineNumber 1 (Plus | |
(Fix (Comp (LineNumber 2 (Literal 3)))) | |
(Fix (Comp (LineNumber 3 (Literal 5))))))) | |
mapFix :: (Functor f, Functor g) => (f (Fix f) -> g (Fix f)) -> Fix f -> Fix g | |
mapFix f (Fix x) = Fix (fmap (mapFix f) (f x)) | |
stripOne :: Comp LineNumber ExprFunctor x -> ExprFunctor x | |
stripOne (Comp (LineNumber _ e)) = e | |
strip :: Annotated -> Expr | |
strip = mapFix stripOne | |
unannotated :: Expr | |
unannotated = strip annotated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment