blob: 9ba2c0a56e0aa55ffe52f28dfe82dfeeaabe1ab3 (
plain)
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
|
Require Import QhasmCommon QhasmEvalCommon.
Require Import Language.
Require Import List NPeano.
Module Qhasm <: Language.
Import ListNotations.
Import QhasmEval.
(* A constant upper-bound on the number of operations we run *)
Definition Params := unit.
Definition State := fun (_: Params) => State.
Transparent Params.
(* Program Types *)
Inductive QhasmStatement :=
| QAssign: Assignment -> QhasmStatement
| QOp: Operation -> QhasmStatement
| QCond: Conditional -> Label -> QhasmStatement
| QLabel: Label -> QhasmStatement
| QCall: Label -> QhasmStatement
| QRet: QhasmStatement.
Hint Constructors QhasmStatement.
Definition Program := fun (_: Params) => list QhasmStatement.
(* Only execute while loops a fixed number of times.
TODO (rsloan): can we do any better? *)
Fixpoint getLabelMap' {x} (prog: Program x) (cur: LabelMap) (index: nat): LabelMap :=
match prog with
| p :: ps =>
match p with
| QLabel label => @getLabelMap' x ps (NatM.add label index cur) (S index)
| _ => @getLabelMap' x ps cur (S index)
end
| [] => cur
end.
Definition getLabelMap {x} (prog: Program x): LabelMap :=
getLabelMap' prog (NatM.empty nat) O.
Inductive QhasmEval {x}: nat -> Program x -> LabelMap -> State x -> State x -> Prop :=
| QEOver: forall p n m s, (n > (length p))%nat -> QhasmEval n p m s s
| QEZero: forall p s m, QhasmEval O p m s s
| QEAssign: forall n p m a s s' s'',
(nth_error p n) = Some (QAssign a)
-> evalAssignment a s = Some s'
-> QhasmEval (S n) p m s' s''
-> QhasmEval n p m s s''
| QEOp: forall n p m a s s' s'',
(nth_error p n) = Some (QOp a)
-> evalOperation a s = Some s'
-> QhasmEval (S n) p m s' s''
-> QhasmEval n p m s s''
| QECondTrue: forall (n loc next: nat) p m c l s s',
(nth_error p n) = Some (QCond c l)
-> evalCond c s = Some true
-> NatM.find l m = Some loc
-> QhasmEval loc p m s s'
-> QhasmEval n p m s s'
| QECondFalse: forall (n loc next: nat) p m c l s s',
(nth_error p n) = Some (QCond c l)
-> evalCond c s = Some false
-> QhasmEval (S n) p m s s'
-> QhasmEval n p m s s'
| QERet: forall (n n': nat) s s' s'' p m,
(nth_error p n) = Some QRet
-> popRet s = Some (s', n')
-> QhasmEval n' p m s' s''
-> QhasmEval n p m s s''
| QECall: forall (w n n' lbl: nat) s s' s'' p m,
(nth_error p n) = Some (QCall lbl)
-> NatM.find lbl m = Some n'
-> QhasmEval n' p m (pushRet (S n) s') s''
-> QhasmEval n p m s s''
| QELabel: forall n p m l s s',
(nth_error p n) = Some (QLabel l)
-> QhasmEval (S n) p m s s'
-> QhasmEval n p m s s'.
Definition evaluatesTo := fun (x: Params) p => @QhasmEval x O p (getLabelMap p).
(* world peace *)
End Qhasm.
|