-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathLec_3_6.lhs
184 lines (108 loc) · 2.81 KB
/
Lec_3_6.lhs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
\begin{code}
{-@ LIQUID "--reflection" @-}
{-@ LIQUID "--diff" @-}
{-@ LIQUID "--ple" @-}
{-@ LIQUID "--short-names" @-}
{-@ infixr ++ @-} -- TODO: Silly to have to rewrite this annotation!
{-# LANGUAGE GADTs #-}
module Lec_3_6 where
import Prelude hiding ((++))
import ProofCombinators
import qualified State as S
import qualified Data.Set as S
import Expressions hiding (And)
import Imp
import BigStep
\end{code}
"AXIOMATIC SEMANTICS"
Floyd-Hoare Triples
===================
A Floyd-Hoare triple is of the form
{ P } c { Q }
where
- `P` and `Q` are assertions (think `BExp`) and
- `c` is a command (think `Com`)
What does it MEAN for {P} c {Q} to hold?
let s = "starting" state
let s' = "after you execute" c the state s "transitions to" s'
\begin{code}
{-@ type Legit P C Q = s:_ -> s':_ -> { bval P s } -> (BStep c s s') -> { bval Q s' } @-}
\end{code}
1. {True}
X <~ 5
{X = 5}
2. {X = 2}
X <~ X + 1
{X = 3}
3. {True}
X <~ 5;
Y <~ 0
{X = 5}
4. {True}
X <~ 5;
Y <~ X
{Y = 5}
Legit P C Q?
type Legit P C Q = s:_ -> s':_ -> { bval P s } -> (BStep c s s') -> { bval Q s' }
5. {X = 2 && X = 3}
X <~ 5
{X = 0}
LEGIT
6. {True}
SKIP
{False}
NOT-LEGIT
7. {False}
SKIP
{True}
LEGIT
type Legit P C Q = s:_, s':_ . IF bval P s && BStep c s s' THEN bval Q s'
6. {True}
SKIP
{False}
NOT-LEGIT
8. {True}
WHILE True DO
SKIP
{ False }
9.
{False}
SKIP
{False}
LEGIT
NOT-LEGIT
10. {X = 0}
WHILE X <= 0 DO
X <~ X + 1
{X = 1}
11. {X = 1}
WHILE (X > 0) DO
X <~ X + 1
{X = 100}
LEGIT
NOT-LEGIT
A Floyd-Hoare triple states that
IF
* The program `c` is starts at a state where the *precondition* `P` is True, and
* The program finishes execution
THEN
* At the final state, the *postcondition* `Q` will also evaluate to True.
Lets paraphrase the following Hoare triples in English.
1. `{True} c {X = 5}`
2. `{X = m} c {X = m + 5}`
3. `{X <= Y} c {Y <= X}`
4. `{True} c {False}`
The type `Assertion` formalizes the type for the
assertions (i.e. pre- and post-conditions) `P`, `Q`
appearing in the triples {P} c {Q}
\begin{code}
{-@ type Legit P C Q = s:_ -> s':_ -> { bval P s } -> (BStep c s s') -> { bval Q s' } @-}
type Assertion = BExp
type Legit = State -> State -> BStep -> Proof
{-@ lem_skip :: p:_ -> (Legit p Skip p) @-}
lem_skip :: Assertion -> Legit
lem_skip p = \s s' (BSkip {}) -> ()
{-@ lem_asgn :: x:_ -> a:_ -> q:_ -> (Legit (bsubst x a q) (Assign x a) q) @-}
lem_asgn :: Vname -> AExp -> Assertion -> Legit
lem_asgn x a q = \s s' (BAssign {}) -> ()
\end{code}