-
Notifications
You must be signed in to change notification settings - Fork 24
/
Expressions.hs
65 lines (50 loc) · 1.68 KB
/
Expressions.hs
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
52
53
54
55
56
57
58
59
60
61
62
63
{-@ LIQUID "--reflection" @-}
{-@ LIQUID "--ple" @-}
{-@ LIQUID "--diff" @-}
{-@ infixr ++ @-} -- TODO: Silly to have to rewrite this annotation!
{-# LANGUAGE PartialTypeSignatures #-}
module Expressions where
import Prelude hiding ((++), const, sum)
import ProofCombinators
import Lists
import qualified State as S
--------------------------------------------------------------------------------
-- | Arithmetic Expressions
--------------------------------------------------------------------------------
type Vname = String
data AExp
= N Val
| V Vname
| Plus AExp AExp
deriving (Show)
type Val = Int
type State = S.GState Vname Val
{-@ reflect aval @-}
aval :: AExp -> State -> Val
aval (N n) _ = n
aval (V x) s = S.get s x
aval (Plus e1 e2) s = aval e1 s + aval e2 s
{-@ reflect asgn @-}
asgn :: Vname -> AExp -> State -> State
asgn x a s = S.set s x (aval a s)
--------------------------------------------------------------------------------
-- | Boolean Expressions
--------------------------------------------------------------------------------
data BExp
= Bc Bool -- true, false
| Not BExp -- not b
| And BExp BExp -- b1 && b2
| Less AExp AExp -- a1 < a2
deriving (Show)
{-@ reflect bOr @-}
bOr :: BExp -> BExp -> BExp
bOr b1 b2 = Not ((Not b1) `And` (Not b2))
{-@ reflect bImp @-}
bImp :: BExp -> BExp -> BExp
bImp b1 b2 = bOr (Not b1) b2
{-@ reflect bval @-}
bval :: BExp -> State -> Bool
bval (Bc b) s = b
bval (Not b) s = not (bval b s)
bval (And b1 b2) s = bval b1 s && bval b2 s
bval (Less a1 a2) s = aval a1 s < aval a2 s