-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCond.hs
More file actions
53 lines (35 loc) · 811 Bytes
/
Cond.hs
File metadata and controls
53 lines (35 loc) · 811 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# LANGUAGE
DeriveFunctor,
FlexibleContexts,
MultiParamTypeClasses,
TypeOperators
#-}
module Cond where
import AlaCarte
import Prim
--
-- * Syntax
--
-- | Extend Primitive Operations
-- | Conditional
data Cond t = If t t t
deriving (Eq,Functor,Show)
-- | Smart constructor.
cond :: (Cond :<: t) => Term t -> Term t -> Term t -> Term t
cond c t e = inject (If c t e)
--
-- * Extend Pretty printing
--
instance Pretty Cond where
prettyAlg (If c t e) = unwords ["if", c, "then", t, "else", e, "end"]
--
-- * Extend Evaluation
--
evalCond :: PVal t-> PVal t -> PVal t -> PVal t
evalCond c t e =
case c of
B True -> t
B False -> e
_ -> error "Type error: non-boolean condition"
instance PEval Cond where
pevalAlg (If c t e) = evalCond c t e