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
185
186
187
188
189
190
191
192
193
194
195
196
open Lang
open Tactical
open Conditions
open Definitions
let definition f es =
let d = find_symbol f in
match d.d_definition with
| Function(_,_,u) ->
let sigma = Lang.subst d.d_params es in
F.e_subst sigma u
| Predicate(_,p) ->
let sigma = Lang.subst d.d_params es in
F.e_prop (F.p_subst sigma p)
| _ ->
raise Not_found
let range f es =
let a,b = Ctypes.bounds (Cint.is_cint f) in
let range e = F.p_and
(F.p_leq (F.e_zint a) e)
(F.p_leq e (F.e_zint b)) in
F.e_prop (F.p_all range es)
let unfold f es =
try definition f es with Not_found -> range f es
let rec applicable ?at e f es = function
| phi::others ->
begin
try
let v = phi f es in
let d = Format.asprintf "Unfold '%a'" Lang.Fun.pretty f in
Applicable (Tactical.rewrite ?at [d,F.p_true,e,v])
with Not_found | Invalid_argument _ ->
applicable ?at e f es others
end
| [] ->
Not_applicable
module Smap = Qed.Idxmap.Make
(struct
type t = step
let id s = s.id
end)
let condition original p =
match original.condition with
| Type _ -> Type p
| Have _ -> Have p
| When _ -> When p
| Core _ -> Core p
| Init _ -> Init p
| _ -> assert false
let collect_term_to_unfold (g, m) = function
| Inside(Step step, unfold) ->
let l =
try Smap.find step m
with Not_found -> []
in
g, Smap.add step (unfold :: l) m
| Inside (Goal _, unfold) ->
begin match g with
| None -> Some [ unfold ], m
| Some g -> Some (unfold :: g), m
end
| _ -> raise Not_found
let rec collect_unfold phis m e =
match phis with
| phi :: others ->
begin
try
match F.repr e with
| Qed.Logic.Fun(f,es) -> Lang.F.Tmap.add e (phi f es) m
| _ -> raise Not_found
with Not_found | Invalid_argument _ -> collect_unfold others m e
end
| [] -> m
let unfolds_from_list phis es =
List.fold_left (collect_unfold phis) Lang.F.Tmap.empty es
let unfolds_from_smap phis m =
Smap.map (fun _s es -> unfolds_from_list phis es) m
module Unfoldedset = Qed.Listset.Make(Lang.Fun)
let tactical_inside step unfolds sequent =
if Lang.F.Tmap.is_empty unfolds
then raise Not_found
else match step.condition with
| Type p | Have p | When p | Core p | Init p ->
let unfolded = ref Unfoldedset.empty in
let subst t =
let result = Lang.F.Tmap.find t unfolds in
begin match F.repr t with
| Qed.Logic.Fun(f,_) -> unfolded := Unfoldedset.add f !unfolded
| _ -> ()
end ;
result
in
let p = condition step @@ Lang.p_subst subst p in
let feedback =
let pp fmt f = Format.fprintf fmt "'%a'" Lang.Fun.pretty f in
Format.asprintf "Unfold %a"
(Pretty_utils.pp_list ~sep:", " pp) !unfolded
in
!unfolded, snd @@ Tactical.replace_single ~at:step.id (feedback, p) sequent
| _ -> raise Not_found
let tactical_goal unfolds (seq, g) =
if Lang.F.Tmap.is_empty unfolds
then raise Not_found
else
let subst t = Lang.F.Tmap.find t unfolds in
seq, Lang.p_subst subst g
let fold_selection goal_unfolds step_unfolds sequent =
let tactical s l (acc_unfolded, seq) =
let unfolded, seq = tactical_inside s l seq in
Unfoldedset.union unfolded acc_unfolded, seq
in
let unfolded, seq = Smap.fold tactical step_unfolds ([], sequent) in
let feedback =
let pp fmt f = Format.fprintf fmt "'%a'" Lang.Fun.pretty f in
Format.asprintf "Unfold %a"
(Pretty_utils.pp_list ~sep:", " pp) unfolded
in
let add_goal = match goal_unfolds with
| None -> Stdlib.Fun.id
| Some goal_unfolds -> tactical_goal goal_unfolds
in
feedback, add_goal seq
let process (f: sequent -> string * sequent) s = [ f s ]
class unfold =
object
inherit Tactical.make ~id:"Wp.unfold"
~title:"Definition"
~descr:"Unfold definitions of predicates and functions."
~params:[]
method select _feedback (s : Tactical.selection) =
let unfoldings = [ definition ; range ] in
match s with
| Multi es ->
let goal, steps =
List.fold_left collect_term_to_unfold (None, Smap.empty) es in
let goal = Option.map (unfolds_from_list unfoldings) goal in
let steps = unfolds_from_smap unfoldings steps in
Applicable (process @@ fold_selection goal steps)
| s ->
let at = Tactical.at s in
let e = Tactical.selected s in
match F.repr e with
| Qed.Logic.Fun(f,es) ->
applicable ?at e f es unfoldings
| _ -> Not_applicable
end
let tactical = Tactical.export (new unfold)
let strategy = Strategy.make tactical ~arguments:[]