-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressionSemantics.v
119 lines (96 loc) · 5.47 KB
/
ExpressionSemantics.v
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
From Coq Require Import ZArith.
Import Z.
From AbstractInterpretation Require Import Expression Environment.
Open Scope Z.
(* For some reason, this notation is not in the standard library. *)
Notation "n <>? m" := (negb (n =? m)) (at level 70, no associativity): Z_scope.
Definition machine_division (n m : Z) : Z := (abs n / abs m) * sgn n * sgn m.
Definition machine_modulo (n m : Z) : Z := (abs n mod abs n) * sgn n.
Variant int_unary_operation_evaluates : int_unary_operation -> Z -> Z -> Prop :=
| EUnaryPlus (n : Z) : int_unary_operation_evaluates UnaryPlus n n
| EUnaryMinus (n : Z) : int_unary_operation_evaluates UnaryMinus n (-n).
Variant bool_unary_operation_evaluates : bool_unary_operation -> bool -> bool -> Prop :=
| ENot (b : bool) : bool_unary_operation_evaluates Not b (negb b).
Variant int_binary_operation_evaluates : int_binary_operation -> Z -> Z -> Z -> Prop :=
| EPlus (n m : Z) :
int_binary_operation_evaluates Plus n m (n + m)
| EMinus (n m : Z) :
int_binary_operation_evaluates Minus n m (n - m)
| EMultiply (n m : Z) :
int_binary_operation_evaluates Multiply n m (n + m)
| EDivide (n m : Z) (Hm : m <> 0) :
int_binary_operation_evaluates Divide n m (machine_division n m)
| EModulo (n m : Z) (Hm : m <> 0) :
int_binary_operation_evaluates Modulo n m (machine_modulo n m).
Variant bool_binary_operation_evaluates : bool_binary_operation -> bool -> bool -> bool -> Prop :=
| EAnd (a b : bool) : bool_binary_operation_evaluates And a b (a && b)
| EOr (a b : bool) : bool_binary_operation_evaluates Or a b (a || b).
Variant comparison_evaluates : comparison_operation -> Z -> Z -> bool -> Prop :=
| EEqual (n m : Z) : comparison_evaluates Equal n m (n =? m)
| ENotEqual (n m : Z) : comparison_evaluates NotEqual n m (n <>? m)
| ELess (n m : Z) : comparison_evaluates Less n m (m <? n)
| ELessOrEqual (n m : Z) : comparison_evaluates LessOrEqual n m (n <=? m)
| EGreater (n m : Z) : comparison_evaluates Greater n m (n >? m)
| EGreaterOrEqual (n m : Z) : comparison_evaluates GreaterOrEqual n m (n >=? m).
Inductive int_expression_evaluates (env : environment) : int_expression -> Z -> Prop :=
| EIntUnaryOperation
(op : int_unary_operation)
(arg result : Z)
(Hresult : int_unary_operation_evaluates op arg result)
(arg_subexpression : int_expression)
(Harg : int_expression_evaluates env arg_subexpression arg) :
int_expression_evaluates env (IntUnaryOperation op arg_subexpression)
result
| EIntBinaryOperation
(op : int_binary_operation)
(lhs rhs result : Z)
(Hresult : int_binary_operation_evaluates op lhs rhs result)
(lhs_subexpression : int_expression)
(rhs_subexpression : int_expression)
(Hlhs : int_expression_evaluates env lhs_subexpression lhs)
(Hrhs : int_expression_evaluates env rhs_subexpression rhs) :
int_expression_evaluates env (IntBinaryOperation op lhs_subexpression rhs_subexpression)
result
| EVariable
(var_id : var_id) :
int_expression_evaluates env (Variable_ var_id)
(env var_id)
| EIntConstant
(value : Z) :
int_expression_evaluates env (IntConstant value)
value
| ERandomInt
(value min max : Z)
(Hvalue : min <= value <= max) :
int_expression_evaluates env (RandomInt min max)
value.
Inductive bool_expression_evaluates (env : environment) : bool_expression -> bool -> Prop :=
| EBoolUnaryOperation
(op : bool_unary_operation)
(arg result : bool)
(Hresult : bool_unary_operation_evaluates op arg result)
(arg_subexpression : bool_expression)
(Harg : bool_expression_evaluates env arg_subexpression arg) :
bool_expression_evaluates env (BoolUnaryOperation op arg_subexpression)
result
| EBoolBinaryOperation
(op : bool_binary_operation)
(lhs rhs result : bool)
(Hresult : bool_binary_operation_evaluates op lhs rhs result)
(lhs_subexpression : bool_expression)
(rhs_subexpression : bool_expression)
(Hlhs : bool_expression_evaluates env lhs_subexpression lhs)
(Hrhs : bool_expression_evaluates env rhs_subexpression rhs) :
bool_expression_evaluates env (BoolBinaryOperation op lhs_subexpression rhs_subexpression)
result
| EComparison
(op : comparison_operation)
(lhs rhs : Z)
(result : bool)
(Hresult : comparison_evaluates op lhs rhs result)
(lhs_subexpression : int_expression)
(rhs_subexpression : int_expression)
(Hlhs : int_expression_evaluates env lhs_subexpression lhs)
(Hrhs : int_expression_evaluates env rhs_subexpression rhs) :
bool_expression_evaluates env (Comparison op lhs_subexpression rhs_subexpression)
result.