Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
- data AstF t
- type Ast = Fix AstF
- pattern Add :: Fix AstF -> Fix AstF -> Fix AstF
- pattern Mul :: Fix AstF -> Fix AstF -> Fix AstF
- pattern Abs :: Fix AstF -> Fix AstF
- pattern Real :: Double -> Fix AstF
- pattern Complex :: Double -> Double -> Fix AstF
- pattern Var :: String -> Fix AstF
- class Pretty a where
- expr1 :: Ast
- expr2 :: Ast
- expr3 :: Holey Ast
- data Type
- inferTypes :: Ast -> Annotated Type Ast
- unreal :: Ast -> Holey Ast
Documentation
An example AST type, as an F-algebra. This AST will be used to demonstrate some tricks for composing new data types while maintaining separation of concerns.
This approach was inspired by the recursion-schemes
library
and the "Data Types a la Carte" paper.
pattern Add :: Fix AstF -> Fix AstF -> Fix AstF Source #
Patterns to simplify the construction and destructuring of ASTs
A pretty-printing typeclass.
This demonstrates an approach to separation of concerns; the pretty-printer for normal ASTs is implemented independently from the pretty-printer for holes and the pretty-printer for annotations.
Although the implementations don't know about each other, they can
still be composed; as a result, a Holey Ast
knows how to print itself
using a mixture of the pretty-printer for Ast
s and the pretty-printer
for Hole
s.
Pretty (f (Fix f)) => Pretty (Fix f) Source # | Pretty-printer for fixpoints. |
Pretty t => Pretty (AstF t) Source # | Pretty-printer for ASTs. |
Pretty (f (Fix (HoleF f))) => Pretty (HoleF f (Fix (HoleF f))) Source # | Pretty-printer for holes. |
(Pretty (f (Fix (AnnotatedF a f))), Show a) => Pretty (AnnotatedF a f (Fix (AnnotatedF a f))) Source # | Pretty-printer for annotations. |
An expression-with-holes. Holes can be plugged with plug
.
λ> pretty expr3 "|(_ + 1.0)|"
λ> pretty (plug (Var "z") expr3) "|(z + 1.0)|"
λ> :t plug (Var "z") expr3 plug (Var "z") expr3 :: Fix AstF -- aka Ast