package orthologic-coq

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file ce_api.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
open Ol
open Constr
open Pp
open Typing
open Util


module PV = Proofview


let counter_bug = ref 0
let checkfail () = 
  incr counter_bug;
  if !counter_bug > 30 then failwith "bug"



(* Coq helpers *)
let decomp_term sigma (c : Constr.t) =
  Constr.kind (EConstr.Unsafe.to_constr (Termops.strip_outer_cast sigma (EConstr.of_constr c)))

let lib_constr n = lazy (UnivGen.constr_of_monomorphic_global (Global.env ()) @@ Coqlib.lib_ref n)

let find_reference = Coqlib.find_reference [@ocaml.warning "-3"]

let msg_in_tactic str : unit PV.tactic =
  PV.tclLIFT (PV.NonLogical.make (fun () ->
      Feedback.msg_warning (Pp.str str)))

let (===) = Constr.equal



(* Hash table with coq terms as keys *)
module Constrhash = Hashtbl.Make
  (struct type t = constr
          let equal = eq_constr_nounivs
          let hash = Constr.hash
   end)



(* Reference Theorems *)

let eq = lib_constr "core.eq.type"
let bool_typ    = lib_constr "core.bool.type"
let trueb  = lib_constr "core.bool.true"
let falseb = lib_constr "core.bool.false"
let andb   = lib_constr "core.bool.andb"
let orb    = lib_constr "core.bool.orb"
let xorb   = lib_constr "core.bool.xorb"
let negb   = lib_constr "core.bool.negb"
let eqb   = lib_constr "core.bool.eqb"



let tpair = lib_constr "olplugin.tpair"
let left_true_or = lib_constr "olplugin.left_true_or"
let right_true_or = lib_constr "olplugin.right_true_or"
let left_neg_or = lib_constr "olplugin.left_neg_or"
let right_neg_or = lib_constr "olplugin.right_neg_or"
let left_and_or = lib_constr "olplugin.left_and_or"
let right_and_or = lib_constr "olplugin.right_and_or"
let left_or_or_1 = lib_constr "olplugin.left_or_or_1"
let left_or_or_2 = lib_constr "olplugin.left_or_or_2"
let right_or_or_1 = lib_constr "olplugin.right_or_or_1"
let right_or_or_2 = lib_constr "olplugin.right_or_or_2"
let contract_or_1 = lib_constr "olplugin.contract_or_1"
let contract_or_2 = lib_constr "olplugin.contract_or_2"

let tpair_to_eq = lib_constr "olplugin.tpair_to_eq"



(* Reification in ol.ml formulas*)


let revmap_ol_formula = Constrhash.create 50
let map_var_types: (int, types) Hashtbl.t = Hashtbl.create 50
let counter = ref 0

let quote (env : Environ.env) sigma (e : Constr.t)  =

  let trueb = Lazy.force trueb in
  let falseb = Lazy.force falseb in
  let andb = Lazy.force andb in
  let orb = Lazy.force orb in
  let xorb = Lazy.force xorb in
  let negb = Lazy.force negb in

  let rec aux e = match decomp_term sigma e with
  | App (head, args) ->
    if head === andb && Array.length args = 2 then
      let a0 = aux args.(0) in
      let a1 = aux args.(1) in
      new_and [a0; a1]
    else if head === orb && Array.length args = 2 then
      let a0 = aux args.(0) in
      let a1 = aux args.(1) in
      new_or [a0; a1]
    else if head === xorb && Array.length args = 2 then
      let a0 = aux args.(0) in
      let a1 = aux args.(1) in
      new_or [new_and [a0; new_neg a1]; new_and [new_neg a0; a1]]
    else if head === negb && Array.length args = 1 then
      new_neg (aux args.(0))
    else (
      match Constrhash.find_opt revmap_ol_formula e with
      | Some v -> v
      | None ->
        (incr counter;
        let v = new_variable (!counter) in
        Constrhash.add revmap_ol_formula e v;
        Hashtbl.add map_var_types !counter e;
        v)
    )
  | _ ->
    if e === falseb then new_literal false
    else if e === trueb then new_literal true
    else (
      match Constrhash.find_opt revmap_ol_formula e with
      | Some v -> v
      | None ->
        (incr counter;
        let v = new_variable (!counter) in
        Constrhash.add revmap_ol_formula e v;
        Hashtbl.add map_var_types !counter e;
        v)
    )
  in
  aux e


