-
Notifications
You must be signed in to change notification settings - Fork 0
/
imp1.ml
168 lines (135 loc) · 3.05 KB
/
imp1.ml
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
type bool =
| True
| False
(** val negb : bool -> bool **)
let negb = function
| True -> False
| False -> True
type nat =
| O
| S of nat
type 'a option =
| Some of 'a
| None
type sumbool =
| Left
| Right
(** val plus : nat -> nat -> nat **)
let rec plus n m =
match n with
| O -> m
| S p -> S (plus p m)
(** val mult : nat -> nat -> nat **)
let rec mult n m =
match n with
| O -> O
| S p -> plus m (mult p m)
(** val minus : nat -> nat -> nat **)
let rec minus n m =
match n with
| O -> n
| S k ->
(match m with
| O -> n
| S l -> minus k l)
(** val eq_nat_dec : nat -> nat -> sumbool **)
let rec eq_nat_dec n m =
match n with
| O ->
(match m with
| O -> Left
| S m0 -> Right)
| S n0 ->
(match m with
| O -> Right
| S m0 -> eq_nat_dec n0 m0)
(** val beq_nat : nat -> nat -> bool **)
let rec beq_nat n m =
match n with
| O ->
(match m with
| O -> True
| S n0 -> False)
| S n1 ->
(match m with
| O -> False
| S m1 -> beq_nat n1 m1)
(** val ble_nat : nat -> nat -> bool **)
let rec ble_nat n m =
match n with
| O -> True
| S n' ->
(match m with
| O -> False
| S m' -> ble_nat n' m')
type id =
nat
(* singleton inductive, whose constructor was Id *)
(** val eq_id_dec : id -> id -> sumbool **)
let eq_id_dec id1 id2 =
eq_nat_dec id1 id2
type state = id -> nat
(** val update : state -> id -> nat -> state **)
let update st x n x' =
match eq_id_dec x x' with
| Left -> n
| Right -> st x'
type aexp =
| ANum of nat
| AId of id
| APlus of aexp * aexp
| AMinus of aexp * aexp
| AMult of aexp * aexp
type bexp =
| BTrue
| BFalse
| BEq of aexp * aexp
| BLe of aexp * aexp
| BNot of bexp
| BAnd of bexp * bexp
(** val aeval : state -> aexp -> nat **)
let rec aeval st = function
| ANum n -> n
| AId x -> st x
| APlus (a1, a2) -> plus (aeval st a1) (aeval st a2)
| AMinus (a1, a2) -> minus (aeval st a1) (aeval st a2)
| AMult (a1, a2) -> mult (aeval st a1) (aeval st a2)
(** val beval : state -> bexp -> bool **)
let rec beval st = function
| BTrue -> True
| BFalse -> False
| BEq (a1, a2) -> beq_nat (aeval st a1) (aeval st a2)
| BLe (a1, a2) -> ble_nat (aeval st a1) (aeval st a2)
| BNot b1 -> negb (beval st b1)
| BAnd (b1, b2) ->
(match beval st b1 with
| True -> beval st b2
| False -> False)
type com =
| CSkip
| CAss of id * aexp
| CSeq of com * com
| CIf of bexp * com * com
| CWhile of bexp * com
(** val ceval_step : state -> com -> nat -> state option **)
let rec ceval_step st c = function
| O -> None
| S i' ->
(match c with
| CSkip -> Some st
| CAss (l, a1) -> Some (update st l (aeval st a1))
| CSeq (c1, c2) ->
(match ceval_step st c1 i' with
| Some st' -> ceval_step st' c2 i'
| None -> None)
| CIf (b, c1, c2) ->
(match beval st b with
| True -> ceval_step st c1 i'
| False -> ceval_step st c2 i')
| CWhile (b1, c1) ->
(match beval st b1 with
| True ->
(match ceval_step st c1 i' with
| Some st' -> ceval_step st' c i'
| None -> None)
| False -> Some st))