Skip to content

Instantly share code, notes, and snippets.

@PhDP
Last active August 29, 2015 14:16
Show Gist options
  • Save PhDP/a1d24062400612eff217 to your computer and use it in GitHub Desktop.
Save PhDP/a1d24062400612eff217 to your computer and use it in GitHub Desktop.
First example of Harrison's "Handbook of Practical Logic and Automated Reasoning" in Haskell
data Expr =
Var String
| Const Int
| Add Expr Expr
| Mult Expr Expr
simplify1 :: Expr -> Expr
simplify1 e = case e of
Add (Const 0) x -> x
Add x (Const 0) -> x
Add (Const a) (Const b) -> Const $ a + b
Mult x (Const 0) -> Const 0
Mult (Const 0) x -> Const 0
Mult x (Const 1) -> x
Mult (Const 1) x -> x
Mult (Const a) (Const b) -> Const $ a * b
_ -> e
simplify :: Expr -> Expr
simplify e = case e of
Add x y -> simplify1 $ Add (simplify x) (simplify y)
Mult x y -> simplify1 $ Mult (simplify x) (simplify y)
_ -> simplify1 e
expr1 = Add (Mult (Add (Const 1) (Mult (Const 0) (Var "x"))) (Const 3)) (Const 12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment