quasiquoting for patterns
[clean-tests.git] / datatype / Interpreter.hs
1 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 module Interpreter where
5
6 import Language
7 import Control.Monad
8
9 newtype Interpreter a = I {runInterpreter :: Maybe a}
10 deriving (Functor, Applicative, Monad)
11
12 instance Expression Interpreter where
13 lit = pure
14 (+.) = liftM2 (+)
15 (-.) = liftM2 (-)
16 (/.) = liftM2 (/)
17 (*.) = liftM2 (*)
18 (^.) = liftM2 (^)
19 neg = fmap negate
20 (&.) = liftM2 (&&)
21 (|.) = liftM2 (||)
22 not = fmap Prelude.not
23 (==.) = liftM2 (==)
24 (/=.) = liftM2 (/=)
25 (<.) = liftM2 (<)
26 (>.) = liftM2 (>)
27 (<=.) = liftM2 (<=)
28 (>=.) = liftM2 (>=)
29 if' p t e = p >>= \b->if b then t else e
30
31 instance Function a Interpreter where
32 fun def = Main $
33 let g :- m = def g
34 in unmain m