let unquote (f: formula) : Constr.t =
  let trueb = Lazy.force trueb in
  let falseb = Lazy.force falseb in
  let andb = Lazy.force andb in
  let orb = Lazy.force orb in
  let negb = Lazy.force negb in

  let rec aux f = match f with
  | Variable r -> Hashtbl.find map_var_types r.id
  | Neg r -> Constr.mkApp (negb, [|aux r.child|])
  | Or r -> (match r.children with
    | [] -> falseb
    | head :: tail -> List.fold_left (fun acc x -> 
      Constr.mkApp (orb, [|acc; aux x|])) (aux head) tail
    )
  | And r -> (match r.children with
    | [] -> trueb
    | head :: tail -> List.fold_left (fun acc x -> 
      Constr.mkApp (andb, [|acc; aux x|])) (aux head) tail
    )
  | Literal r -> if r.b then trueb else falseb
  in
  aux f




(* Formulas used for the proof-producing certification algorithm *)
type cert_formula = 
| CertVariable of { polarity : bool; id : int;
      unique_key : int; lt_cache : (int * bool * bool, types option) Hashtbl.t; coqterm : types }
| CertNeg of { child: cert_formula; 
      unique_key : int; lt_cache : ((int * bool * bool), types option) Hashtbl.t; coqterm : types }
| CertOr of { c1: cert_formula; c2:cert_formula; 
      unique_key : int; lt_cache : ((int * bool * bool), types option) Hashtbl.t; coqterm : types }
| CertAnd of { c1: cert_formula; c2:cert_formula; 
      unique_key : int; lt_cache : ((int * bool * bool), types option) Hashtbl.t; coqterm : types }
| CertLiteral of { b : bool; 
      unique_key : int; lt_cache : ((int * bool * bool), types option) Hashtbl.t; coqterm : types }

let tot_cert = ref 0
let tot_id = ref 0


