Source file Tactical.ml
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
open Conditions
open Lang.F
module Tmap = Map.Make(String)
type computer = term list -> term
class type composer =
object
method id : string
method group : string
method title : string
method descr : string
method arity : int
method filter : term list -> bool
method compute : term list -> term
end
let computers : computer Tmap.t ref = ref Tmap.empty
let composers : composer Tmap.t ref = ref Tmap.empty
let groups = ref []
let rec insert_group cc = function
| [] -> [cc#group , [cc]]
| (( gid , ccs ) as group ):: others ->
if cc#group = gid then
( gid , ccs @ [cc] ) :: others
else
group :: insert_group cc others
let add_computer id cc =
if not (Tmap.mem id !computers) then
computers := Tmap.add id cc !computers
let add_composer (c : #composer) =
let id = c#id in
if Tmap.mem id !composers || Tmap.mem id !computers then
Wp_parameters.error "Composer #%s already registered (skipped)" id
else
begin
computers := Tmap.add id (c#compute :> computer) !computers ;
composers := Tmap.add id (c :> composer) !composers ;
groups := insert_group (c :> composer) !groups ;
end
let iter_composer f =
List.iter (fun (_,ccs) -> List.iter f ccs) !groups
type clause = Goal of pred | Step of step
type process = sequent -> (string * sequent) list
type status =
| Not_applicable
| Not_configured
| Applicable of process
type selection =
| Empty
| Clause of clause
| Inside of clause * term
| Compose of compose
| Multi of selection list
and compose =
| Cint of Integer.t
| Range of int * int
| Code of term * string * selection list
let head = function
| Goal p -> p
| Step s -> Conditions.head s
let is_empty = function Empty -> true | _ -> false
let eq_clause a b =
a == b ||
match a, b with
| Goal p, Goal q -> Lang.F.eqp p q
| Step u, Step v -> u == v
| Goal _, Step _ | Step _, Goal _ -> false
let eq_compose a b =
a == b ||
match a,b with
| Cint n, Cint m -> Integer.equal n m
| Range(a,b) , Range(p,q) -> a = b && p = q
| Code(ta,_,_), Code(tb,_,_) -> Lang.F.equal ta tb
| (Cint _ | Range _ | Code _), (Cint _ | Range _ | Code _) -> false
let rec equal a b =
a == b ||
match a,b with
| Empty,Empty -> true
| Clause ca, Clause cb -> eq_clause ca cb
| Inside(ca,ta), Inside(cb,tb) -> eq_clause ca cb && Lang.F.equal ta tb
| Compose ca, Compose cb -> eq_compose ca cb
| Multi sa , Multi sb -> Qed.Hcons.equal_list equal sa sb
| (Empty | Clause _ | Inside _ | Compose _ | Multi _) ,
(Empty | Clause _ | Inside _ | Compose _ | Multi _)
-> false
let composed = function
| Cint a -> e_zint a
| Range(a,_) -> e_int a
| Code(v,_id,_es) -> v
let selected = function
| Empty -> e_true
| Inside(_,t) -> t
| Clause c -> e_prop (head c)
| Compose code -> composed code
| Multi _s -> e_true
let get_int_z z =
try Some (Integer.to_int_exn z) with Z.Overflow -> None
let get_int = function
| Empty -> None
| Compose(Cint a) -> get_int_z a
| s ->
match Lang.F.repr (selected s) with
| Kint z -> get_int_z z
| _ -> None
let get_bool = function
| Empty -> None
| s ->
match Lang.F.repr (selected s) with
| True -> Some true
| False -> Some false
| _ -> None
let subclause clause p =
match clause with
| Step s ->
let hs = Conditions.have s in
hs == p ||
( match Lang.F.p_expr hs with
| Qed.Logic.And ps -> List.memq p ps
| _ -> false )
| Goal hs ->
hs == p ||
( match Lang.F.p_expr hs with
| Qed.Logic.Or ps -> List.memq p ps
| _ -> false )
let pp_clause fmt = function
| Goal _ -> Format.pp_print_string fmt "Goal"
| Step s -> Format.fprintf fmt "Hyp %d" s.id
let rec pp_selection fmt = function
| Empty -> Format.pp_print_string fmt "Empty"
| Inside(c,t) ->
Format.fprintf fmt "Term %d in %a" (Lang.F.QED.id t) pp_clause c
| Clause c -> pp_clause fmt c
| Compose(Cint k) ->
Format.fprintf fmt "Constant '%a'" Integer.pretty k
| Compose(Range(a,b)) ->
Format.fprintf fmt "Range '%d..%d'" a b
| Compose(Code(_,id,es)) ->
Format.fprintf fmt "@[<hov 2>Compose '%s'" id ;
List.iter (fun e -> Format.fprintf fmt "(%a)" pp_selection e) es ;
Format.fprintf fmt "@]"
| Multi es ->
Format.fprintf fmt "@[<hov 2>Multi-selection" ;
List.iter (fun e -> Format.fprintf fmt "(%a)" pp_selection e) es ;
Format.fprintf fmt "@]"
let int a = Compose(Cint (Integer.of_int a))
let cint a = Compose(Cint a)
let range a b = Compose(Range(a,b))
let compose id es =
try
let cc = Tmap.find id !computers in
let e = cc (List.map selected es) in
match Lang.F.repr e with
| Qed.Logic.Kint n -> cint n
| _ -> Compose(Code(e,id,es))
with Not_found -> Empty
let multi es = Multi es
let findhead (s:selection) e =
match s with
| Empty -> None
| Compose(Range _ | Cint _) -> None
| Inside(clause,_) | Clause clause ->
let p = Lang.F.e_prop (head clause) in
if Lang.F.is_subterm e p
then Some(Inside(clause,e))
else None
| Compose(Code(v,_,_)) as s ->
if v == e then Some s else None
| Multi _ -> None
let rec lookup (s:selection) e q =
match findhead s e with
| Some _ as result -> result
| None -> lookup_inner s e q
and lookup_inner (s:selection) e q =
begin match s with
| Compose(Code(_,_,ps)) ->
List.iter (fun p -> Queue.add p q) ps
| _ -> ()
end ;
if Queue.is_empty q then None else lookup (Queue.pop q) e q
and subterm (s:selection) e =
match Lang.F.repr e with
| Qed.Logic.Kint z -> Some (cint z)
| _ ->
match findhead s e with
| Some _ as result -> result
| None -> lookup_inner s e (Queue.create ())
let rec subterms s = function
| [] -> []
| e::es ->
let ps = subterms s es in
match subterm s e with
| None -> ps
| Some p -> p::ps
let destruct_value s =
let v = selected s in
let open Qed.Logic in
match Lang.F.repr v with
| Kint _ | Kreal _ | True | False | Bind _ | Fvar _ | Bvar _ | Apply _ -> []
| Add es | Mul es | And es | Or es | Fun(_,es) -> subterms s es
| Imply(hs,p) -> subterms s (hs @ [p])
| If(a,b,c) | Aset(a,b,c) -> subterms s [a;b;c]
| Not a | Rget(a,_) | Acst(_,a) -> subterms s [a]
| Rdef fvs -> subterms s (List.map snd fvs)
| Times(k,v) -> cint k :: subterms s [v]
| Div(a,b) | Mod(a,b) | Eq(a,b) | Neq(a,b) | Lt(a,b) | Leq(a,b) | Aget(a,b) ->
subterms s [a;b]
let destruct = function
| Empty | Compose(Cint _) -> []
| Compose(Range(a,b)) -> [int a;int b]
| s ->
let ps = destruct_value s in
if ps <> [] then ps else
match s with
| Compose(Code(_,_,ps)) -> ps
| _ -> []
type 'a named = { title : string ; descr : string ; vid : string ; value : 'a }
type 'a range = { vmin : 'a option ; vmax : 'a option ; vstep : 'a }
type 'a field = 'a named
type 'a browser = ('a named -> unit) -> selection -> unit
type 'a finder = string -> 'a named
let field ~id ~title ~descr ~default : 'a field =
if id = "" then raise (Invalid_argument "Tactical.field") ;
{ title ; descr ; vid = id ; value=default }
let ident fd = fd.vid
let signature fd = fd
let default fd = fd.value
module Fmap :
sig
type t
val create : unit -> t
val reset : t -> unit
val get : t -> 'a field -> 'a
val set : t -> 'a field -> 'a -> unit
end =
struct
type t = (string,Obj.t) Hashtbl.t
let create () = Hashtbl.create 8
let reset t = Hashtbl.clear t
let get m (fd : 'a field) : 'a =
try Obj.obj (Hashtbl.find m fd.vid) with Not_found -> fd.value
let set m (fd : 'a field) (v : 'a) = Hashtbl.add m fd.vid (Obj.repr v)
end
type parameter =
| Checkbox of bool field
| Spinner of int field * int range
| Composer of selection field * (Lang.F.term -> bool)
| Selector : 'a field * 'a named list * ('a -> 'a -> bool) -> parameter
| Search : 'a named option field * 'a browser * 'a finder -> parameter
let checkbox ~id ~title ~descr ?(default=false) () =
let fd = field ~id ~title ~descr ~default in
fd , Checkbox fd
let spinner ~id ~title ~descr ?default ?vmin ?vmax ?(vstep=1) () =
let () = match vmin , vmax with
| Some a , Some b ->
if a >= b then raise (Invalid_argument "Tactical.spinner")
| _ -> () in
let default = match default, vmin, vmax with
| Some v , _ , _ -> v
| None , None , None -> 0
| None , Some v , _ -> v
| None , None , Some v -> v in
let fd = field ~id ~title ~descr ~default in
fd , Spinner(fd,{vmin;vmax;vstep})
let selector ~id ~title ~descr ?default ~options ?(equal=(=)) () =
let default = match default,options with
| _ , [] -> raise (Invalid_argument "Tactical.selector(empty)")
| Some value , vs ->
if List.for_all (fun v -> equal v.value value) vs
then raise (Invalid_argument "Tactical.selector(default)") ;
value
| None , {value}::_ -> value in
let fd = field ~id ~title ~descr ~default in
fd , Selector(fd,options,equal)
let accept _ = true
let composer ~id ~title ~descr ?(default=Empty) ?(filter=accept) () =
let fd = field ~id ~title ~descr ~default in
fd , Composer(fd,filter)
let search ~id ~title ~descr ~browse ~find () =
let fd = field ~id ~title ~descr ~default:None in
fd , Search(fd,browse,find)
let pident = function
| Checkbox fd -> ident fd
| Spinner(fd,_) -> ident fd
| Composer(fd,_) -> ident fd
| Selector(fd,_,_) -> ident fd
| Search(fd,_,_) -> ident fd
type 'a formatter = ('a,Format.formatter,unit) format -> 'a
class type feedback =
object
method pool : pool
method interactive : bool
method get_title : string
method has_error : bool
method set_title : 'a. 'a formatter
method set_descr : 'a. 'a formatter
method set_error : 'a. 'a formatter
method update_field :
'a. ?enabled:bool -> ?title:string -> ?tooltip:string ->
?range:bool -> ?vmin:int -> ?vmax:int ->
?filter:(Lang.F.term -> bool) -> 'a field -> unit
end
let at = function
| Empty | Clause (Goal _) | Inside(Goal _,_) | Compose _ | Multi _ -> None
| Clause (Step s) | Inside(Step s,_) -> Some s.id
let mapi f cases =
let rec iter f i n = function
| [] -> []
| p::ps -> (f i n p) :: iter f (succ i) n ps
in iter f 1 (List.length cases) cases
let insert ?at cases sequent =
List.map
(fun (descr,p) ->
let step = Conditions.(step ~descr (When p)) in
descr , Conditions.insert ?at step sequent)
cases
let replace ~at cases sequent =
List.map
(fun (descr,cond) ->
let step = Conditions.(step ~descr cond) in
descr , Conditions.replace ~at step sequent)
cases
let replace_single ~at (descr,cond) sequent =
let step = Conditions.(step ~descr cond) in
descr , Conditions.replace ~at step sequent
let replace_step ~at conditions sequent =
let step =
let s = Conditions.step_at (fst sequent) at in
Conditions.step ?descr:s.descr ?stmt:s.stmt ~deps:s.deps ~warn:s.warn
in
let conditions = List.map step conditions in
[ "Split conj", Conditions.replace_by_step_list ~at conditions sequent ]
let split cases sequent =
let hyps = fst sequent in
List.map (fun (descr,p) -> descr,(hyps,p)) cases
let rewrite ?at patterns sequent =
List.map
(fun (descr,guard,src,tgt) ->
let sequent =
Conditions.subst
(fun e -> if e == src then tgt else raise Not_found)
sequent in
let sequent =
if Lang.F.eqp Lang.F.p_true guard then sequent else
let step = Conditions.(step ~descr (When guard)) in
Conditions.insert ?at step sequent
in descr , sequent
) patterns
let condition name guard process seq =
( name , (fst seq , guard) ) :: process seq
class type tactical =
object
method id : string
method title : string
method descr : string
method params : parameter list
method reset : unit
method get_field : 'a. 'a field -> 'a
method set_field : 'a. 'a field -> 'a -> unit
method select : feedback -> selection -> status
end
type t = tactical
class virtual make ~id ~title ~descr ~params =
object
val hmap = Fmap.create ()
method id : string = id
method title : string = title
method descr : string = descr
method params : parameter list = params
method reset = Fmap.reset hmap
method get_field : 'a. 'a field -> 'a = Fmap.get hmap
method set_field : 'a. 'a field -> 'a -> unit = Fmap.set hmap
method virtual select : feedback -> selection -> status
end
let tacticals = ref Tmap.empty
let register t =
let id = t#id in
if Tmap.mem id !tacticals then
Wp_parameters.error "Tactical #%s already registered (skipped)" id
else
tacticals := Tmap.add id (t :> t) !tacticals
let export t = register t ; (t :> t)
let iter f = Tmap.iter (fun _id t -> f t) !tacticals
let lookup ~id = Tmap.find id !tacticals
let lookup_param tactic ~id =
List.find (fun p -> pident p = id) tactic#params
open Lang
let () = add_computer "wp:true" (fun _ -> e_true)
let () = add_computer "wp:false" (fun _ -> e_false)
let () = add_computer "wp:list" Vlist.list
let () = add_computer "wp:concat" Vlist.concat
let () = add_computer "wp:repeat"
(function [a;n] -> Vlist.repeat a n | _ -> raise Not_found)
let () =
for i = 0 to 9 do
add_composer
(object
method id = Printf.sprintf "wp:%d" i
method group = "const:unit"
method title = string_of_int i
method descr = ""
method arity = 0
method filter = function _ -> true
method compute = function _ -> F.e_int i
end)
done
let () = add_composer
(object
method id = "wp:eq"
method group = "logic"
method title = "A == B"
method descr = ""
method arity = 2
method filter = function
| [a;b] ->
(try
let ta = F.typeof a in
let tb = F.typeof b in
F.Tau.equal ta tb
with Not_found -> false)
| _ -> false
method compute = function [a;b] -> F.e_eq a b | _ -> F.e_true
end)
let () = add_composer
(object
method id = "wp:neq"
method group = "logic"
method title = "A != B"
method descr = ""
method arity = 2
method filter = function
| [a;b] ->
(try
let ta = F.typeof a in
let tb = F.typeof b in
F.Tau.equal ta tb
with Not_found -> false)
| _ -> false
method compute = function [a;b] -> F.e_neq a b | _ -> F.e_false
end)
let () = add_composer
(object
method id = "wp:leq"
method group = "logic"
method title = "A <= B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_arith
method compute = function [a;b] -> F.e_leq a b | _ -> F.e_true
end)
let () = add_composer
(object
method id = "wp:lt"
method group = "logic"
method title = "A < B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_arith
method compute = function [a;b] -> F.e_lt a b | _ -> F.e_true
end)
let () = add_composer
(object
method id = "wp:range"
method group = "logic"
method title = "A <= B <= C"
method descr = ""
method arity = 3
method filter = List.for_all F.is_arith
method compute = function [a;b;c] -> F.e_and [F.e_leq a b;F.e_leq b c]
| _ -> F.e_true
end)
let () = add_composer
(object
method id = "wp:not"
method group = "logic"
method title = "not A"
method descr = ""
method arity = 1
method filter = List.for_all F.is_prop
method compute = function a::_ -> F.e_not a | _ -> F.e_false
end)
let () = add_composer
(object
method id = "wp:and"
method group = "logic"
method title = "A && B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_prop
method compute = F.e_and
end)
let () = add_composer
(object
method id = "wp:or"
method group = "logic"
method title = "A || B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_prop
method compute = F.e_or
end)
let () = add_composer
(object
method id = "wp:incr"
method group = "additive"
method title = "A+1"
method descr = ""
method arity = 1
method filter = List.for_all F.is_arith
method compute es = F.e_sum (F.e_int 1 :: es)
end)
let () = add_composer
(object
method id = "wp:decr"
method group = "additive"
method title = "A-1"
method descr = ""
method arity = 1
method filter = List.for_all F.is_arith
method compute es = F.e_sum (F.e_int (-1) :: es)
end)
let () = add_composer
(object
method id = "wp:add"
method group = "additive"
method title = "Add A+B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_arith
method compute = F.e_sum
end)
let () = add_composer
(object
method id = "wp:sub"
method group = "additive"
method title = "Sub A-B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_arith
method compute = function [a;b] -> F.e_sub a b | _ -> F.e_int 0
end)
let () =
add_composer
(object
method id = Printf.sprintf "wp:ten"
method group = "product"
method title = "A*10"
method descr = ""
method arity = 1
method filter = List.for_all F.is_arith
method compute = function [e] -> F.e_times (Integer.of_int 10) e
| _ -> F.e_int 0
end)
let () = add_composer
(object
method id = "wp:mul"
method group = "product"
method title = "Mul A*B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_arith
method compute = F.e_prod
end)
let () = add_composer
(object
method id = "wp:div"
method group = "product"
method title = "Div A/B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_arith
method compute = function [a;b] -> F.e_div a b | _ -> F.e_int 1
end)
let () = add_composer
(object
method id = "wp:mod"
method group = "product"
method title = "Mod A%B"
method descr = ""
method arity = 2
method filter = List.for_all F.is_int
method compute = function [a;b] -> F.e_mod a b | _ -> F.e_int 1
end)
let () = add_composer
(object
method id = "wp:get"
method group = "structure"
method title = "Get A[B]"
method descr = ""
method arity = 2
method filter = function
| [a;b] ->
begin
try
let ta = F.typeof a in
let tb = F.typeof b in
match ta with
| Qed.Logic.Array(tm,_) -> F.Tau.equal tm tb
| _ -> false
with Not_found -> false
end
| _ -> false
method compute = function [a;b] -> F.e_get a b | _ -> F.e_int 0
end)
let () = add_composer
(object
method id = "wp:set"
method group = "structure"
method title = "Set A[B <-C]"
method descr = ""
method arity = 3
method filter = function
| [a;b;c] ->
begin
try
let ta = F.typeof a in
let tb = F.typeof b in
let tc = F.typeof c in
match ta with
| Qed.Logic.Array(tm,tv) ->
F.Tau.equal tm tb &&
F.Tau.equal tv tc
| _ -> false
with Not_found -> false
end
| _ -> false
method compute = function [a;b] -> F.e_get a b | _ -> F.e_int 0
end)