-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathecScope.ml
More file actions
2779 lines (2331 loc) · 93.1 KB
/
ecScope.ml
File metadata and controls
2779 lines (2331 loc) · 93.1 KB
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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* -------------------------------------------------------------------- *)
open EcUtils
open EcMaps
open EcSymbols
open EcLocation
open EcPath
open EcParsetree
open EcAst
open EcTypes
open EcDecl
open EcModules
open EcFol
open EcTyping
open EcHiInductive
open EcBigInt.Notations
module Sid = EcIdent.Sid
module Mid = EcIdent.Mid
module MSym = EcSymbols.Msym
module BI = EcBigInt
(* -------------------------------------------------------------------- *)
exception HiScopeError of EcLocation.t option * string
let pp_hi_scope_error fmt exn =
match exn with
| HiScopeError (None, s) ->
Format.fprintf fmt "%s" s
| HiScopeError (Some loc, s) ->
Format.fprintf fmt "%s: %s" (EcLocation.tostring loc) s
| _ -> raise exn
let _ = EcPException.register pp_hi_scope_error
let hierror ?loc fmt =
let buf = Buffer.create 127 in
let bfmt = Format.formatter_of_buffer buf in
Format.kfprintf
(fun _ ->
Format.pp_print_flush bfmt ();
raise (HiScopeError (loc, Buffer.contents buf)))
bfmt fmt
(* -------------------------------------------------------------------- *)
exception ImportError of EcLocation.t option * symbol * exn
let is_import_error = function ImportError _ -> true | _ -> false
let pp_import_error fmt exn =
match exn with
| ImportError (None, name, e) ->
Format.fprintf fmt "In external theory %s [<unknown> location]:@\n%a"
name EcPException.exn_printer e
| ImportError (Some l, name, e) when is_import_error e ->
Format.fprintf fmt "In external theory %s [%s]:@\n%a"
name (EcLocation.tostring l)
EcPException.exn_printer e
| ImportError (Some l, name, e) ->
Format.fprintf fmt "In external theory %s [%s]:@\n@\n%a"
name (EcLocation.tostring l)
EcPException.exn_printer e
| _ -> raise exn
let _ = EcPException.register pp_import_error
(* -------------------------------------------------------------------- *)
exception TopError of EcLocation.t * exn
let rec toperror_of_exn_r ?gloc exn =
match exn with
| TyError (loc, _, _) -> Some (loc, exn)
| RcError (loc, _, _) -> Some (loc, exn)
| DtError (loc, _, _) -> Some (loc, exn)
| ParseError (loc, _) -> Some (loc, exn)
| EcHiPredicates.TransPredError (loc, _, _) -> Some (loc, exn)
| EcHiNotations .NotationError (loc, _, _) -> Some (loc, exn)
| EcLexer.LexicalError (loc, _) ->
Some (odfl (odfl _dummy gloc) loc, exn)
| EcCoreGoal.TcError { EcCoreGoal.tc_location = None } ->
Some (odfl _dummy gloc, exn)
| EcCoreGoal.TcError
{ EcCoreGoal.tc_location = Some { EcCoreGoal.plc_loc = loc } } ->
let gloc = if EcLocation.isdummy loc then gloc else Some loc in
Some (odfl _dummy gloc, exn)
| LocError (loc, e) -> begin
let gloc = if EcLocation.isdummy loc then gloc else Some loc in
match toperror_of_exn_r ?gloc e with
| None -> Some (loc, e)
| Some (loc, e) -> Some (loc, e)
end
| ImportError _ ->
Some (odfl _dummy gloc, exn)
| TopError (loc, e) ->
let gloc = if EcLocation.isdummy loc then gloc else Some loc in
toperror_of_exn_r ?gloc e
| EcSection.SectionError _ ->
Some (odfl _dummy gloc, exn)
| HiScopeError (loc, msg) ->
let gloc =
match loc with
| None -> gloc
| Some loc -> if EcLocation.isdummy loc then gloc else Some loc
in
Some (odfl _dummy gloc, HiScopeError (None, msg))
| Sys.Break ->
Some (odfl _dummy gloc, HiScopeError (None, "interrupted"))
| _ -> None
let toperror_of_exn ?gloc exn =
match toperror_of_exn_r ?gloc exn with
| Some (loc, exn) -> TopError (loc, exn)
| None -> exn
let pp_toperror fmt loc exn =
Format.fprintf fmt "%s: %a"
(EcLocation.tostring loc)
EcPException.exn_printer exn
let () =
let pp fmt exn =
match exn with
| TopError (loc, exn) -> pp_toperror fmt loc exn
| _ -> raise exn
in
EcPException.register pp
(* -------------------------------------------------------------------- *)
type goption = ..
module type IOptions = sig
type oid
type options
type action = { for_loading : goption -> goption; }
val register : ?action:action -> goption -> oid
val freeze : unit -> options
val get : options -> oid -> goption
val set : options -> oid -> goption -> options
val for_loading : options -> options
val for_subscope : options -> options
end
(* -------------------------------------------------------------------- *)
module GenOptions : IOptions = struct
type action = { for_loading : goption -> goption; }
type oid = EcUid.uid
type options = (action * goption) EcUid.Muid.t
let options : options ref =
ref EcUid.Muid.empty
let identity = { for_loading = (fun x -> x); }
let register ?(action = identity) goption =
let oid = EcUid.unique () in
options := EcUid.Muid.add oid (action, goption) !options; oid
let freeze () =
!options
let get (options : options) (oid : oid) =
snd (oget (EcUid.Muid.find_opt oid options))
let set (options : options) (oid : oid) (goption : goption) =
EcUid.Muid.change (fun k -> Some (fst (oget k), goption)) oid options
let for_loading options =
EcUid.Muid.map (fun (act, exn) -> act, act.for_loading exn) options
let for_subscope options =
options
end
(* -------------------------------------------------------------------- *)
module Check_mode = struct
type mode = [`Off | `On | `Forced]
type goption += Check of mode
let oid =
let for_loading = function
| Check `Off -> Check `Off
| Check `On -> Check `Off
| Check `Forced -> Check `Forced
| exn -> exn
in GenOptions.register ~action:({ GenOptions.for_loading }) (Check `On)
let check options =
match GenOptions.get options oid with
| Check `On -> true
| Check `Forced -> true
| Check `Off -> false
| _ -> true
let set_checkproof options b =
match GenOptions.get options oid with
| Check `On when not b -> GenOptions.set options oid (Check `Off)
| Check `Off when b -> GenOptions.set options oid (Check `On )
| _ -> options
let set_fullcheck options =
GenOptions.set options oid (Check `Forced)
end
(* -------------------------------------------------------------------- *)
module Prover_info = struct
type goption += PI of EcProvers.prover_infos
let oid = GenOptions.register (PI EcProvers.dft_prover_infos)
let set options pi =
GenOptions.set options oid (PI pi)
let get options =
match GenOptions.get options oid with
| PI pi -> pi
| _ -> assert false
end
(* -------------------------------------------------------------------- *)
module KnownFlags = struct
let implicits = "implicits"
let oldip = "oldip"
let redlogic = "redlogic"
let und_delta = "und_delta"
let flags = [
(implicits, false);
(oldip , false);
(redlogic , true );
(und_delta, false);
]
end
exception UnknownFlag of string
module Flags : sig
open GenOptions
val get : options -> string -> bool
val set : options -> string -> bool -> options
end = struct
type flags = bool Mstr.t
type goption += Flags of flags
let asflags = function Flags m -> m | _ -> assert false
let oid =
let default = Mstr.of_list KnownFlags.flags in
let for_loading = function
| Flags _ -> Flags default
| exn -> exn
in GenOptions.register ~action:{ GenOptions.for_loading } (Flags default)
let get options name =
let flags = asflags (GenOptions.get options oid) in
oget ~exn:(UnknownFlag name) (Mstr.find_opt name flags)
let set options name value =
let flags = asflags (GenOptions.get options oid) in
let flags =
Mstr.change (fun x ->
ignore (oget ~exn:(UnknownFlag name) x : bool);
Some value)
name flags in
GenOptions.set options oid (Flags flags)
end
(* -------------------------------------------------------------------- *)
type proof_uc = {
puc_active : (proof_auc * (proof_ctxt option)) option;
puc_cont : proof_ctxt list * (EcSection.scenv option);
puc_init : EcSection.scenv;
}
and proof_auc = {
puc_name : symbol option;
puc_started : bool;
puc_jdg : proof_state;
puc_flags : pucflags;
puc_crt : EcDecl.axiom;
}
and proof_ctxt =
(symbol option * EcDecl.axiom) * EcPath.path * EcSection.scenv
and proof_state =
PSNoCheck | PSCheck of EcCoreGoal.proof
and pucflags = {
puc_smt : bool;
puc_local : bool;
}
(* -------------------------------------------------------------------- *)
type required_info = {
rqd_name : symbol;
rqd_namespace : EcLoader.namespace option;
rqd_kind : EcLoader.kind;
rqd_digest : Digest.t;
rqd_direct : bool;
}
type required = required_info list
type prelude = {
pr_env : EcEnv.env;
pr_required : required;
}
type thloaded = EcEnv.Theory.compiled_theory
type scope = {
sc_name : (symbol * EcTheory.thmode);
sc_env : EcSection.scenv;
sc_top : scope option;
sc_prelude : ([`Frozen | `InPrelude] * prelude);
sc_loaded : (thloaded * required) Msym.t;
sc_required : required;
sc_clears : path list;
sc_pr_uc : proof_uc option;
sc_options : GenOptions.options;
sc_globdoc : string list;
sc_locdoc : docstate;
}
and docstate = {
docentities : docentity list;
subdocentbl : docentity list;
docstringbl : string list;
srcstringbl : string list;
currentname : string option;
currentkind : itemkind option;
currentmode : mode option;
currentproc : bool;
}
and docentity =
| ItemDoc of string list * docitem
| SubDoc of (string list * docitem) * docentity list
and docitem =
mode * itemkind * string * string list (* dec/reg, kind, name, src *)
and itemkind = [`Type | `Operator | `Axiom | `Lemma | `ModuleType | `Module | `Theory]
and mode = [`Abstract | `Specific]
(* -------------------------------------------------------------------- *)
let get_gdocstrings (sc : scope) : string list =
sc.sc_globdoc
let get_ldocentities (sc : scope) : docentity list =
sc.sc_locdoc.docentities
module DocState = struct
let empty : docstate =
{ docentities = [];
subdocentbl = [];
docstringbl = [];
srcstringbl = [];
currentname = None;
currentkind = None;
currentmode = None;
currentproc = false; }
let start_process (state : docstate) (name : string) (kind : itemkind) (md : mode): docstate =
{ state with
currentname = Some name;
currentkind = Some kind;
currentmode = Some md;
currentproc = true }
let prevent_process (state : docstate) : docstate =
{ state with
currentname = None;
currentkind = None;
currentmode = None;
currentproc = false }
let reinitialize_process (state : docstate) : docstate =
{ state with
docstringbl = [];
srcstringbl = [];
currentname = None;
currentkind = None;
currentmode = None;
currentproc = false }
let push_docbl (state : docstate) (docc : string) : docstate =
{ state with docstringbl = state.docstringbl @ [docc] }
let push_srcbl (state : docstate) (srcs : string) : docstate =
{ state with srcstringbl = state.srcstringbl @ [srcs] }
let add_entity (state : docstate) (docent : docentity) : docstate =
{ state with docentities = state.docentities @ [docent] }
let add_item (state : docstate) : docstate =
let state =
if state.currentproc
then
add_entity state (ItemDoc (state.docstringbl, (oget state.currentmode, oget state.currentkind, oget state.currentname, state.srcstringbl)))
else
state
in
reinitialize_process state
let add_sub (state : docstate) (substate : docstate) : docstate =
let state =
if state.currentproc
then
add_entity state (SubDoc ((state.docstringbl, (oget state.currentmode, oget state.currentkind, oget state.currentname, state.srcstringbl)),
(substate.docentities)))
else
state
in
reinitialize_process state
end
(* -------------------------------------------------------------------- *)
let empty (gstate : EcGState.gstate) =
let env = EcEnv.initial gstate in
{ sc_name = (EcPath.basename (EcEnv.root env), `Concrete);
sc_env = EcSection.initial env;
sc_top = None;
sc_prelude = (`InPrelude, { pr_env = env; pr_required = []; });
sc_loaded = Msym.empty;
sc_required = [];
sc_clears = [];
sc_pr_uc = None;
sc_options = GenOptions.freeze ();
sc_globdoc = [];
sc_locdoc = DocState.empty; }
(* -------------------------------------------------------------------- *)
let env (scope : scope) =
EcSection.env scope.sc_env
(* -------------------------------------------------------------------- *)
let gstate (scope : scope) =
EcEnv.gstate (env scope)
(* -------------------------------------------------------------------- *)
let name (scope : scope) =
scope.sc_name
(* -------------------------------------------------------------------- *)
let path (scope : scope) =
EcEnv.root (env scope)
(* -------------------------------------------------------------------- *)
let attop (scope : scope) =
scope.sc_top = None
(* -------------------------------------------------------------------- *)
let freeze (scope : scope) =
match scope.sc_prelude with
| `Frozen , _ -> assert false
| `InPrelude, pr -> { scope with sc_prelude = (`Frozen, pr) }
(* -------------------------------------------------------------------- *)
let goal (scope : scope) =
scope.sc_pr_uc |> obind (fun x -> omap fst x.puc_active)
(* -------------------------------------------------------------------- *)
let xgoal (scope : scope) =
scope.sc_pr_uc
(* -------------------------------------------------------------------- *)
let dump_why3 (scope : scope) (filename : string) =
try EcSmt.dump_why3 (env scope) filename
with
| Sys_error msg ->
hierror "cannot dump to `%s`: system error: %s"
filename msg
| Unix.Unix_error (e, _, _) ->
hierror "cannot dump to `%s`: system error: %s"
filename (Unix.error_message e)
(* -------------------------------------------------------------------- *)
type topmode = [`InProof | `InActiveProof | `InTop]
let check_state (mode : topmode) action (scope : scope) =
match mode with
| `InProof when scope.sc_pr_uc = None ->
hierror "cannot process [%s] outside a proof script" action
| `InActiveProof when scope.sc_pr_uc = None ->
hierror "cannot process [%s] outside a proof script" action
| `InTop when scope.sc_pr_uc <> None ->
hierror "cannot process [%s] inside a proof script" action
| _ -> ()
(* -------------------------------------------------------------------- *)
let notify (scope : scope) (lvl : EcGState.loglevel) =
EcEnv.notify (env scope) lvl
(* -------------------------------------------------------------------- *)
module Options = struct
let get scope name =
Flags.get scope.sc_options name
let set scope name value =
{ scope with sc_options =
Flags.set scope.sc_options name value }
let get_implicits scope =
get scope KnownFlags.implicits
let set_implicits scope value =
set scope KnownFlags.implicits value
let get_oldip scope =
get scope KnownFlags.oldip
let set_oldip scope value =
set scope KnownFlags.oldip value
let get_redlogic scope =
get scope KnownFlags.redlogic
let set_redlogic scope value =
set scope KnownFlags.redlogic value
let get_und_delta scope =
get scope KnownFlags.und_delta
let set_und_delta scope value =
set scope KnownFlags.und_delta value
end
(* -------------------------------------------------------------------- *)
let for_loading (scope : scope) =
let pr = snd (scope.sc_prelude) in
let env = EcEnv.copy pr.pr_env in
let lg = EcGState.loglevel (EcEnv.gstate env) in
EcGState.set_loglevel
(EcGState.max_loglevel `Warning lg)
(EcEnv.gstate env);
{ sc_name = (EcPath.basename (EcEnv.root pr.pr_env), `Concrete);
sc_env = EcSection.initial env;
sc_top = None;
sc_prelude = scope.sc_prelude;
sc_loaded = scope.sc_loaded;
sc_required = pr.pr_required;
sc_clears = [];
sc_pr_uc = None;
sc_options = GenOptions.for_loading scope.sc_options;
sc_globdoc = [];
sc_locdoc = DocState.empty; }
(* -------------------------------------------------------------------- *)
let subscope (scope : scope) (mode : EcTheory.thmode) (name : symbol) lc =
let env = EcSection.enter_theory name lc mode scope.sc_env in
{ sc_name = (name, mode);
sc_env = env;
sc_top = Some scope;
sc_prelude = scope.sc_prelude;
sc_loaded = scope.sc_loaded;
sc_required = scope.sc_required;
sc_clears = [];
sc_pr_uc = None;
sc_options = GenOptions.for_subscope scope.sc_options;
sc_globdoc = [];
sc_locdoc = DocState.empty;
}
(* -------------------------------------------------------------------- *)
module Prover = struct
let all_provers () =
List.map
(fun p -> p.EcProvers.pr_name)
(EcProvers.known ~evicted:false)
let check_prover_name { pl_desc = name; pl_loc = loc } =
if not (EcProvers.is_prover_known name) then
hierror ~loc "Unknown prover %s" name;
name
(* -------------------------------------------------------------------- *)
let process_dbhint env db =
let add hints x =
let nf kind p =
hierror
~loc:p.pl_loc "cannot find %s `%s'"
(match kind with `Lemma -> "lemma" | `Theory -> "theory")
(string_of_qsymbol (unloc p))
in
let addm hints hflag p =
match EcEnv.Theory.lookup_opt (unloc p) env with
| None -> nf `Theory p
| Some (p, _) -> EcProvers.Hints.addm p hflag hints
and add1 hints hflag p =
match EcEnv.Ax.lookup_opt (unloc p) env with
| None -> nf `Lemma p
| Some (p, _) -> EcProvers.Hints.add1 p hflag hints
in
match x.pht_kind with
| `Theory -> addm hints x.pht_flag x.pht_name
| `Lemma -> add1 hints x.pht_flag x.pht_name
in
let hints = EcProvers.Hints.empty in
let hints = List.fold_left add hints db in
hints
(* -------------------------------------------------------------------- *)
type smt_options = {
po_timeout : int option;
po_cpufactor : int option;
po_nprovers : int option;
po_provers : string list option * (include_exclude * string) list;
po_quorum : int option;
po_verbose : int option;
pl_all : bool option;
pl_max : int option;
pl_wanted : EcProvers.hints option;
pl_unwanted : EcProvers.hints option;
pl_dumpin : string located option;
pl_selected : bool option;
gn_debug : bool option;
}
(* -------------------------------------------------------------------- *)
let empty_options = {
po_timeout = None;
po_cpufactor = None;
po_nprovers = None;
po_provers = (None, []);
po_quorum = None;
po_verbose = None;
pl_all = None;
pl_max = None;
pl_wanted = None;
pl_unwanted = None;
pl_dumpin = None;
pl_selected = None;
gn_debug = None;
}
(* -------------------------------------------------------------------- *)
let process_prover_option env ppr =
let provers =
match ppr.pprov_names with
| None -> None, []
| Some pl ->
let do_uo uo s =
match s.pl_desc with
| "!" -> all_provers ()
| "" -> []
| _ ->
let x = check_prover_name s in
if List.exists ((=) x) uo then uo else x :: uo in
let uo =
if pl.pp_use_only = [] then None
else Some (List.fold_left do_uo [] pl.pp_use_only) in
let do_ar (k,s) = k, check_prover_name s in
uo, List.map do_ar pl.pp_add_rm in
let verbose = omap (odfl 1) ppr.pprov_verbose in
{
po_timeout = ppr.pprov_timeout;
po_cpufactor = ppr.pprov_cpufactor;
po_nprovers = ppr.pprov_max;
po_provers = provers;
po_quorum = ppr.pprov_quorum;
po_verbose = verbose;
pl_all = ppr.plem_all;
pl_max =
begin match ppr.plem_max, ppr.plem_wanted with
| Some i, _ -> Some (odfl max_int i)
| None , None -> None
| None , Some _ -> Some 0
end;
pl_wanted = omap (process_dbhint env) ppr.plem_wanted;
pl_unwanted = omap (process_dbhint env) ppr.plem_unwanted;
pl_dumpin = ppr.plem_dumpin;
pl_selected = ppr.plem_selected;
gn_debug = ppr.psmt_debug;
}
(* -------------------------------------------------------------------- *)
let mk_prover_info_from_dft (dft : EcProvers.prover_infos)
(options : smt_options) : EcProvers.prover_infos =
let open EcProvers in
let gn_debug = odfl dft.gn_debug options.gn_debug in
let pr_maxprocs = odfl dft.pr_maxprocs options.po_nprovers in
let pr_timelimit = max 0 (odfl dft.pr_timelimit options.po_timeout) in
let pr_cpufactor = max 0 (odfl dft.pr_cpufactor options.po_cpufactor) in
let pr_verbose = max 0 (odfl dft.pr_verbose options.po_verbose) in
let pr_all = odfl dft.pr_all options.pl_all in
let pr_max = odfl dft.pr_max options.pl_max in
let pr_wanted = odfl dft.pr_wanted options.pl_wanted in
let pr_unwanted = odfl dft.pr_unwanted options.pl_unwanted in
let pr_selected = odfl dft.pr_selected options.pl_selected in
let pr_quorum = max 1 (odfl dft.pr_quorum options.po_quorum) in
let pr_dumpin = options.pl_dumpin in
let pr_provers =
let l = odfl dft.pr_provers (fst options.po_provers) in
let do_ar l (k, p) =
match k with
| `Exclude -> List.remove_all l p
| `Include -> if List.exists ((=) p) l then l else p::l
in List.fold_left do_ar l (snd options.po_provers) in
{ pr_maxprocs; pr_provers ; pr_timelimit; pr_cpufactor;
pr_verbose ; pr_all ; pr_max ;
pr_wanted ; pr_unwanted; pr_selected ; pr_quorum ;
pr_dumpin ;
gn_debug ; }
(* -------------------------------------------------------------------- *)
let mk_prover_info scope (options : smt_options) =
let dft = Prover_info.get scope.sc_options in
mk_prover_info_from_dft dft options
(* -------------------------------------------------------------------- *)
let do_prover_info scope ?(default = empty_options) ppr =
let options = Option.map (process_prover_option (env scope)) ppr in
let options = Option.value ~default options in
mk_prover_info scope options
(* -------------------------------------------------------------------- *)
let pprover_infos_to_prover_infos
(env : EcEnv.env) (dft : EcProvers.prover_infos)
(ppr : pprover_infos) : EcProvers.prover_infos =
let options = process_prover_option env ppr in
mk_prover_info_from_dft dft options
(* -------------------------------------------------------------------- *)
let process scope ppr =
let pi = do_prover_info scope (Some ppr) in
{ scope with sc_options = Prover_info.set scope.sc_options pi }
(* -------------------------------------------------------------------- *)
let set_default scope options =
let provers = match fst options.po_provers with
| None ->
let provers = EcProvers.dft_prover_names in
List.filter EcProvers.is_prover_known provers
| Some l ->
List.iter
(fun name -> if not (EcProvers.is_prover_known name) then
hierror "unknown prover %s" name) l; l in
let options =
{ options with po_provers = (Some provers, snd options.po_provers) } in
let pi = mk_prover_info scope options in
{ scope with sc_options = Prover_info.set scope.sc_options pi }
(* -------------------------------------------------------------------- *)
let full_check scope =
{ scope with sc_options = Check_mode.set_fullcheck scope.sc_options }
(* -------------------------------------------------------------------- *)
let check_proof scope b =
{ scope with sc_options = Check_mode.set_checkproof scope.sc_options b }
end
(* -------------------------------------------------------------------- *)
module Tactics = struct
type prinfos =
EcCoreGoal.proofenv * (EcCoreGoal.handle * EcCoreGoal.handle list)
type proofmode = [`WeakCheck | `Check | `Report]
let pi scope pi = Prover.do_prover_info scope pi
let proof ?(src : string option) (scope : scope) =
check_state `InActiveProof "proof script" scope;
match (oget scope.sc_pr_uc).puc_active with
| None -> hierror "no active lemmas"
| Some (pac, pct) ->
let pac =
if pac.puc_started then
hierror "[proof] can only be used at beginning of a proof script";
{ pac with puc_started = true }
in
{ scope with
sc_pr_uc = Some { (oget scope.sc_pr_uc) with puc_active = Some (pac, pct) };
sc_locdoc =
match src with
| Some src -> DocState.push_srcbl scope.sc_locdoc src
| None -> scope.sc_locdoc; }
let process_r ?(src : string option) ?reloc mark (mode : proofmode) (scope : scope) (tac : ptactic list) =
check_state `InProof "proof script" scope;
let scope =
match (oget scope.sc_pr_uc).puc_active with
| None -> hierror "no active lemma"
| Some (pac, _) ->
if mark && not pac.puc_started
then proof scope
else scope
in
let scope = { scope with
sc_locdoc =
match src with
| Some src -> DocState.push_srcbl scope.sc_locdoc src
| None -> scope.sc_locdoc; }
in
let puc = oget (scope.sc_pr_uc) in
let pac, pct = oget (puc).puc_active in
match pac.puc_jdg with
| PSNoCheck ->
None, scope
| PSCheck juc ->
let module TTC = EcHiTacticals in
let htmode =
match mode with
| `WeakCheck -> `Admit
| `Check -> `Strict
| `Report -> `Report
in
let ttenv = {
EcHiGoal.tt_provers = pi scope;
EcHiGoal.tt_smtmode = htmode;
EcHiGoal.tt_implicits = Options.get_implicits scope;
EcHiGoal.tt_oldip = Options.get_oldip scope;
EcHiGoal.tt_redlogic = Options.get_redlogic scope;
EcHiGoal.tt_und_delta = Options.get_und_delta scope; } in
let (hds, juc) =
try TTC.process ttenv tac juc
with EcCoreGoal.TcError tcerror ->
let tcerror =
ofold
(fun reloc error ->
{ error with EcCoreGoal.tc_reloced = Some (reloc, true) })
tcerror reloc
in raise (EcCoreGoal.TcError tcerror)
in
let penv = EcCoreGoal.proofenv_of_proof juc in
let pac = { pac with puc_jdg = PSCheck juc } in
let puc = { puc with puc_active = Some (pac, pct); } in
let scope = { scope with sc_pr_uc = Some puc; } in
Some (penv, hds), scope
let process1_r mark mode scope t =
process_r mark mode scope [t]
let process_core mark mode (scope : scope) (ts : ptactic_core list) =
let ts = List.map (fun t -> { pt_core = t; pt_intros = []; }) ts in
snd (process_r mark mode scope ts)
let process ?(src : string option) scope mode tac =
process_r ?src true mode scope tac
end
(* -------------------------------------------------------------------- *)
module Auto = struct
let add_rw scope ~local ~base l =
let scope, base =
match EcEnv.BaseRw.lookup_opt base.pl_desc (env scope) with
| None ->
let pre, ibase = unloc base in
if not (List.is_empty pre) then
hierror ~loc:base.pl_loc
"cannot create rewrite hints out of its enclosing theory";
let scope =
let item = EcTheory.mkitem ~import:true (EcTheory.Th_baserw (ibase, local)) in
{ scope with sc_env = EcSection.add_item item scope.sc_env; } in
(scope, fst (EcEnv.BaseRw.lookup base.pl_desc (env scope)))
| Some (base, _) -> (scope, base) in
let env = env scope in
let l = List.map (fun l -> EcEnv.Ax.lookup_path (unloc l) env) l in
let item = EcTheory.mkitem ~import:true (Th_addrw (base, l, local)) in
{ scope with sc_env = EcSection.add_item item scope.sc_env }
let bind_hint scope ~local ~level ?base axioms =
let item = EcTheory.mkitem ~import:true (Th_auto { level; base; axioms; locality=local} ) in
{ scope with sc_env = EcSection.add_item item scope.sc_env }
let add_hint scope hint =
let base = omap unloc hint.ht_base in
let env = env scope in
let names = List.map
(fun l -> EcEnv.Ax.lookup_path (unloc l) env)
hint.ht_names in
let mode = if List.mem `Rigid hint.ht_options then `Rigid else `Default in
let names = List.map (fun p -> (p, mode)) names in
bind_hint scope ~local:hint.ht_local ~level:hint.ht_prio ?base names
end
(* -------------------------------------------------------------------- *)
module Ax = struct
open EcParsetree
open EcDecl
module TT = EcTyping
type proofmode = Tactics.proofmode
(* ------------------------------------------------------------------ *)
let bind ?(import = true) (scope : scope) ((x, ax) : _ * axiom) =
assert (scope.sc_pr_uc = None);
let item = EcTheory.mkitem ~import (EcTheory.Th_axiom (x, ax)) in
{ scope with sc_env =
EcSection.add_item item scope.sc_env;
sc_locdoc = DocState.add_item scope.sc_locdoc; }
(* ------------------------------------------------------------------ *)
let start_lemma scope (cont, axflags) check ?name (axd, ctxt) =
let puc =
match check with
| false -> PSNoCheck
| true ->
let hyps = EcEnv.LDecl.init (env scope) axd.ax_tparams in
let proof = EcCoreGoal.start hyps axd.ax_spec in
PSCheck proof
in
let puc =
let active =
{ puc_name = name
; puc_started = false
; puc_jdg = puc
; puc_flags = axflags
; puc_crt = axd }
in
{ puc_active = Some (active, ctxt);
puc_cont = cont;
puc_init = scope.sc_env; }
in
{ scope with sc_pr_uc = Some puc }
(* ------------------------------------------------------------------ *)
let rec add_r (scope : scope) (mode : proofmode) (ax : paxiom located) =
assert (scope.sc_pr_uc = None);
let env = env scope in
let loc = ax.pl_loc and ax = ax.pl_desc in
let ue = TT.transtyvars env (loc, ax.pa_tyvars) in
let (pconcl, tintro) =
match ax.pa_vars with
| None ->
(ax.pa_formula, [])
| Some vs ->
let pconcl = mk_loc loc (PFforall (vs, ax.pa_formula)) in
(pconcl, List.flatten (List.map fst vs))
in
let ip =
let ip x = x |> omap (fun x -> `Named (unloc x)) |> odfl `Clear in
List.map (lmap (fun x -> IPCore (ip x))) tintro in
let tintro = mk_loc loc (Plogic (Pmove prevertv0)) in
let tintro = { pt_core = tintro; pt_intros = [`Ip ip]; } in
let concl = TT.trans_prop env ue pconcl in
if not (EcUnify.UniEnv.closed ue) then
hierror "the formula contains free type variables";
let uidmap = EcUnify.UniEnv.close ue in
let fs = Tuni.subst uidmap in
let concl = Fsubst.f_subst fs concl in
let tparams = EcUnify.UniEnv.tparams ue in
let axd =
let kind =