Created
May 31, 2014 07:48
-
-
Save jtobin/3fc26d852af9e82e378e to your computer and use it in GitHub Desktop.
HOAS language
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
{-# 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