(* This is of course better, but it's a little cheating for the benchmarks...*)
  (*let revmap : (cert_formula Constrhash.t) = Constrhash.create 50*)

(* helpers for cert_formula *)
let new_formulas (env : Environ.env) sigma (e:(types array)) : cert_formula array =
  let trueb = Lazy.force trueb in
  let falseb = Lazy.force falseb in
  let andb = Lazy.force andb in
  let orb = Lazy.force orb in
  let negb = Lazy.force negb in

  let revmap : (cert_formula Constrhash.t) = Constrhash.create 50 in

  let rec aux e =
    incr tot_cert;
    let key = !tot_cert in
    match decomp_term sigma e with
    | App (head, args) when head === andb && Array.length args = 2 ->
      CertAnd {c1 = aux args.(0); c2 = aux args.(1); 
        unique_key = key; lt_cache = Hashtbl.create 50; coqterm = e}
    | App (head, args) when head === orb && Array.length args = 2 ->
      CertOr {c1 = aux args.(0); c2 = aux args.(1); 
        unique_key = key; lt_cache = Hashtbl.create 50; coqterm = e}
    | _ when e === trueb -> CertLiteral {b = true; 
        unique_key = key; lt_cache = Hashtbl.create 50; coqterm = e}
    | _ when e === falseb -> CertLiteral {b = false; 
        unique_key = key; lt_cache = Hashtbl.create 50; coqterm = e}
    | App (head, args) when head === negb && Array.length args = 1 ->
      CertNeg {child = aux args.(0); 
        unique_key = key; lt_cache = Hashtbl.create 50; coqterm = e}
    | _ -> match Constrhash.find_opt revmap e with
      | Some v -> v
      | None ->
        (incr tot_id;
        let id = !tot_id in
        let v = CertVariable {polarity = true; id = id; 
          unique_key = key; lt_cache = Hashtbl.create 50; coqterm = e} in
        Constrhash.add revmap e v;
        v)

  in Array.map aux e

let get_cert_key (f: cert_formula) : int =
  match f with
  | CertVariable {unique_key = k; _} -> k
  | CertNeg {unique_key = k; _} -> k
  | CertOr {unique_key = k; _} -> k
  | CertAnd {unique_key = k; _} -> k
  | CertLiteral {unique_key = k; _} -> k

let get_coq_term (f: cert_formula) : types =
  match f with
  | CertVariable {coqterm = c; _} -> c
  | CertNeg {coqterm = c; _} -> c
  | CertOr {coqterm = c; _} -> c
  | CertAnd {coqterm = c; _} -> c
  | CertLiteral {coqterm = c; _} -> c


let get_lt_cache_cert cf =
  match cf with
  | CertVariable {lt_cache = c; _} -> c
  | CertNeg {lt_cache = c; _} -> c
  | CertOr {lt_cache = c; _} -> c
  | CertAnd {lt_cache = c; _} -> c
  | CertLiteral {lt_cache = c; _} -> c

let lt_cached_cert cf1 cf2 b1 b2=
  Hashtbl.find_opt (get_lt_cache_cert cf1) (get_cert_key cf2, b1, b2)


let set_lt_cached_cert cf1 cf2 b1 b2 res =
  Hashtbl.add (get_lt_cache_cert cf1) (get_cert_key cf2, b1, b2) res


let cert_formula_to_string (f: cert_formula) : string =
  let rec aux f = match f with
  | CertVariable {id = i; _} -> Printf.sprintf "Var(%d)" i
  | CertNeg {child = c; _} -> Printf.sprintf "Neg(%s)" (aux c)
  | CertOr {c1 = c1; c2 = c2; _} -> Printf.sprintf "Or(%s, %s)" (aux c1) (aux c2)
  | CertAnd {c1 = c1; c2 = c2; _} -> Printf.sprintf "And(%s, %s)" (aux c1) (aux c2)
  | CertLiteral {b = b; _} -> Printf.sprintf "Lit(%b)" b
  in aux f



(* Returns the first option if it is defined, the second otherwise *)
let first_some opt1 opt2 =
  match opt1 with
  | Some _ -> opt1
  | None -> Lazy.force opt2 

(* Produces a proof that f1 = f2, using the laws of orthologic.
   If no such proof is found, return None. *)
let proof_ol (f1: cert_formula) (f2: cert_formula) : types option =
  let left_true_or = Lazy.force left_true_or in
  let right_true_or = Lazy.force right_true_or in
  let left_neg_or = Lazy.force left_neg_or in
  let right_neg_or = Lazy.force right_neg_or in
  let left_and_or = Lazy.force left_and_or in
  let right_and_or = Lazy.force right_and_or in
  let left_or_or_1 = Lazy.force left_or_or_1 in
  let left_or_or_2 = Lazy.force left_or_or_2 in
  let right_or_or_1 = Lazy.force right_or_or_1 in
  let right_or_or_2 = Lazy.force right_or_or_2 in
  let contract_or_1 = Lazy.force contract_or_1 in
  let contract_or_2 = Lazy.force contract_or_2 in

  let rec aux f1 f2 (b1: bool) (b2: bool): types option = 
    match lt_cached_cert f1 f2 b1 b2 with
    | Some res -> res
    | None ->
      let gct = get_coq_term in
      let res = match f1, f2 with
      | CertLiteral a, _ when a.b -> Some (Constr.mkApp(left_true_or, [|gct f2|]))
      | _, CertLiteral a when a.b -> Some (Constr.mkApp(right_true_or, [|gct f1|]))
      | CertNeg {child = CertVariable a; _}, CertVariable b when a.id = b.id -> 
        Some (Constr.mkApp(left_neg_or, [|gct f2|]))
      | CertVariable a, CertNeg {child = CertVariable b; _} when a.id = b.id -> 
        Some (Constr.mkApp(right_neg_or, [|gct f1|]))
      | CertAnd a, _ -> 
        let r1 = aux a.c1 f2 false b2 in
        (match r1 with
        | Some x -> 
          let r2 = aux a.c2 f2 false b2 in
          (match r2 with
          | Some y -> Some (Constr.mkApp(left_and_or, [|gct a.c1; gct a.c2; gct f2; x; y|]))
          | _ -> None)  
        | _ -> None)
      | _, CertAnd a ->
        let r1 = aux f1 a.c1 b1 false in 
        (match r1 with
        | Some x -> let r2 = aux f1 a.c2 b1 false in
          (match r2 with
          | Some y -> Some (Constr.mkApp(right_and_or, [|gct f1; gct a.c1; gct a.c2; x; y|]))
          | _ -> None)
        | _ -> None)
      | _ -> (first_some (first_some (first_some
        (match f1 with | CertOr a -> 
          first_some (Option.map (fun x -> Constr.mkApp(left_or_or_1, [|gct a.c1; gct a.c2; gct f2; x|])) (aux a.c1 f2 false b2))
                      (lazy (Option.map (fun x -> Constr.mkApp(left_or_or_2, [|gct a.c1; gct a.c2; gct f2; x|])) (aux a.c2 f2 false b2)))
        | _ -> None)
        
        (lazy (match f2 with | CertOr b -> 
          first_some (Option.map (fun x -> Constr.mkApp(right_or_or_1, [|gct f1; gct b.c1; gct b.c2; x|])) (aux f1 b.c1 b1 false))
                      (lazy (Option.map (fun x -> Constr.mkApp(right_or_or_2, [|gct f1; gct b.c1; gct b.c2; x|])) (aux f1 b.c2 b1 false)))
        | _ -> None))
      )
        (lazy (if b1 then None else (Option.map (fun x -> Constr.mkApp(contract_or_1, [|gct f1; gct f2; x|])) (aux f1 f1 true true))))
      )
        (lazy (if b2 then None else (Option.map (fun x -> Constr.mkApp(contract_or_2, [|gct f1; gct f2; x|])) (aux f2 f2 true true))))
      )
      in
      set_lt_cached_cert f1 f2 b1 b2 res;
      res
  in 
  aux f1 f2 false false

(* Find the atom in a formula (i.e. variable) that appears most often *)
let best_atom (l: cert_formula list) : cert_formula option =

  let counts = Hashtbl.create 50 in
  let rec aux f = 
    match f with
    | CertVariable a -> 
      let count = Hashtbl.find_opt counts a.id in
      (match count with
      | Some (c, e) -> Hashtbl.replace counts a.id ((c + 1), e)
      | None -> Hashtbl.add counts a.id (1, f))
    | CertNeg a -> aux a.child
    | CertOr a -> aux a.c1; aux a.c2
    | CertAnd a -> aux a.c1; aux a.c2
    | CertLiteral _ -> ()
    in

  List.iter aux l;
  let max_key = ref None in
  let max_value = ref 0 in
  Hashtbl.iter (fun key (c, e) ->
    if c > !max_value then begin
      max_value := c;
      max_key := Some e
    end
  ) counts;
  !max_key


(* Tactic: Introduces the normal form of a t as h, without proving equality*)

let ol_normal (t: Evd.econstr) (h: Names.Id.t): unit PV.tactic =
  let bool = Lazy.force bool_typ in

  Proofview.Goal.enter begin fun gl ->
    let env = Tacmach.pf_env gl in
    let sigma = Tacmach.project gl in
    let t2 = EConstr.Unsafe.to_constr t in
    let typ = type_of env sigma t in
    let typ_c = EConstr.Unsafe.to_constr (snd typ) in
    if typ_c === bool then
      let formula = quote env sigma t2 in
      let normal_form = reduced_form formula in
      let res = unquote normal_form in
      let res  = EConstr.of_constr res in
    Tacticals.tclTHENLIST [
            Tactics.pose_tac (Names.Name.mk_name h) res;
          ]
    else
      let typ_str = Pp.string_of_ppcmds (Printer.pr_constr_env env sigma typ_c) in
      let msg = str "Not a boolean expression: " ++ str typ_str in
      Tacticals.tclFAIL msg
  end





(* Tactic: introduce as "h" a proof of t, where t is a tpair of between boolean terms. *)
let ol_cert_tactic (t: Evd.econstr) (h: Names.Id.t): unit PV.tactic =
  let tpair = Lazy.force tpair in

  Proofview.Goal.enter begin fun gl ->
    let env = Tacmach.pf_env gl in
    let sigma = Tacmach.project gl in
    let t2 = EConstr.Unsafe.to_constr t in
    match decomp_term sigma t2 with
    | App (head, args) when head === tpair && Array.length args = 2 ->
      let new_forms = new_formulas env sigma args in
      let f1 = new_forms.(0) in
      let f2 = new_forms.(1) in
      let res = proof_ol f1 f2 in
      (match res with
      | Some res ->
        let res = EConstr.of_constr res in
        Tacticals.tclTHENLIST [
                Tactics.pose_tac (Names.Name.mk_name h) res;
              ]
      | None ->
        let msg = str "No proof found" in
        Tacticals.tclFAIL msg)
    | App (head, args) when head === tpair -> 
      let msg = str (Printf.sprintf "Not a pair of boolean expressions. Is tpair but args size: %d" (Array.length args)) in
      Tacticals.tclFAIL msg

    | _ ->
      let typ_str = Pp.string_of_ppcmds (Printer.pr_constr_env env sigma t2) in
      let msg = str "Goal is not a tpair: " ++ str typ_str in
      Tacticals.tclFAIL msg
  end

(* Tactic: Prove the current goal, which has to be of the form `tpair a b` or ` a = b` with a and b of type bool, using orthologic rules. *)
let ol_cert_goal_tactic cl : unit PV.tactic =
  let tpair = Lazy.force tpair in
  let eq = Lazy.force eq in

  let aux () =
    Proofview.Goal.enter begin fun gl ->
      let concl = Proofview.Goal.concl gl in
      let env = Tacmach.pf_env gl in
      let sigma = Tacmach.project gl in
      let concl2 = EConstr.Unsafe.to_constr concl in
      match decomp_term sigma concl2 with
      | App (head, args) when head === tpair && Array.length args = 2 ->
        let new_forms = new_formulas env sigma args in
        let f1 = new_forms.(0) in
        let f2 = new_forms.(1) in
        let res = proof_ol f1 f2 in
        (match res with
        | Some res ->
          let res = EConstr.of_constr res in
          Tacticals.tclTHENLIST [
            Tactics.exact_check res;
          ]
        | None ->
          let f1_s = Pp.string_of_ppcmds (Printer.pr_constr_env env sigma args.(0)) in
          let f2_s = Pp.string_of_ppcmds (Printer.pr_constr_env env sigma args.(1)) in
          let msg = str (Printf.sprintf "No proof of tpair %s %s found" f1_s f2_s) in
          Tacticals.tclFAIL msg)
      | _ ->
        let typ_str = Pp.string_of_ppcmds (Printer.pr_constr_env env sigma concl2) in
        let msg = str "Goal is not a tpair nor an equality: " ++ str typ_str in
        Tacticals.tclFAIL msg
      end
    in
    Proofview.Goal.enter begin fun gl ->
      let concl = Proofview.Goal.concl gl in
      let env = Tacmach.pf_env gl in
      let sigma = Tacmach.project gl in
      let concl2 = EConstr.Unsafe.to_constr concl in
      match decomp_term sigma concl2 with
      | App (head, args) when head === tpair && Array.length args = 2 ->
        Tacticals.tclTHENLIST [
          Autorewrite.auto_multi_rewrite ["nnf_lemmas"] cl;
          aux ()
        ]
      | App (head, args) when head === eq ->
          Tacticals.tclTHENFIRSTn
            (Tactics.apply (EConstr.of_constr (Lazy.force tpair_to_eq)))
            [|
              (Tacticals.tclTHEN
              (Autorewrite.auto_multi_rewrite ["nnf_lemmas"] cl)
              (aux ()));

              (Tacticals.tclTHEN
              (Autorewrite.auto_multi_rewrite ["nnf_lemmas"] cl)
              (aux ()))
            |]

      | App (head, args) when head === tpair -> 
        let msg = str (Printf.sprintf "Not a pair of boolean expressions. Is tpair but args size: %d" (Array.length args)) in
        Tacticals.tclFAIL msg

      | _ ->
        let typ_str = Pp.string_of_ppcmds (Printer.pr_constr_env env sigma concl2) in
        let msg = str "Goal is not a tpair nor an equality: " ++ str typ_str in
        Tacticals.tclFAIL msg
  end


(* Tactic: Prove the current goal, which has to be of the form `tpair a b` or ` a = b` with a and b of type bool, using orthologic rules. *)

(* Tactic: Destruct the boolean atom in a boolean term that appears most often *)
let destruct_atom () : unit PV.tactic =
  

  Proofview.Goal.enter begin fun gl ->
    let eq = Lazy.force eq in
    let bool_typ = Lazy.force bool_typ in
    let env = Tacmach.pf_env gl in
    let sigma = Tacmach.project gl in
    let concl = Proofview.Goal.concl gl in
    let concl = EConstr.Unsafe.to_constr concl in
    let t = decomp_term sigma concl in
    match t with
    | App (head, args) when head === eq && Array.length args = 3 && args.(0) === bool_typ ->
      let new_forms = new_formulas env sigma [|args.(1); args.(2)|] in
      let tl = new_forms.(0) in
      let tr = new_forms.(1) in
      let res = best_atom [tl; tr] in
      
      (match res with
      | Some res ->
        let res = get_coq_term res in
        let res = EConstr.of_constr res in
        Tactics.destruct false None res None None;
      | None ->
        let msg = str "No atom found" in
        Tacticals.tclFAIL msg)
    | _ ->
      let msg = str "Not an equality of boolean expressions" in
      Tacticals.tclFAIL msg
  end


(* Tactic: For every maximal boolean subterm `a` of `t`,
   compute `a'` the OL-normal form of `a`, prove `a=a'`, and substitute `ta`by `a'` in the goal. *)
let ol_norm_cert_tactic (t: Evd.econstr) cl : unit PV.tactic =
  let andb = Lazy.force andb in
  let orb = Lazy.force orb in
  let xorb = Lazy.force xorb in
  let negb = Lazy.force negb in
  let eqb = Lazy.force eqb in

  Proofview.Goal.enter begin fun gl ->
    let env = Tacmach.pf_env gl in
    let sigma = Tacmach.project gl in
    let rec aux t: types list =
      match decomp_term sigma t with
      | App (head, args) when 
        head === andb && Array.length args = 2 ||
        head === orb && Array.length args = 2 ||
        head === xorb && Array.length args = 2 ||
        head === negb && Array.length args = 1 ||
        head === eqb && Array.length args = 2 ->
        [t]
      | App (head, args) ->
        let h: types list = aux head in
        let rest: types list = List.flatten (List.map aux (Array.to_list args)) in
        h @ rest
      | _ -> 
        []
      in
    let t2 = EConstr.Unsafe.to_constr t in
    let l = aux t2 in
    let l = List.map (fun t2 -> 
      let f = quote env sigma t2 in
      (t2, f, size f)
      ) l in
    let l = List.sort (fun (_, _, i1) -> fun (_, _, i2) -> Int.compare i2 i1) l in
    Tacticals.tclTHENLIST
      (List.map (fun (t, f, _) ->
        let normal_form = reduced_form f in

        let res = unquote normal_form in
        let tac = ol_cert_goal_tactic cl in
        let res2 = EConstr.of_constr res in
        let t2 = EConstr.of_constr t in
        Equality.replace_by t2 res2 tac
      ) l)
    
  end




(* Tactic: For every maximal boolean subterm `a` of the goal,
   compute `a'` the OL-normal form of `a`, prove `a=a'`, and substitute `ta`by `a'` in the goal. *)
let ol_norm_cert_goal_tactic cl : unit PV.tactic =
  Proofview.Goal.enter begin fun gl ->
    let concl = Proofview.Goal.concl gl in
    ol_norm_cert_tactic concl cl
  end


  
(* Tactic: oltauto with certification*)

let oltauto_cert cl : unit PV.tactic =

  let eq = Lazy.force eq in
  let bool_typ = Lazy.force bool_typ in
  let rec aux () : unit PV.tactic = Proofview.Goal.enter begin fun gl ->
    let env = Tacmach.pf_env gl in
    let sigma = Tacmach.project gl in
    let concl = Proofview.Goal.concl gl in
    let concl = EConstr.Unsafe.to_constr concl in
    match decomp_term sigma concl with
    | App (head, args) when head === eq && Array.length args = 3 && args.(0) === bool_typ ->
      let f1 = args.(1) in
      let f2 = args.(2) in
      let f1_q = quote env sigma f1 in
      let f2_q = quote env sigma f2 in
      let f1_q_n = polar_to_normal_form (polarize f1_q true) in
      let f2_q_n = polar_to_normal_form (polarize f2_q true) in
      let f1_q_r = to_formula f1_q_n in
      let f2_q_r = to_formula f2_q_n in
      if (lattices_leq f1_q_n f2_q_n) && (lattices_leq f2_q_n f1_q_n) then
        ol_cert_goal_tactic cl
      else (
        let f1_n = unquote f1_q_r in
        let f2_n = unquote f2_q_r in
        let f1_n_e = EConstr.of_constr f1_n in
        let f2_n_e = EConstr.of_constr f2_n in
        let tac = 
          (Proofview.tclIFCATCH
              (ol_cert_goal_tactic cl) 
              (fun () -> aux ())
              (fun er -> (
                (Autorewrite.auto_multi_rewrite ["nnf_lemmas"] cl;)
              ))
            ) in
        let t1, t2 = 
          let f1_e = EConstr.of_constr f1 in
          let f2_e = EConstr.of_constr f2 in
          if (size f1_q) > (size f2_q) then 
            (Equality.replace_by f1_e f1_n_e tac, Equality.replace_by f2_e f2_n_e tac)
          else 
            (Equality.replace_by f2_e f2_n_e tac, Equality.replace_by f1_e f1_n_e tac)
          in

        Tacticals.tclTHENLIST [
          t1; 
          t2;
          (Proofview.tclIFCATCH
            (destruct_atom ())
            (fun () -> aux ())
            (fun er -> (Proofview.tclUNIT ()))
          )
        ]
      )
    | _ ->
      let typ_str = Pp.string_of_ppcmds (Printer.pr_constr_env env sigma concl) in
      let msg = str "Goal must be an equality between boolean terms: " ++ str typ_str in
      Tacticals.tclFAIL msg
    end in
  aux ()

(* Tactic: oltauto with certification*)
OCaml

Innovation. Community. Security.