-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.fs
More file actions
2333 lines (2096 loc) · 141 KB
/
Copy pathEngine.fs
File metadata and controls
2333 lines (2096 loc) · 141 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
module Engine
//--------------------------------------------------------------------
open System.Diagnostics
open System.Collections.Generic
open Common
open IndMap
open PrettyPrinting
open Background
//--------------------------------------------------------------------
let trace = ref 0
let level = ref 0
let module_name = "Engine"
let trace_smt_calls = false
let spaces () =
let rec F level = if level = 0 then "" else " " + F (level-1)
F !level
let indent ppt =
let rec F level ppt = if level = 0 then ppt else blo2 [ F (level-1) ppt ]
toString 120 (F !level ppt)
//--------------------------------------------------------------------
let always_unfold_guards = true
//--------------------------------------------------------------------
type FUNCTION_INTERPRETATION =
| Constructor of (VALUE list -> VALUE)
| StaticBackground of (VALUE list -> VALUE)
| StaticUserDefined of (VALUE list -> VALUE) * (string list * TERM) option
| ControlledInitial of (VALUE list -> VALUE) * (string list * TERM) option
| ControlledUninitialized
| Derived of (string list * TERM) option // string list is the list of arguments, TERM is the body of the derived function
and FUNCTION' =
FctName of string
and FUNCTION_ATTRS = {
fct_id : int;
fct_kind : Signature.FCT_KIND;
fct_types : (TYPE list * TYPE) list; // !!! to be implemented together with monomorphization
fct_interpretation : FUNCTION_INTERPRETATION
}
and FCT_ID =
| FctId of int
and RULE_DEF' =
RuleName of string
and RULE_DEF_ATTRS = {
rule_def_id : int;
// rule_type : (TYPE list * TYPE); // !!! to be implemented together with monomorphization
rule_def : (string list * RULE) option
}
and RULE_DEF_ID =
| RuleDefId of int
and ENGINE' = {
signature : Signature.SIGNATURE
initial_state : State.STATE // use only for initial state in this module, never use '_dynamic' field - also the second elem. of _dynamic_initial seems not to be used !
invariants : Map<string, TERM> // Added invariants field
functions : IndMap<FUNCTION', FUNCTION_ATTRS>
rule_defs : IndMap<RULE_DEF', RULE_DEF_ATTRS>
types : IndMap<TYPE', TYPE_ATTRS>
terms : IndMap<TERM', TERM_ATTRS>
rules : IndMap<RULE', RULE_ATTRS>
supds : IndMap<S_UPDATE_SET', S_UPDATE_SET_ATTRS>
TRUE_ : TERM option ref
FALSE_ : TERM option ref
AND_ : FCT_ID option ref
OR_ : FCT_ID option ref
NOT_ : FCT_ID option ref
EQUALS_ : FCT_ID option ref
BOOLEAN_TYPE_ : TYPE option ref
smt_ctx : SmtInterface.SMT_CONTEXT
}
and ENGINE = Engine of int
and ENGINES = ResizeArray<ENGINE'>
and TYPE = Type of int
and TYPE' =
| Boolean'
| Integer'
| String'
| Undef'
| Rule'
| TypeParam' of string
| TypeCons' of string * TYPE list
| Subset' of string * TYPE
| Prod' of TYPE list
| Seq' of TYPE
| Powerset' of TYPE
| Bag' of TYPE
| Map' of TYPE * TYPE
and TYPE_ATTRS = {
type_id : int
signature_type : Signature.TYPE
carrier_set : VALUE Set option
}
and TERM' =
| Value' of (VALUE) // used for special purposes (symbolic evaluation): "partially interpreted term", not an actual term of the language
| Initial' of (FCT_ID * VALUE list) // used for special purposes (symbolic evaluation): "partially interpreted term", not an actual term of the language
| AppTerm' of (FCT_ID * TERM list)
| CondTerm' of (TERM * TERM * TERM)
| TupleTerm' of (TERM list)
| VarTerm' of (string * TYPE)
| QuantTerm' of (AST.QUANT_KIND * string * TERM * TERM)
| LetTerm' of (string * TERM * TERM)
| DomainTerm' of TYPE // AsmetaL construct: finite type (e.g. enum, abstract, subsetof) used as finite set
| UnfoldedTerm' of ((FCT_ID * VALUE list) * Map<VALUE, TERM>) // not a part of the language, used internally for unfolding transformation
// | TupleTerm of 'annotation * ('annotation ANN_TERM list)
and TERM = Term of int
and TERM_ATTRS = {
term_id : int
term_type : TYPE
smt_expr : SmtInterface.SMT_EXPR option ref
smt_result : Dictionary<int, TERM>
initial_state_eval_res : TERM option ref // (symbolic) value of the term in the initial state, used also for static functions
}
and TERM_INDUCTION<'fct_id, 'term> = {
Value : (VALUE) -> 'term;
Initial : ('fct_id * VALUE list) -> 'term;
AppTerm : ('fct_id * 'term list) -> 'term;
CondTerm : ('term * 'term * 'term) -> 'term;
TupleTerm : ('term list) -> 'term;
VarTerm : (string * TYPE) -> 'term;
QuantTerm : (AST.QUANT_KIND * string * 'term * 'term) -> 'term;
LetTerm : (string * 'term * 'term) -> 'term;
DomainTerm : (TYPE) -> 'term;
UnfoldedTerm : (('fct_id * VALUE list) * Map<VALUE, 'term>) -> 'term;
}
and S_UPDATE_SET' = Set<(FCT_ID * VALUE list) * TERM>
and S_UPDATE_SET = S_UpdateSet of int
and S_UPDATE_SET_ATTRS = { s_update_set_id : int }
and RULE' =
| S_Updates' of S_UPDATE_SET //Map<FCT_NAME * VALUE list, TERM> // used for special purposes (symbolic evaluation): "partially interpreted rules", not actual rules of the language
| UpdateRule' of (FCT_ID * TERM list) * TERM
| CondRule' of TERM * RULE * RULE
| ParRule' of RULE list
| SeqRule' of RULE list
| IterRule' of RULE
| LetRule' of string * TERM * RULE
| ForallRule' of string * TERM * TERM * RULE
| MacroRuleCall' of RULE_DEF_ID * TERM list
| UnfoldedRule' of ((FCT_ID * VALUE list) * Map<VALUE, RULE>) // not a part of the language, used internally for unfolding transformation
and RULE = Rule of int
and RULE_ATTRS = unit
and RULE_INDUCTION<'term, 'rule> = {
S_Updates : S_UPDATE_SET -> 'rule;
UpdateRule : (FCT_ID * 'term list) * 'term -> 'rule;
CondRule : 'term * 'rule * 'rule -> 'rule;
ParRule : 'rule list -> 'rule;
SeqRule : 'rule list -> 'rule;
IterRule : 'rule -> 'rule;
LetRule : string * 'term * 'rule -> 'rule;
ForallRule : string * 'term * 'term * 'rule -> 'rule;
MacroRuleCall : RULE_DEF_ID * 'term list -> 'rule; // Map<FCT_NAME * VALUE list, 'term> -> 'rule;
UnfoldedRule : ((FCT_ID * VALUE list) * Map<VALUE, 'rule>) -> 'rule;
}
and FCT_DEF_DB = Map<Signature.FCT_NAME, string list * TERM> // for function definitions
and RULE_DEF_DB = Map<Signature.RULE_NAME, string list * RULE> // for rule macros
and LOCATION = FCT_ID * VALUE list
and UPDATE = LOCATION * TERM
and UPDATE_SET = Set<UPDATE>
and UPDATE_MAP = Map<FCT_ID, Map<VALUE list, TERM>>
and ENV = Map<string, TERM>
and PATH_COND = Set<TERM> * UPDATE_MAP
and ErrorDetails = ENGINE * string * ErrorDetails'
and ErrorDetails' =
// type errors
| TypeMismatch of TYPE * TYPE
| FunctionCallTypeMismatch of (string * TYPE list * TYPE) * TYPE list
| RuleCallTypeMismatch of string * TYPE list * TYPE list
| TypeOfResultUnknown of string * TYPE list * TYPE
| NoMatchingFunctionType of string * TYPE list
| AmbiguousFunctionCall of string * TYPE list
| NotAFunctionName of string
| VariableAlreadyInUse of string
| UnknownVariable of string
// runtime errors
| InconsistentUpdates of ENGINE * TERM list option * UPDATE * UPDATE * UPDATE_SET option
let engines = new ENGINES()
exception Error of ErrorDetails
let rec error_msg ((eng, fct_name, details) : ErrorDetails) =
let type_to_string, type_list_to_string = type_to_string eng, type_list_to_string eng
(sprintf "error in function Engine.%s:\n" fct_name) +
match details with
// type errors
| TypeMismatch (ty, ty_sign) ->
sprintf "type mismatch: %s does not match %s" (ty |> type_to_string) (ty_sign |> type_to_string)
| FunctionCallTypeMismatch ((fct_name, sign_args_types, sign_res_type), args_types) ->
sprintf "function '%s : %s -> %s' called with arguments of type(s): %s"
fct_name (sign_args_types |> type_list_to_string) (sign_res_type |> type_to_string) (args_types |> type_list_to_string)
| RuleCallTypeMismatch (rname, def_args_types, sign_rule_types) ->
sprintf "rule macro '%s'\n expecting arguments of type(s): (%s)\n called with arguments of type(s): (%s)"
rname (sign_rule_types |> type_list_to_string) (def_args_types |> type_list_to_string)
| TypeOfResultUnknown (fct_name, sign_args_types, sign_res_type) ->
sprintf "type of result of function %s : %s -> %s is unknown (type parameter '%s cannot be instantiated)"
fct_name (sign_args_types |> type_list_to_string) (sign_res_type |> type_to_string) (sign_res_type |> type_to_string)
| NoMatchingFunctionType (fct_name, args_types) ->
sprintf "no matching function type found for '%s' with arguments of type(s) (%s)" fct_name (args_types |> type_list_to_string)
| AmbiguousFunctionCall (fct_name, args_types) ->
sprintf "ambiguous function call: multiple matching function types found for '%s' with arguments of type(s) %s" fct_name (args_types |> type_list_to_string)
| NotAFunctionName name ->
sprintf "there is no function name '%s' in the signature" name
| VariableAlreadyInUse v ->
sprintf "variable '%s' already in use" v
| UnknownVariable v ->
sprintf "unknown variable '%s'" v
// runtime errors
| InconsistentUpdates (eng, opt_conditions, u1, u2, opt_u_set) ->
( sprintf "\n--- inconsistent updates:\n%s\n%s\n" (show_s_update eng u1) (show_s_update eng u2) ) +
( match opt_conditions with
| None -> ""
| Some ts ->
sprintf "\n--- initial state conditions leading to the inconsistent updates:\n%s\n"
(String.concat "\n" (ts >>| term_to_string eng)) ) +
( match opt_u_set with
| None -> ""
| Some U ->
sprintf "\n--- updates collected on this path so far:\n%s\n" (String.concat "\n" (List.map (show_s_update eng) (List.ofSeq U))) )
and type'_to_string eng (ty' : TYPE') =
let type_to_string, type_list_to_string = type_to_string eng, type_list_to_string eng
match ty' with
| TypeParam' a -> "'" ^ a
| Undef' -> "Undef"
| Boolean' -> "Boolean"
| Integer' -> "Integer"
| String' -> "String"
| Rule' -> "Rule"
| TypeCons' (s, tys) -> if List.isEmpty tys then s else s ^ "(" ^ (tys |> type_list_to_string) ^ ")"
| Subset' (tyname, main_type) -> (tyname) ^ " subsetof " ^ (type_to_string main_type)
| Prod' tys -> "Prod(" ^ (tys |> type_list_to_string) ^ ")"
| Seq' ty -> "Seq(" ^ (type_to_string ty) ^ ")"
| Powerset' ty -> "Powerset(" ^ (type_to_string ty) ^ ")"
| Bag' ty -> "Bag(" ^ (type_to_string ty) ^ ")"
| Map' (ty1, ty2) -> "Map(" ^ (type_to_string ty1) ^ ", " ^ (type_to_string ty2) ^ ")"
and type_to_string eng ty =
type'_to_string eng (get_type' eng ty)
and type_list_to_string (eng : ENGINE) tys =
tys >>| type_to_string eng |> String.concat ", "
and fct_type_to_string (eng : ENGINE) (args_type, res_type) =
sprintf "%s -> %s" (args_type |> type_list_to_string eng) (res_type |> type_to_string eng)
and inline get_signature (Engine eid) : Signature.SIGNATURE =
engines.[eid].signature
and inline get_fct_id (Engine eid) (name : string) : FCT_ID =
match engines.[eid].functions |> try_get_index (FctName name) with
| Some fct_id -> FctId fct_id
| None -> failwith (sprintf "Engine.get_fct_id: function '%s' not found in global context #%d" name eid)
and inline get_function' (Engine eid) (FctId id) : FUNCTION' * FUNCTION_ATTRS =
engines.[eid].functions |> get id
and inline fct_name eng fct_id = let (FctName name) = fst (get_function' eng fct_id) in name
and inline fct_kind eng fct_id = (snd (get_function' eng fct_id)).fct_kind
and inline fct_types eng fct_id = (snd (get_function' eng fct_id)).fct_types
and inline fct_interpretation eng fct_id = (snd (get_function' eng fct_id)).fct_interpretation
and inline make_type (eng as Engine eid : ENGINE) (ty' : TYPE') (opt_sign_type : Signature.TYPE option) : TYPE =
let enum_elems (ty' : TYPE') (S : State.STATE) : VALUE Set option =
match ty' with
| Boolean' -> Some State.boolean_carrier_set
| Integer' -> None
| String' -> None
| Undef' -> Some State.undef_carrier_set
| Rule' ->
try Some (Option.get (Map.find "Rule" S._carrier_sets))
with _ -> failwith "SymbState.enum_finite_type: carrier set of 'Rule' not found or not defined" // not found: not in map; not defined: None
| TypeParam' _ -> None
| TypeCons' (tyname, []) ->
try Some (Option.get (Map.find tyname S._carrier_sets))
with _ -> failwith (sprintf "SymbState.enum_finite_type: carrier set of '%s' not found" tyname)
| Subset' (tyname, _) ->
try Some (Option.get (Map.find tyname S._carrier_sets))
with _ -> failwith (sprintf "SymbState.enum_finite_type: carrier set of '%s' not found" tyname)
| TypeCons' (tyname, _) -> failwith (sprintf "SymbState.enum_finite_type: not yet implemented for user-defined type '%s' with type arity > 0" tyname)
| Prod' tys ->
let list_option_list = tys >>| enum_finite_type eng
match List.foldBack (fun head tail ->
tail |> Option.bind (fun t ->
head |> Option.map (fun h -> h::t))
) list_option_list (Some []) with
| Some lists -> ((Common.product_of_lists (lists >>| Set.toList)) >>| TUPLE) |> Set.ofList |> Some
| None -> None
| Seq' _ -> failwith (sprintf "SymbState.enum_finite_type: not yet implemented for 'Seq' types")
| Powerset' _ -> failwith (sprintf "SymbState.enum_finite_type: not yet implemented for 'Powerset' types")
| Bag' _ -> failwith (sprintf "SymbState.enum_finite_type: not yet implemented for 'Bag' types")
| Map' _ -> failwith (sprintf "SymbState.enum_finite_type: not yet implemented for 'Map' types")
let rec type'_to_sign_type (ty' : TYPE') : Signature.TYPE =
let type_to_sign_type ty = type'_to_sign_type (get_type' eng ty)
match ty' with
| Boolean' -> Signature.Boolean
| Integer' -> Signature.Integer
| String' -> Signature.String
| Undef' -> Signature.Undef
| Rule' -> Signature.Rule
| TypeParam' name -> Signature.TypeParam name
| TypeCons' (tyname, ty_args) -> Signature.TypeCons (tyname, ty_args |> List.map type_to_sign_type)
| Subset' (tyname, main_ty) -> Signature.Subset (tyname, type_to_sign_type main_ty)
| Prod' ty_elems -> Signature.Prod (ty_elems |> List.map (type_to_sign_type))
| Seq' elem_ty -> Signature.Seq (type_to_sign_type elem_ty)
| Powerset' elem_ty -> Signature.Powerset (type_to_sign_type elem_ty)
| Bag' elem_ty -> Signature.Bag (type_to_sign_type elem_ty)
| Map' (dom_ty, rng_ty) -> Signature.Map (type_to_sign_type dom_ty, type_to_sign_type rng_ty)
match IndMap.try_get_index ty' engines.[eid].types with
| Some type_id ->
Type type_id
| None ->
let e = engines.[eid]
let type_id = e.types |> count
let attrs = {
type_id = type_id;
signature_type = match opt_sign_type with Some sign_ty -> sign_ty | None -> type'_to_sign_type ty';
carrier_set = try enum_elems ty' e.initial_state with _ -> None //!!!! temporarily set to None for type where enum_finite_type is not yet implemented
}
e.types |> add (ty', attrs) |> Type
and inline get_type eng ty' = make_type eng ty' None // precond.: type must exist, i.e. must have been created before with make_type called with (Some signature_type)
and get_type' (Engine eid) (Type type_id) = engines.[eid].types |> get_obj type_id
and convert_type (eng as Engine eid) (ty : Signature.TYPE) : TYPE =
let make_type, convert_type = make_type eng, convert_type eng
match ty with
| Signature.Boolean -> make_type Boolean' (Some ty)
| Signature.Integer -> make_type Integer' (Some ty)
| Signature.String -> make_type String' (Some ty)
| Signature.Undef -> make_type Undef' (Some ty)
| Signature.Rule -> make_type Rule' (Some ty)
| Signature.TypeParam name -> make_type (TypeParam' name) (Some ty)
| Signature.Prod ty_elems -> make_type (Prod' (ty_elems |> List.map convert_type)) (Some ty)
| Signature.Seq elem_ty -> make_type (Seq' (convert_type elem_ty)) (Some ty)
| Signature.Powerset elem_ty -> make_type (Powerset' (convert_type elem_ty)) (Some ty)
| Signature.Bag elem_ty -> make_type (Bag' (convert_type elem_ty)) (Some ty)
| Signature.Map (dom_ty, rng_ty) -> make_type (Map' (convert_type dom_ty, convert_type rng_ty)) (Some ty)
| Signature.TypeCons (tyname, ty_args) -> make_type (TypeCons' (tyname, ty_args |> List.map convert_type)) (Some ty)
| Signature.Subset (tyname, main_ty) -> make_type (Subset' (tyname, convert_type main_ty)) (Some ty)
and inline to_signature_type (eng as Engine eid) (Type type_id : TYPE) : Signature.TYPE =
(snd (engines.[eid].types |> get type_id)).signature_type
and enum_finite_type (eng as Engine eid) (Type type_id : TYPE) : VALUE Set option =
(snd (engines.[eid].types |> get type_id)).carrier_set
and inline BooleanType (eng : ENGINE) = get_type eng Boolean'
and inline IntegerType (eng : ENGINE) = get_type eng Integer'
and inline StringType (eng : ENGINE) = get_type eng String'
and inline UndefType (eng : ENGINE) = get_type eng Undef'
and inline RuleType (eng : ENGINE) = get_type eng Rule'
and inline TypeParam (eng : ENGINE) (name : string) = get_type eng (TypeParam' name)
and inline ProdType (eng : ENGINE) (ty_elems : TYPE list) = get_type eng (Prod' ty_elems)
and inline SeqType (eng : ENGINE) (elem_ty : TYPE) = get_type eng (Seq' elem_ty)
and inline PowersetType (eng : ENGINE) (elem_ty : TYPE) = get_type eng (Powerset' elem_ty)
and inline BagType (eng : ENGINE) (elem_ty : TYPE) = get_type eng (Bag' elem_ty)
and inline MapType (eng : ENGINE) (dom_ty : TYPE, rng_ty : TYPE) = get_type eng (Map' (dom_ty, rng_ty))
and inline TypeCons (eng : ENGINE) (tyname : string, ty_args : TYPE list) = get_type eng (TypeCons' (tyname, ty_args))
and inline SubsetType (eng : ENGINE) (tyname : string, main_ty : TYPE) = get_type eng (Subset' (tyname, main_ty))
and match_type (eng : ENGINE) (ty : TYPE) (ty_sign : TYPE) (ty_env : Map<string, TYPE>) : Map<string, TYPE> =
let type_to_string, get_type', match_type = type_to_string eng, get_type' eng, match_type eng
if !trace > 2 then fprintf stderr "match_type(%s, %s)\n" (ty |> type_to_string) (ty_sign |> type_to_string)
match (get_type' ty, get_type' ty_sign) with
| (_, Subset' (_, ty_sign')) -> match_type ty ty_sign' ty_env
| (Subset' (_, ty'), _) -> match_type ty' ty_sign ty_env
| (TypeParam' a, _) ->
failwith (sprintf "%s: type parameter not allowed in concrete type to be matched to signature type %s"
(type_to_string ty) (type_to_string ty_sign))
| _ ->
if ty = ty_sign then Map.empty else
match get_type' ty_sign with
| TypeParam' a ->
if Map.containsKey a ty_env then
if ty = Map.find a ty_env then ty_env
else raise (Error (eng, "match_type", TypeMismatch (ty, ty_sign)))
else Map.add a ty ty_env
| _ -> raise (Error (eng, "match_type", TypeMismatch (ty, ty_sign)))
and match_one_fct_type eng (fct_name : string) (args_types : TYPE list) (sign_fct_type : TYPE list * TYPE) : TYPE =
let (sign_args_types, sign_res_type) = sign_fct_type
let result_type sign_res_type ty_env =
match get_type' eng sign_res_type with
| TypeParam' a ->
try Map.find a ty_env
with _ -> raise (Error (eng, "match_one_fct_type", TypeOfResultUnknown (fct_name, sign_args_types, sign_res_type)))
| _ -> sign_res_type
let rec match_types = function
| ([], [], ty_env : Map<string, TYPE>) ->
(ty_env, result_type sign_res_type ty_env)
| (arg_type :: args_types', sign_arg_type :: sign_arg_types', ty_env) ->
let ty_env_1 =
try match_type eng arg_type sign_arg_type ty_env
with _ -> raise (Error (eng, "match_one_fct_type", FunctionCallTypeMismatch ((fct_name, sign_args_types, sign_res_type), args_types)))
match_types (args_types', sign_arg_types', ty_env_1)
| (_, _, _) -> // arity does not match
raise (Error (eng, "match_one_fct_type", FunctionCallTypeMismatch ((fct_name, sign_args_types, sign_res_type), args_types)))
// let (_, result_type) = match_types (args_types, sign_args_types, Map.empty)
let (_, result_type) =
match args_types with
| [ty] ->
match get_type' eng ty with
| Prod' args_types -> match_types (args_types, sign_args_types, Map.empty) // one single tuple (x_1, ..., x_n) is equivalent to n arguments x_1, ..., x_n
| _ -> match_types (args_types, sign_args_types, Map.empty)
| _ -> match_types (args_types, sign_args_types, Map.empty)
result_type
and match_fct_type (eng as Engine eid) (fct_name : string) (args_types : TYPE list) (sign_fct_types : list<TYPE list * TYPE>) : TYPE =
if !trace > 2 then fprintf stderr "\nfunction '%s': match_fct_type (%s) with:\n%s\n" fct_name (args_types |> type_list_to_string eng) (String.concat "," (sign_fct_types >>| fct_type_to_string eng))
let rec matching_types results candidates =
match candidates with
| [] -> results
| sign_fct_type :: candidates' ->
if !trace > 2 then fprintf stderr " sign_fct_type = %s\n" (sign_fct_type |> fct_type_to_string eng)
try match match_one_fct_type eng fct_name args_types sign_fct_type with
| ty -> matching_types (ty :: results) candidates'
with ex -> matching_types results candidates'
let results = List.rev (matching_types [] sign_fct_types)
match results with
| [] -> raise (Error (eng, "match_fct_type", NoMatchingFunctionType (fct_name, args_types)))
| [ty] -> ty
| _ -> raise (Error (eng, "match_fct_type", AmbiguousFunctionCall (fct_name, args_types)))
and type_of_value (eng as Engine eid) x = convert_type eng (Background.type_of_value engines.[eid].signature x) //!!!! could be made more efficient for cells
and main_type_of eng ty = match get_type' eng ty with Subset' (_, main_type) -> main_type | _ -> ty
and compute_type (eng as Engine eid) (t' : TERM') : TYPE =
let fct_name, fct_types, type_of_value, get_term_type = fct_name eng, fct_types eng, type_of_value eng, get_term_type eng
match t' with
| Value' x -> type_of_value x
| Initial' (f, xs) -> match_fct_type eng (fct_name f) (xs >>| type_of_value) (fct_types f)
| AppTerm' (f, ts) -> match_fct_type eng (fct_name f) (ts >>| get_term_type) (fct_types f)
| CondTerm' (G, t1, t2) ->
let ty1 = main_type_of eng (get_term_type t1)
let ty2 = main_type_of eng (get_term_type t2)
if ty1 = ty2 then ty1 else failwith "compute_type: types of branches of conditional term do not match"
| TupleTerm' ts -> ProdType eng (ts >>| get_term_type)
| VarTerm' (v, ty) -> ty
| QuantTerm' (_, _, t_set, _) -> BooleanType eng
| LetTerm' (_, t1, t2) -> get_term_type t2
| DomainTerm' tyname -> PowersetType eng tyname
| UnfoldedTerm' ((f, xs), M) -> if Map.isEmpty M then failwith "compute_type: UnfoldedTerm with empty map" else get_term_type (snd (M |> Map.toList |> List.head))
and get_term_type (eng as Engine eid) (t as Term tid : TERM) : TYPE =
(engines.[eid].terms |> get_attrs tid).term_type
and inline is_boolean_term (eng as Engine eid) (t as Term tid : TERM) : bool =
let e = engines.[eid]
match (e.terms |> get_attrs tid).term_type with
| Type type_id -> match e.types |> get_obj type_id with Boolean' -> true | _-> false
and inline get_smt_expr (Engine eid) (t as Term tid : TERM) : SmtInterface.SMT_EXPR option =
!(engines.[eid].terms |> get_attrs tid).smt_expr
and inline set_smt_expr (Engine eid) (t as Term tid : TERM) (smt_expr : SmtInterface.SMT_EXPR) =
(engines.[eid].terms |> get_attrs tid).smt_expr := Some smt_expr
and inline initial_state_eval_res (Engine eid) (t as Term tid : TERM) : TERM option ref =
(engines.[eid].terms |> get_attrs tid).initial_state_eval_res
and get_term (eng as Engine eid : ENGINE) (t' : TERM') : TERM =
match IndMap.try_get_index t' engines.[eid].terms with
| Some tid ->
Term tid
| None ->
let e = engines.[eid]
let tid = e.terms |> count
let attrs = {
term_id = tid;
term_type = compute_type eng t';
smt_expr = ref None;
smt_result = Dictionary<int, TERM>();
initial_state_eval_res = ref None;
}
e.terms |> add (t', attrs) |> Term
and inline get_term'_attrs (Engine eid) (Term tid) = engines.[eid].terms |> get tid
and get_term' (Engine eid) (Term tid) = engines.[eid].terms |> get_obj tid
and inline get_term_attrs (Engine eid) (Term tid) = engines.[eid].terms |> get_attrs tid
and inline Value eng x = get_term eng (Value' x)
and inline Initial eng (f, xs) = get_term eng (Initial' (f, xs))
and inline AppTerm eng (f, ts) = get_term eng (AppTerm' (f, ts))
and inline CondTerm eng (G, t1, t2) = get_term eng (CondTerm' (G, t1, t2))
and inline TupleTerm eng ts = get_term eng (TupleTerm' ts)
and inline VarTerm eng v = get_term eng (VarTerm' v)
and inline QuantTerm eng (q_kind, v, t_set, t_cond) = get_term eng (QuantTerm' (q_kind, v, t_set, t_cond))
and inline LetTerm eng (x, t1, t2) = get_term eng (LetTerm' (x, t1, t2))
and inline DomainTerm eng tyname = get_term eng (DomainTerm' tyname)
and inline UnfoldedTerm eng (loc, M) = get_term eng (UnfoldedTerm' (loc, M))
and inline TRUE (Engine eid) = !engines.[eid].TRUE_ |> Option.get
and inline FALSE (Engine eid) = !engines.[eid].FALSE_ |> Option.get
and inline AND (Engine eid) = !engines.[eid].AND_ |> Option.get
and inline OR (Engine eid) = !engines.[eid].OR_ |> Option.get
and inline NOT (Engine eid) = !engines.[eid].NOT_ |> Option.get
and inline EQUALS (Engine eid) = !engines.[eid].EQUALS_ |> Option.get
and inline BOOLEAN_TYPE (Engine eid) = !engines.[eid].BOOLEAN_TYPE_ |> Option.get
and convert_term (eng : ENGINE) (t : AST.TYPED_TERM) : TERM =
AST.ann_term_induction (fun x -> x) {
Value = fun (_, x) -> Value eng x;
Initial = fun (_, (f, xs)) -> Initial eng (get_fct_id eng f, xs);
AppTerm = fun (ty, (f, ts)) ->
match f with
| Signature.UndefConst -> Value eng UNDEF
| Signature.BoolConst b -> Value eng (BOOL b)
| Signature.IntConst i -> Value eng (INT i)
| Signature.StringConst s -> Value eng (STRING s)
| Signature.FctName f ->
let f_id = get_fct_id eng f
try
AppTerm eng (f_id, ts)
with ex as Signature.Error (Signature.NoMatchingFunctionType (f, tys)) ->
fprintf stderr "convert_term: in term %A\n" (AppTerm eng (f_id, ts))
raise ex;
TupleTerm = fun (_, ts) -> TupleTerm eng ts;
CondTerm = fun (_, (G, t1, t2)) -> CondTerm eng (G, t1, t2);
VarTerm = fun (ty, v) -> VarTerm eng (v, convert_type eng ty);
QuantTerm = fun (ty, (q_kind, v, t_set, t_cond)) -> QuantTerm eng (q_kind, v, t_set, t_cond);
LetTerm = fun (_, (v, t1, t2)) -> LetTerm eng (v, t1, t2);
DomainTerm = fun (_, D) -> DomainTerm eng (convert_type eng D);
} t
and inline get_s_update_set (eng as Engine eid : ENGINE) (us' : S_UPDATE_SET') : S_UPDATE_SET =
match IndMap.try_get_index us' engines.[eid].supds with
| Some uid ->
S_UpdateSet uid
| None ->
let e = engines.[eid]
let uid = e.supds |> count
let attrs : S_UPDATE_SET_ATTRS = {
s_update_set_id = uid
}
e.supds |> add (us', attrs) |> S_UpdateSet
and inline get_s_update_set' (Engine eid) (S_UpdateSet uid : S_UPDATE_SET) : S_UPDATE_SET' =
engines.[eid].supds |> get uid |> fst
and inline s_update_set_empty (eng as Engine eid) : S_UPDATE_SET =
get_s_update_set eng Set.empty
and inline s_update_set_is_empty (eng as Engine eid) (S_UpdateSet uid : S_UPDATE_SET) : bool =
engines.[eid].supds |> get uid |> fst |> Set.isEmpty
and inline s_update_set_union (eng as Engine eid) (S_UpdateSet uid1 : S_UPDATE_SET) (S_UpdateSet uid2 : S_UPDATE_SET) : S_UPDATE_SET =
let supds = engines.[eid].supds
let us1, _ = supds |> get uid1
let us2, _ = supds |> get uid2
get_s_update_set eng (Set.union us1 us2)
and inline s_update_set_seq_merge_2 (eng as Engine eid) (S_UpdateSet uid1 : S_UPDATE_SET) (S_UpdateSet uid2 : S_UPDATE_SET) : S_UPDATE_SET =
let supds = engines.[eid].supds
let us1, _ = supds |> get uid1
let us2, _ = supds |> get uid2
get_s_update_set eng (seq_merge_2 eng us1 us2)
and inline s_update_set_to_list (Engine eid) (S_UpdateSet uid : S_UPDATE_SET) : list<(FCT_ID * VALUE list) * TERM> =
let us', _ = engines.[eid].supds |> get uid
List.ofSeq us'
and inline get_rule_def_id (Engine eid) (name : string) : RULE_DEF_ID =
match engines.[eid].rule_defs |> try_get_index (RuleName name) with
| Some rule_def_id -> RuleDefId rule_def_id
| None -> failwith (sprintf "Engine.get_named_rule_id: function '%s' not found in global context #%d" name eid)
and inline get_rule_def' (Engine eid) (RuleDefId id : RULE_DEF_ID) : RULE_DEF' * RULE_DEF_ATTRS =
engines.[eid].rule_defs |> get id
and inline get_rule_name (Engine eid) (RuleDefId id : RULE_DEF_ID) = let RuleName r_name, _ =engines.[eid].rule_defs |> get id in r_name
and inline get_rule_def (eng as Engine eid) (id : RULE_DEF_ID) = (snd (get_rule_def' eng id)).rule_def
and inline get_rule' (Engine eid) (Rule rid) = engines.[eid].rules |> get rid |> fst
and inline get_rule (eng as Engine eid : ENGINE) (R' : RULE') : RULE =
match IndMap.try_get_index R' engines.[eid].rules with
| Some rid ->
Rule rid
| None ->
let e = engines.[eid]
let rid = e.rules |> count
e.rules |> add (R', ()) |> Rule
and inline UpdateRule eng ((f, ts), t_rhs) = get_rule eng (UpdateRule' ((f, ts), t_rhs))
and inline CondRule eng (G, R1, R2) = get_rule eng (CondRule' (G, R1, R2))
and inline ParRule eng Rs = get_rule eng (ParRule' Rs)
and inline SeqRule eng Rs = get_rule eng (SeqRule' Rs)
and inline IterRule eng R' = get_rule eng (IterRule' R')
and inline LetRule eng (v, t1, R') = get_rule eng (LetRule' (v, t1, R'))
and inline MacroRuleCall eng (r, args) = get_rule eng (MacroRuleCall' (r, args))
and inline ForallRule eng (v, t_set, G, R') = get_rule eng (ForallRule' (v, t_set, G, R'))
and inline S_Updates eng upds = get_rule eng (S_Updates' upds) // Map.map (fun ((f, xs), t_rhs) -> ((get_fct_id eng f, xs), convert_term eng t_rhs)) upds
and inline UnfoldedRule eng (loc, M) = get_rule eng (UnfoldedRule' (loc, M))
and convert_rule (eng : ENGINE) (R : AST.RULE) : RULE =
AST.rule_induction (convert_term eng) {
UpdateRule = fun ((f, ts), t_rhs) -> UpdateRule eng ((get_fct_id eng f, ts), t_rhs);
CondRule = fun (G, R1, R2) -> CondRule eng (G, R1, R2);
ParRule = fun Rs -> ParRule eng Rs;
SeqRule = fun Rs -> SeqRule eng Rs;
IterRule = fun R' -> IterRule eng R';
LetRule = fun (v, t1, R') -> LetRule eng (v, t1, R');
MacroRuleCall = fun (r_name, args) -> MacroRuleCall eng (get_rule_def_id eng r_name, args);
ForallRule = fun (v, t_set, G, R') -> ForallRule eng (v, t_set, G, R');
S_Updates = fun upds -> S_Updates eng (get_s_update_set eng (Set.map (fun ((f, xs), t_rhs) -> (get_fct_id eng f, xs), convert_term eng t_rhs) upds))
} R
and new_engine (sign : Signature.SIGNATURE, initial_state : State.STATE, fct_def_db : AST.MACRO_DB, rule_def_db : AST.RULES_DB, invariants : Map<string, AST.TYPED_TERM>, smt_ctx : SmtInterface.SMT_CONTEXT) : ENGINE =
let eid = engines.Count
let new_engine = {
signature = sign
initial_state = initial_state
invariants = Map.empty
functions = newIndMap<FUNCTION', FUNCTION_ATTRS>()
rule_defs = newIndMap<RULE_DEF', RULE_DEF_ATTRS>()
types = newIndMap<TYPE', TYPE_ATTRS>()
terms = newIndMap<TERM', TERM_ATTRS>()
rules = newIndMap<RULE', RULE_ATTRS>()
supds = newIndMap<S_UPDATE_SET', S_UPDATE_SET_ATTRS>()
TRUE_ = ref None
FALSE_ = ref None
AND_ = ref None
OR_ = ref None
NOT_ = ref None
EQUALS_ = ref None
BOOLEAN_TYPE_ = ref None
smt_ctx = smt_ctx
}
engines.Add new_engine
let extract_fct_interpretation_if_possible f_name =
let f_kind = Signature.fct_kind f_name sign
match f_kind with
| Signature.Constructor ->
Some (Constructor (fun xs -> CELL (f_name, xs)))
| Signature.Static ->
match initial_state._static |> Map.tryFind f_name with
| None -> fprintf stderr "Warning: static function '%s' is in signature, but is not defined - ignored\n" f_name; None
| Some fct_interp ->
if Set.contains f_name (Signature.fct_names Background.signature) // background functions
then Some (StaticBackground fct_interp)
else Some (StaticUserDefined (fct_interp, None)) // second component is the AsmetaL definition to be filled in later
| Signature.Controlled ->
match initial_state._dynamic_initial |> fst |> Map.tryFind f_name with
| Some fct_def -> Some (ControlledInitial (fct_def, None)) // AsmetaL definition of controlled function initialization will be filled in later
| None -> Some ControlledUninitialized
| Signature.Derived ->
match fct_def_db |> Map.tryFind f_name with
| Some (args, body) -> Some (Derived None) // AsmetaL definition of derived function will be filled in later
| None -> failwith (sprintf "Engine.new_engine: derived function '%s' is in signature, but is not defined\n" f_name)
| Signature.Monitored -> failwith (sprintf "Engine.new_engine: monitored function '%s' - not implemented" f_name)
| Signature.Shared -> failwith (sprintf "Engine.new_engine: shared function '%s' - not implemented" f_name)
| Signature.Out -> failwith (sprintf "Engine.new_engine: out function '%s' - not implemented" f_name)
let extract_rule_def_rhs r_name =
match rule_def_db |> Map.tryFind r_name with
| Some (args, R) -> (args, R)
| None -> failwith (sprintf "Engine.new_engine: no definition found for rule macro '%s'\n" r_name)
let add_functions fct_list =
fct_list
|> List.map (fun (name, _) -> name, extract_fct_interpretation_if_possible name)
|> List.filter (function (_, None) -> false | _ -> true)
|> Seq.iteri ( fun i (name, opt_fct_interpretation) ->
let sign = sign
match opt_fct_interpretation with
| None -> ()
| Some fct_interpretation ->
new_engine.functions |> add (
FctName name, {
fct_id = i;
fct_kind = Signature.fct_kind name sign;
fct_types = List.map (fun (args_ty, res_ty) -> (args_ty >>| convert_type (Engine eid), convert_type (Engine eid) res_ty)) (Signature.fct_types name sign);
fct_interpretation = fct_interpretation
}) |> ignore )
let functions = sign |> Map.toList |> List.filter (fun (name, _) -> Signature.is_function_name name sign)
// add to engine non-derived functions first, then derived functions
functions |> List.filter (fun (name, _) -> not (Signature.fct_kind name sign = Signature.Derived)) |> add_functions
functions |> List.filter (fun (name, _) -> Signature.fct_kind name sign = Signature.Derived) |> add_functions
// add rules
rule_def_db |> Map.toList
|> List.map (fun (name, _) -> (name, extract_rule_def_rhs name))
|> Seq.iteri (fun i (name, rule_def) ->
new_engine.rule_defs |> add (
RuleName name, {
rule_def_id = i;
// rule_type = Signature.rule_types name sign; // !!! to be implemented together with monomorphization
rule_def = None
}) |> ignore )
for i in 0..(new_engine.functions |> count) - 1 do
let FctName f_name, (f_attrs as { fct_interpretation = fct_intp }) = new_engine.functions |> get i
match fct_intp with
| StaticUserDefined (fct_interp, None) ->
match fct_def_db |> Map.tryFind f_name with
| Some (args, body) ->
new_engine.functions |> set i (FctName f_name, { f_attrs with fct_interpretation = StaticUserDefined (fct_interp, Some (args, convert_term (Engine eid) body)) })
| None -> failwith (sprintf "cannot find definition of static function '%s'\n" f_name)
| ControlledInitial (fct_interp, None) ->
match fct_def_db |> Map.tryFind f_name with
| Some (args, body) ->
new_engine.functions |> set i (FctName f_name, { f_attrs with fct_interpretation = ControlledInitial (fct_interp, Some (args, convert_term (Engine eid) body)) })
| None -> failwith (sprintf "cannot find initial definition of controlled function '%s'\n" f_name)
| Derived None ->
match fct_def_db |> Map.tryFind f_name with
| Some (args, body) ->
new_engine.functions |> set i (FctName f_name, { f_attrs with fct_interpretation = Derived (Some (args, convert_term (Engine eid) body)) })
| None -> failwith (sprintf "cannot find definition of derived function '%s'\n" f_name)
| _ -> () // if not any of the above cases, do nothing (the entry of fctTable was already completely initialized)
for i in 0..(new_engine.rule_defs |> count) - 1 do
let RuleName rd_name, (rd_attrs as { rule_def_id = id; rule_def = _ }) = new_engine.rule_defs |> get i
let (args, body) = extract_rule_def_rhs rd_name
new_engine.rule_defs |> set i (RuleName rd_name, { rd_attrs with rule_def = Some (args, convert_rule (Engine eid) body) })
let new_ctx = {
new_engine with
invariants = Map.map (fun _ t -> convert_term (Engine eid) t) invariants
}
engines.[eid] <- new_ctx
engines.[eid].TRUE_ := Some (Value (Engine eid) (BOOL true))
engines.[eid].FALSE_ := Some (Value (Engine eid) (BOOL false))
engines.[eid].AND_ := Some (get_fct_id (Engine eid) "and")
engines.[eid].OR_ := Some (get_fct_id (Engine eid) "or")
engines.[eid].NOT_ := Some (get_fct_id (Engine eid) "not")
engines.[eid].EQUALS_ := Some (get_fct_id (Engine eid) "=")
engines.[eid].BOOLEAN_TYPE_ := Some (BooleanType (Engine eid))
Engine eid
and get_engine' (Engine eid : ENGINE) : ENGINE' =
if eid < engines.Count then
engines.[eid]
else
failwith (sprintf "get_engine': engine %d not found" eid)
and initial_state_of (Engine eid) =
engines.[eid].initial_state
and invariants_of (Engine eid) =
engines.[eid].invariants
and smt_solver_push (Engine eid) =
SmtInterface.smt_solver_push engines.[eid].smt_ctx
and smt_solver_pop (Engine eid) =
SmtInterface.smt_solver_pop engines.[eid].smt_ctx
//--------------------------------------------------------------------
and term_induction (eng: ENGINE) (fct_id : FCT_ID -> 'fct_id) (F : TERM_INDUCTION<'fct_id, 'term>) (t : TERM) :'term =
let term_ind = term_induction eng fct_id F
match get_term' eng t with
| Value' x -> F.Value x
| Initial' (f, xs) -> F.Initial (fct_id f, xs)
| AppTerm' (f, ts) -> F.AppTerm (fct_id f, List.map (fun t -> term_ind t) ts)
| CondTerm' (G, t1, t2) -> F.CondTerm (term_ind G, term_ind t1, term_ind t2)
| TupleTerm' ts -> F.TupleTerm (List.map (fun t -> term_ind t) ts)
| VarTerm' (v, ty) -> F.VarTerm (v, ty)
| QuantTerm' (q_kind, v, t_set, t_cond) -> F.QuantTerm (q_kind, v, term_ind t_set, term_ind t_cond)
| LetTerm' (x, t1, t2) -> F.LetTerm (x, term_ind t1, term_ind t2)
| DomainTerm' tyname -> F.DomainTerm tyname
| UnfoldedTerm' ((f, xs), M) -> F.UnfoldedTerm ((fct_id f, xs), Map.map (fun _ t_i -> term_ind t_i) M)
//--------------------------------------------------------------------
and skipRule eng = ParRule eng []
//--------------------------------------------------------------------
and rule_induction (eng: ENGINE) (term : TERM -> 'term) (F : RULE_INDUCTION<'term, 'rule>) (R : RULE) : 'rule =
let rule_ind = rule_induction eng term
match get_rule' eng R with
| S_Updates' U -> F.S_Updates U // F.S_Updates (Map.map (fun loc -> fun t_rhs -> term t_rhs) U)
| UpdateRule' ((f, ts), t) -> F.UpdateRule ((f, List.map term ts), term t)
| CondRule' (G, R1, R2: RULE) -> F.CondRule (term G, rule_ind F R1, rule_ind F R2)
| ParRule' Rs -> F.ParRule (List.map (rule_ind F) Rs)
| SeqRule' Rs -> F.SeqRule (List.map (rule_ind F) Rs)
| IterRule' R -> F.IterRule (rule_ind F R)
| LetRule' (v, t, R) -> F.LetRule (v, term t, (rule_ind F) R)
| ForallRule' (v, t_set, t_filter, R) -> F.ForallRule (v, term t_set, term t_filter, (rule_ind F) R)
| MacroRuleCall' (r, ts) -> F.MacroRuleCall (r, List.map term ts)
| UnfoldedRule' ((f, xs), M) -> F.UnfoldedRule ((f, xs), Map.map (fun _ R_i -> rule_ind F R_i) M)
//--------------------------------------------------------------------
//
// pretty printing
//
//--------------------------------------------------------------------
and pp_list sep = function
| [] -> []
| [x] -> [ x ]
| (x :: xs') -> x :: (sep @ (pp_list sep xs'))
and pp_name (name : Signature.NAME) =
( match name with
| Signature.UndefConst -> "undef"
| Signature.BoolConst b -> if b then "true" else "false"
| Signature.IntConst i -> i.ToString()
| Signature.StringConst s -> "\"" + s + "\""
| Signature.FctName f -> f ) |> str
and pp_app_term sign = function
| (Signature.FctName f, [t1; t2]) when Signature.infix_status f sign <> Signature.NonInfix ->
blo0 [ str "("; blo0 [ t1; brk 1; str (sprintf "%s " f); t2 ]; str ")" ]
| (Signature.FctName f, ts) when ts <> [] ->
blo0 [ str f; str " "; str "("; blo0 (pp_list [str",";brk 1] ts); str ")" ]
| (name, _) -> pp_name name
and pp_location_term sign prefix = function
| (f : string, xs : VALUE list) when xs <> [] ->
blo0 [ str (prefix+"["); str f; str "("; blo0 (pp_list [str",";brk 1] (List.map (fun x -> str (value_to_string x)) xs)); str ")]" ]
| (f, _) -> blo0 [ str $"{prefix}[{f}]" ]
and pp_term (eng : ENGINE) (t : TERM) =
let sign = (get_engine' eng).signature
let (pp_app_term, pp_location_term) = (pp_app_term sign, pp_location_term sign)
term_induction eng (fun x -> x) {
AppTerm = fun (f, ts) -> pp_app_term (Signature.FctName (fct_name eng f), ts);
CondTerm = fun (G, t1, t2) -> blo0 [ str "if "; G; line_brk; str "then "; t1; line_brk; str "else "; t2; line_brk; str "endif" ];
Value = fun x -> str (value_to_string x);
Initial = fun (f, xs) -> pp_location_term "initial" (fct_name eng f, xs);
TupleTerm = fun ts -> blo0 [ str "("; blo0 (pp_list [str",";brk 1] ts); str ")" ];
VarTerm = fun (x, _) -> str x;
QuantTerm = fun (q_kind, v, t_set, t_cond) ->
blo0 [ str ("("^(AST.quant_kind_to_str q_kind)^" "); str v; str " in "; t_set; str " with "; t_cond; str ")" ];
LetTerm = fun (v, t1, t2) -> blo0 [ str "let "; str v; str " = "; t1; line_brk; str "in "; t2; line_brk; str "endlet" ];
DomainTerm = fun tyname -> str (type_to_string eng tyname);
UnfoldedTerm = fun ((f, xs), M) ->
let pp_case (x_i, t_i) = blo0 [ pp_location_term "initial" (fct_name eng f, xs); str " = "; str (value_to_string x_i); str " -> "; t_i ]
let L = M |> Map.toList |> List.map pp_case
blo0 [ str "unfolded "; line_brk; blo2 ( pp_list [line_brk] L ); line_brk; str "endunfolded" ];
} t
and pp_rule (eng : ENGINE) (R : RULE) =
let sign = (get_engine' eng).signature
let (pp_app_term, pp_location_term, pp_term) = (pp_app_term sign, pp_location_term sign, pp_term eng)
rule_induction eng pp_term {
S_Updates = fun U ->
let pp_elem ((f, xs), t) = blo0 [ str (fct_name eng f); str " "; str "("; blo0 (pp_list [str",";brk 1] (xs >>| fun x -> str (value_to_string x))); str ") := "; (pp_term t) ]
let L = s_update_set_to_list eng U >>| pp_elem
blo0 [ str "{"; line_brk; blo2 ( pp_list [line_brk] L); line_brk; str "}" ];
UpdateRule = fun ((f, ts), t) -> blo0 [ pp_app_term (Signature.FctName (fct_name eng f), ts); str " := "; t ];
CondRule = fun (G, R1, R2) -> blo0 ( str "if " :: G:: str " then " :: line_brk :: blo2 [ R1 ] :: line_brk ::
(if R2 <> str "skip" then [ str "else "; line_brk; blo2 [ R2 ]; line_brk; str "endif" ] else [ str "endif"]) );
ParRule = fun Rs -> if Rs <> [] then blo0 [ str "par"; line_brk; blo2 ( pp_list [line_brk] Rs); line_brk; str "endpar" ] else str "skip";
SeqRule = fun Rs -> blo0 [ str "seq"; line_brk; blo2 (pp_list [line_brk] Rs); line_brk; str "endseq" ];
IterRule = fun R' -> blo0 [ str "iterate "; line_brk; blo2 [ R' ]; line_brk; str "enditerate" ];
LetRule = fun (v, t, R) -> blo0 [ str "let "; str v; str " = "; t; line_brk; str "in "; R; line_brk; str "endlet" ];
ForallRule = fun (v, t_set, t_filter, R) -> blo0 [ str "forall "; str v; str " in "; t_set; str " with "; t_filter; str " do"; line_brk; blo2 [ R ]; line_brk; str "endforall" ];
MacroRuleCall = fun (r, ts) -> blo0 [ str (get_rule_name eng r); str "["; blo0 (pp_list [str",";brk 1] ts); str "]" ];
UnfoldedRule = fun ((f, xs), M) ->
let pp_case (x_i, t_i) = blo0 [ pp_location_term "" (fct_name eng f, xs); str " = "; str (value_to_string x_i); str " -> "; t_i ]
let L = M |> Map.toList |> List.map pp_case
blo0 [ str "unfolded "; line_brk; blo2 ( pp_list [line_brk] L ); line_brk; str "endunfolded" ];
} R
and name_to_string t = t |> pp_name |> PrettyPrinting.toString 80
and term_to_string eng t = t |> pp_term eng |> PrettyPrinting.toString 80
and rule_to_string eng t = t |> pp_rule eng |> PrettyPrinting.toString 80
//--------------------------------------------------------------------
//
// function tables
//
//--------------------------------------------------------------------
and show_fct_tables (eng as Engine eid: ENGINE) =
let e = get_engine' eng
let index_s = e.functions |> show_index (fun (FctName f_name) -> f_name)
let show_table_entry (i, FctName f_name, { fct_kind = f_kind; fct_id = f_id; fct_interpretation = f_intp }) =
if i <> f_id then
failwith (sprintf "show_fct_tables: function id %d does not match index %d" f_id i)
else
sprintf "%d: %s (%s) = [%s]"
f_id f_name (Signature.fct_kind_to_string f_kind)
(match f_intp with
| Constructor _ -> "constructor"
| StaticBackground _ -> "static (background)"
| StaticUserDefined _ -> "static (user-defined)"
| ControlledInitial _ -> "controlled (initial)"
| ControlledUninitialized -> "controlled (uninitialized)"
| Derived (Some (args, body)) -> sprintf "derived (%s) = %s" (String.concat ", " args) (term_to_string eng body)
| Derived None -> "derived (definition missing)")
let table_s = e.functions |> show_table show_table_entry
index_s + table_s
//--------------------------------------------------------------------
//
// locations, symbolic updates, symbolic update sets
//
//--------------------------------------------------------------------
and location_to_string eng ((f, xs) : LOCATION) : string = Updates.location_to_string (fct_name eng f, xs)
and show_s_update eng ((f, xs), t) =
let f = fct_name eng f
sprintf "%s := %s"
(if List.isEmpty xs then f else sprintf "%s (%s)" f (String.concat ", " (List.map value_to_string xs)))
(PrettyPrinting.toString 80 (pp_term eng t))
and show_s_update_set eng (U :UPDATE_SET) =
"{ " +
( Set.toList U >>| show_s_update eng
|> String.concat ", " ) +
" }"
and show_s_update_map eng (U :UPDATE_MAP) =
let s_update_set = Set.ofSeq (Map.toSeq U |> Seq.collect (fun (f : FCT_ID, table) -> table |> Map.toSeq |> Seq.map (fun (args, value) -> (f, args), value)))
show_s_update_set eng s_update_set
//--------------------------------------------------------------------
and add_s_update eng (U : UPDATE_MAP) (u as (loc as (f, args), value): UPDATE) =
if !trace > 0 then fprintf stderr "add_s_update: %s\n" (show_s_update eng u)
Map.change f
( function None -> Some (Map.add args value Map.empty)
| Some table ->
Some ( if Map.containsKey args table
then if value <> Map.find args table // deal with conflicting updates
then raise (Error (eng, "add_s_update", InconsistentUpdates (eng, None, (loc, Map.find args table), (loc, value), None)))
else table
else Map.add args value table ) )
U
and s_update_set'_to_s_update_map eng (U : S_UPDATE_SET') = // note: S_UPDATE_SET' is then UPDATE_SET
Set.fold (add_s_update eng) Map.empty U
and s_update_set_to_s_update_map eng (U : S_UPDATE_SET) =
let U' = get_s_update_set' eng U
s_update_set'_to_s_update_map eng U'
and consistent eng (U : S_UPDATE_SET') =
try let x = s_update_set'_to_s_update_map eng U
in true
with Failure _ -> false
and locations (U : UPDATE_SET) : Set<LOCATION> =
Set.map (fun (loc, value) -> loc) U
and seq_merge_2 eng (U : UPDATE_SET) (V : UPDATE_SET) =
if not (consistent eng U)
then U
else let U_reduced = Set.filter (fun (loc, _) -> not (Set.contains loc (locations V))) U
in Set.union U_reduced V
and seq_merge_n eng (Us : UPDATE_SET list) : UPDATE_SET =
List.fold (seq_merge_2 eng) Set.empty Us
and apply_s_update_map (UM0 : UPDATE_MAP) (UM' : UPDATE_MAP) =
let update_dynamic_function_table (f_table : Map<VALUE list, TERM>) (updates_of_f : Map<VALUE list, TERM>) =
Map.fold (fun table args value -> Map.add args value table) f_table updates_of_f
let apply_to_s_update_map (UM0 : UPDATE_MAP) (UM' : UPDATE_MAP) =
Map.fold
( fun UM f updates_of_f ->
Map.change f
(function None -> Some (update_dynamic_function_table Map.empty updates_of_f)
| Some f_table -> Some (update_dynamic_function_table f_table updates_of_f)) UM )
UM0 UM'
in apply_to_s_update_map UM0 UM'
and apply_s_update_set eng S U =
apply_s_update_map S (s_update_set'_to_s_update_map eng U)