Skip to content

Instantly share code, notes, and snippets.

@jtobin
Created May 31, 2014 07:48
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 jtobin/3fc26d852af9e82e378e to your computer and use it in GitHub Desktop.
Save jtobin/3fc26d852af9e82e378e to your computer and use it in GitHub Desktop.
HOAS language
{-# OPTIONS_GHC -fno-warn-missing-methods #-}
module HOAS where
data Expr =
Lit Int
| Add Expr Expr
| Let Expr (Expr -> Expr)
instance Num Expr where
fromInteger = Lit . fromInteger
(+) = Add
eval :: Expr -> Int
eval (Lit d) = d
eval (Add e0 e1) = eval e0 + eval e1
eval (Let e0 e1) =
let shared = Lit (eval e0)
in eval (e1 shared)
tree :: (Num a, Eq a) => a -> Expr
tree 0 = 1
tree n = Let (tree (n - 1)) (\shared -> shared + shared)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment