| 1 |
Axiom zeq : forall n m : nat, { n = m } + { n <> m }. |
| 2 |
|
| 3 |
Definition update (A: Set) (x: nat) (v: A) (s: nat -> A) : nat -> A := |
| 4 |
fun y => if zeq x y then v else s y. |
| 5 |
|
| 6 |
Arguments update [A]. |
| 7 |
|
| 8 |
Definition ident := nat. |
| 9 |
Parameter operator: Set. |
| 10 |
Parameter value: Set. |
| 11 |
Parameter is_true: value -> Prop. |
| 12 |
Definition label := nat. |
| 13 |
|
| 14 |
Inductive expr : Set := |
| 15 |
| Evar: ident -> expr |
| 16 |
| Econst: value -> expr |
| 17 |
| Eop: operator -> expr -> expr -> expr. |
| 18 |
|
| 19 |
Inductive stmt : Set := |
| 20 |
| Sskip: stmt |
| 21 |
| Sassign: ident -> expr -> stmt |
| 22 |
| Scall: ident -> ident -> expr -> stmt (* x := f(e) *) |
| 23 |
| Sreturn: expr -> stmt |
| 24 |
| Sseq: stmt -> stmt -> stmt |
| 25 |
| Sifthenelse: expr -> stmt -> stmt -> stmt |
| 26 |
| Sloop: stmt -> stmt |
| 27 |
| Sblock: stmt -> stmt |
| 28 |
| Sexit: nat -> stmt |
| 29 |
| Slabel: label -> stmt -> stmt |
| 30 |
| Sgoto: label -> stmt. |
| 31 |
|
| 32 |
Record function : Set := mkfunction { |
| 33 |
fn_param: ident; |
| 34 |
fn_body: stmt |
| 35 |
}. |
| 36 |
|
| 37 |
Parameter program: ident -> option function. |
| 38 |
|
| 39 |
Parameter main_function: ident. |
| 40 |
|
| 41 |
Definition store := ident -> value. |
| 42 |
|
| 43 |
Parameter empty_store : store. |
| 44 |
|
| 45 |
Parameter eval_op: operator -> value -> value -> option value. |
| 46 |
|
| 47 |
Fixpoint eval_expr (st: store) (e: expr) {struct e} : option value := |
| 48 |
match e with |
| 49 |
| Evar v => Some (st v) |
| 50 |
| Econst v => Some v |
| 51 |
| Eop op e1 e2 => |
| 52 |
match eval_expr st e1, eval_expr st e2 with |
| 53 |
| Some v1, Some v2 => eval_op op v1 v2 |
| 54 |
| _, _ => None |
| 55 |
end |
| 56 |
end. |
| 57 |
|
| 58 |
Inductive outcome: Set := |
| 59 |
| Onormal: outcome |
| 60 |
| Oexit: nat -> outcome |
| 61 |
| Ogoto: label -> outcome |
| 62 |
| Oreturn: value -> outcome. |
| 63 |
|
| 64 |
Definition outcome_block (out: outcome) : outcome := |
| 65 |
match out with |
| 66 |
| Onormal => Onormal |
| 67 |
| Oexit O => Onormal |
| 68 |
| Oexit (S m) => Oexit m |
| 69 |
| Ogoto lbl => Ogoto lbl |
| 70 |
| Oreturn v => Oreturn v |
| 71 |
end. |
| 72 |
|
| 73 |
Fixpoint label_defined (lbl: label) (s: stmt) {struct s}: Prop := |
| 74 |
match s with |
| 75 |
| Sskip => False |
| 76 |
| Sassign id e => False |
| 77 |
| Scall id fn e => False |
| 78 |
| Sreturn e => False |
| 79 |
| Sseq s1 s2 => label_defined lbl s1 \/ label_defined lbl s2 |
| 80 |
| Sifthenelse e s1 s2 => label_defined lbl s1 \/ label_defined lbl s2 |
| 81 |
| Sloop s1 => label_defined lbl s1 |
| 82 |
| Sblock s1 => label_defined lbl s1 |
| 83 |
| Sexit n => False |
| 84 |
| Slabel lbl1 s1 => lbl1 = lbl \/ label_defined lbl s1 |
| 85 |
| Sgoto lbl => False |
| 86 |
end. |
| 87 |
|
| 88 |
Inductive exec : stmt -> store -> outcome -> store -> Prop := |
| 89 |
| exec_skip: forall st, |
| 90 |
exec Sskip st Onormal st |
| 91 |
| exec_assign: forall id e st v, |
| 92 |
eval_expr st e = Some v -> |
| 93 |
exec (Sassign id e) st Onormal (update id v st) |
| 94 |
| exec_call: forall id fn e st v1 f v2 st', |
| 95 |
eval_expr st e = Some v1 -> |
| 96 |
program fn = Some f -> |
| 97 |
exec_function f (update f.(fn_param) v1 empty_store) v2 st' -> |
| 98 |
exec (Scall id fn e) st Onormal (update id v2 st) |
| 99 |
| exec_return: forall e st v, |
| 100 |
eval_expr st e = Some v -> |
| 101 |
exec (Sreturn e) st (Oreturn v) st |
| 102 |
| exec_seq_2: forall s1 s2 st st1 out' st', |
| 103 |
exec s1 st Onormal st1 -> exec s2 st1 out' st' -> |
| 104 |
exec (Sseq s1 s2) st out' st' |
| 105 |
| exec_seq_1: forall s1 s2 st out st', |
| 106 |
exec s1 st out st' -> out <> Onormal -> |
| 107 |
exec (Sseq s1 s2) st out st' |
| 108 |
| exec_ifthenelse_true: forall e s1 s2 st out st' v, |
| 109 |
eval_expr st e = Some v -> is_true v -> exec s1 st out st' -> |
| 110 |
exec (Sifthenelse e s1 s2) st out st' |
| 111 |
| exec_ifthenelse_false: forall e s1 s2 st out st' v, |
| 112 |
eval_expr st e = Some v -> ~is_true v -> exec s2 st out st' -> |
| 113 |
exec (Sifthenelse e s1 s2) st out st' |
| 114 |
| exec_loop_loop: forall s st st1 out' st', |
| 115 |
exec s st Onormal st1 -> |
| 116 |
exec (Sloop s) st1 out' st' -> |
| 117 |
exec (Sloop s) st out' st' |
| 118 |
| exec_loop_stop: forall s st st' out, |
| 119 |
exec s st out st' -> out <> Onormal -> |
| 120 |
exec (Sloop s) st out st' |
| 121 |
| exec_block: forall s st out st', |
| 122 |
exec s st out st' -> |
| 123 |
exec (Sblock s) st (outcome_block out) st' |
| 124 |
| exec_exit: forall n st, |
| 125 |
exec (Sexit n) st (Oexit n) st |
| 126 |
| exec_label: forall s lbl st st' out, |
| 127 |
exec s st out st' -> |
| 128 |
exec (Slabel lbl s) st out st' |
| 129 |
| exec_goto: forall st lbl, |
| 130 |
exec (Sgoto lbl) st (Ogoto lbl) st |
| 131 |
|
| 132 |
(** [execg lbl stmt st out st'] starts executing at label [lbl] within [s], |
| 133 |
in initial store [st]. The result of the execution is the outcome |
| 134 |
[out] with final store [st']. *) |
| 135 |
|
| 136 |
with execg: label -> stmt -> store -> outcome -> store -> Prop := |
| 137 |
| execg_left_seq_2: forall lbl s1 s2 st st1 out' st', |
| 138 |
execg lbl s1 st Onormal st1 -> exec s2 st1 out' st' -> |
| 139 |
execg lbl (Sseq s1 s2) st out' st' |
| 140 |
| execg_left_seq_1: forall lbl s1 s2 st out st', |
| 141 |
execg lbl s1 st out st' -> out <> Onormal -> |
| 142 |
execg lbl (Sseq s1 s2) st out st' |
| 143 |
| execg_right_seq: forall lbl s1 s2 st out st', |
| 144 |
~(label_defined lbl s1) -> |
| 145 |
execg lbl s2 st out st' -> |
| 146 |
execg lbl (Sseq s1 s2) st out st' |
| 147 |
| execg_ifthenelse_left: forall lbl e s1 s2 st out st', |
| 148 |
execg lbl s1 st out st' -> |
| 149 |
execg lbl (Sifthenelse e s1 s2) st out st' |
| 150 |
| execg_ifthenelse_right: forall lbl e s1 s2 st out st', |
| 151 |
~(label_defined lbl s1) -> |
| 152 |
execg lbl s2 st out st' -> |
| 153 |
execg lbl (Sifthenelse e s1 s2) st out st' |
| 154 |
| execg_loop_loop: forall lbl s st st1 out' st', |
| 155 |
execg lbl s st Onormal st1 -> |
| 156 |
exec (Sloop s) st1 out' st' -> |
| 157 |
execg lbl (Sloop s) st out' st' |
| 158 |
| execg_loop_stop: forall lbl s st st' out, |
| 159 |
execg lbl s st out st' -> out <> Onormal -> |
| 160 |
execg lbl (Sloop s) st out st' |
| 161 |
| execg_block: forall lbl s st out st', |
| 162 |
execg lbl s st out st' -> |
| 163 |
execg lbl (Sblock s) st (outcome_block out) st' |
| 164 |
| execg_label_found: forall lbl s st st' out, |
| 165 |
exec s st out st' -> |
| 166 |
execg lbl (Slabel lbl s) st out st' |
| 167 |
| execg_label_notfound: forall lbl s lbl' st st' out, |
| 168 |
lbl' <> lbl -> |
| 169 |
execg lbl s st out st' -> |
| 170 |
execg lbl (Slabel lbl' s) st out st' |
| 171 |
|
| 172 |
(** [exec_finish out st st'] takes the outcome [out] and the store [st] |
| 173 |
at the end of the evaluation of the program. If [out] is a [goto], |
| 174 |
execute again the program starting at the corresponding label. |
| 175 |
Iterate this way until [out] is [Onormal]. *) |
| 176 |
|
| 177 |
with exec_finish: function -> outcome -> store -> value -> store -> Prop := |
| 178 |
| exec_finish_normal: forall f st v, |
| 179 |
exec_finish f (Oreturn v) st v st |
| 180 |
| exec_finish_goto: forall f lbl st out v st1 st', |
| 181 |
execg lbl f.(fn_body) st out st1 -> |
| 182 |
exec_finish f out st1 v st' -> |
| 183 |
exec_finish f (Ogoto lbl) st v st' |
| 184 |
|
| 185 |
(** Execution of a function *) |
| 186 |
|
| 187 |
with exec_function: function -> store -> value -> store -> Prop := |
| 188 |
| exec_function_intro: forall f st out st1 v st', |
| 189 |
exec f.(fn_body) st out st1 -> |
| 190 |
exec_finish f out st1 v st' -> |
| 191 |
exec_function f st v st'. |
| 192 |
|
| 193 |
Scheme exec_ind4:= Minimality for exec Sort Prop |
| 194 |
with execg_ind4:= Minimality for execg Sort Prop |
| 195 |
with exec_finish_ind4 := Minimality for exec_finish Sort Prop |
| 196 |
with exec_function_ind4 := Minimality for exec_function Sort Prop. |
| 197 |
|
| 198 |
Scheme exec_dind4:= Induction for exec Sort Prop |
| 199 |
with execg_dind4:= Minimality for execg Sort Prop |
| 200 |
with exec_finish_dind4 := Induction for exec_finish Sort Prop |
| 201 |
with exec_function_dind4 := Induction for exec_function Sort Prop. |
| 202 |
|
| 203 |
Combined Scheme exec_inductiond from exec_dind4, execg_dind4, exec_finish_dind4, |
| 204 |
exec_function_dind4. |
| 205 |
|
| 206 |
Scheme exec_dind4' := Induction for exec Sort Prop |
| 207 |
with execg_dind4' := Induction for execg Sort Prop |
| 208 |
with exec_finish_dind4' := Induction for exec_finish Sort Prop |
| 209 |
with exec_function_dind4' := Induction for exec_function Sort Prop. |
| 210 |
|
| 211 |
Combined Scheme exec_induction from exec_ind4, execg_ind4, exec_finish_ind4, |
| 212 |
exec_function_ind4. |
| 213 |
|
| 214 |
Combined Scheme exec_inductiond' from exec_dind4', execg_dind4', exec_finish_dind4', |
| 215 |
exec_function_dind4'. |