-- Sample implementation of classic OO patterns in FLP. -- It includes: Composite, Interpreter and Visitor. -- Sergio Antoy -- Tue Jul 17 08:32:17 PDT 2001 -- Updated Mon Apr 18 09:03:37 PDT 2011 import Store -- Composite-like representation of the abstract syntax of simple -- expressions made up by literal integers, variables, and arithmetic -- and boolean operators. Boolean operations return 1 for true and -- 0 for false. data Expr = Num Int | Var String -- | Neg Expr | Add Expr Expr | Sub Expr Expr | Mul Expr Expr -- | Div Expr Expr | Gt Expr Expr -- Evaluate an expression in a give environment. -- The environment is the second argument of the function. -- Expressions are evaluated without side effects on the environment. evaluate (Num num) _ = num evaluate (Var var) z | get var num z = num where num free evaluate (Add e1 e2) z = (evaluate e1 z) + (evaluate e2 z) evaluate (Sub e1 e2) z = (evaluate e1 z) - (evaluate e2 z) evaluate (Mul e1 e2) z = (evaluate e1 z) * (evaluate e2 z) evaluate (Gt e1 e2) z = if (evaluate e1 z > evaluate e2 z) then 1 else 0 -- A couple of simple tests of expression construction and evaluation. Expr_test1 = evaluate (Sub (Num 8) (Num 5)) init_store Expr_test3 = print (evaluate (Var "var") z) where z = set "var" 4 init_store -- end of module