package electrod

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

Source file Elo.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
(*******************************************************************************
 * electrod - a model finder for relational first-order linear temporal logic
 * 
 * Copyright (C) 2016-2019 ONERA
 * Authors: Julien Brunel (ONERA), David Chemouil (ONERA)
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * SPDX-License-Identifier: MPL-2.0
 * License-Filename: LICENSE.md
 ******************************************************************************)

(** Definition of the type for Electrod models.  *)

open Containers
module HC = Hashcons

(** De Bruijn-style formulas and expressions *)

type ('fml, 'exp, 'iexp) ofml =
  | True
  | False
  | RComp of 'exp * comp_op * 'exp
  | IComp of 'iexp * icomp_op * 'iexp
  | LUn of lunop * 'fml
  | LBin of 'fml * lbinop * 'fml
  | Quant of
      quant
      * ((bool[@opaque]) * (int[@opaque]) * 'exp)
      * (* int : number of bound variables (>= 1); if = 1 then disj must be false *)
      'fml list
  | FIte of 'fml * 'fml * 'fml
  | Block of 'fml list

and quant =
  | All
  | Some_
  | No

and lbinop =
  | And
  | Or
  | Imp
  | Iff
  | U
  | R
  (* releases *)
  | S

(* since *)
and lunop =
  | F
  | G
  | Not
  | O
  (* once *)
  | X
  | H
  | P

(* previous *)
and comp_op =
  | In
  | NotIn
  | REq
  | RNEq

and icomp_op =
  | IEq
  | INEq
  | Lt
  | Lte
  | Gt
  | Gte

and ('fml, 'exp, 'iexp) oexp =
  { prim_exp : ('fml, 'exp, 'iexp) prim_oexp
  ; arity : (int[@opaque]) (* 0 = "polymorphic" arity (that of none) *)
  }

and ('fml, 'exp, 'iexp) prim_oexp =
  | None_
  | Univ
  | Iden
  | Var of (int[@opaque])
  | Name of (Name.t[@opaque])
  | RUn of runop * 'exp
  | RBin of 'exp * rbinop * 'exp
  | RIte of 'fml * 'exp * 'exp
  | Compr of ((bool[@opaque]) * (int[@opaque]) * 'exp) list * 'fml list
  | Prime of 'exp

and runop =
  | Transpose
  | TClos
  | RTClos

and rbinop =
  | Union
  | Inter
  | Over
  | LProj
  | RProj
  | Prod
  | Diff
  | Join

and ('fml, 'exp, 'iexp) oiexp =
  | Num of (int[@opaque])
  | Card of 'exp
  | IUn of iunop * 'iexp
  | IBin of 'iexp * ibinop * 'iexp

and iunop = Neg

and ibinop =
  | Add
  | Sub
[@@deriving
  visitors { variety = "map"; name = "omap" }
  , visitors
      { variety = "fold"
      ; name = "ofold"
      ; ancestors = [ "VisitorsRuntime.map" ]
      }]

type goal = Run of (fml list * bool option) [@@unboxed]

and fml = Fml of (fml, exp, iexp) ofml HC.hash_consed [@@unboxed]

and prim_exp = (fml, exp, iexp) prim_oexp

and exp = Exp of (fml, exp, iexp) oexp HC.hash_consed [@@unboxed]

and iexp = Iexp of (fml, exp, iexp) oiexp HC.hash_consed [@@unboxed]

let fml_table : (fml, exp, iexp) ofml HC.t = HC.create 793

let exp_table : (fml, exp, iexp) oexp HC.t = HC.create 793

let iexp_table : (fml, exp, iexp) oiexp HC.t = HC.create 197

(* to trace the number a piece of data is referenced in the hashconsed syntax DAG *)

module Fml_count = CCHashtbl.Make (struct
  type t = (fml, exp, iexp) ofml HC.hash_consed

  let hash x = x.HC.hkey

  let equal f1 f2 = Equal.physical f1 f2
end)

let fml_count : int Fml_count.t = Fml_count.create 793

let hfml f : fml =
  let hdata = HC.hashcons fml_table f in
  Fml_count.incr fml_count hdata;
  Fml hdata


let exp ~ar (prim_exp : prim_exp) = { prim_exp; arity = ar }

let hexp oe : exp =
  let res = HC.hashcons exp_table oe in
  Exp res


let hiexp oie : iexp =
  let res = HC.hashcons iexp_table oie in
  Iexp res


class ['self] map =
  object (self : 'self)
    inherit [_] omap

    (* TODO is it correct to map the arity to itself?
     BTW the arity is specified as opaque *)
    method visit_'exp env (Exp { node; _ }) = hexp (self#visit_oexp env node)

    method visit_'fml env (Fml { node; _ }) = hfml (self#visit_ofml env node)

    method visit_'iexp env (Iexp { node; _ }) =
      hiexp (self#visit_oiexp env node)

    method visit_exp env exp = self#visit_'exp env exp

    method visit_iexp env iexp = self#visit_'iexp env iexp

    method visit_fml env fml = self#visit_'fml env fml
  end

class virtual ['self] fold =
  object (self : 'self)
    inherit [_] ofold

    (* TODO is it correct to map the arity to itself?
     BTW the arity is specified as opaque *)
    method visit_'exp env (Exp { node; _ }) = self#visit_oexp env node

    method visit_'fml env (Fml { node; _ }) = self#visit_ofml env node

    method visit_'iexp env (Iexp { node; _ }) = self#visit_oiexp env node

    method visit_exp env exp = self#visit_'exp env exp

    method visit_iexp env iexp = self#visit_'iexp env iexp

    method visit_fml env fml = self#visit_'fml env fml
  end

(* type of (well-formed) Electrod models *)
type t =
  { file : string option
  ; (* table of relations indexed by names (remark: a {!Relation.t} also knows its
     own name) *)
    domain : Domain.t
  ; instance : Instance.t
  ; sym : Symmetry.t list
  ; invariants : fml list
  ; goal : goal
  ; atom_renaming : (Atom.t, Atom.t) List.Assoc.t
  ; name_renaming : (Name.t, Name.t) List.Assoc.t
  }

let make file domain instance sym invariants goal atom_renaming name_renaming =
  { file
  ; domain
  ; instance
  ; sym
  ; invariants
  ; goal
  ; atom_renaming
  ; name_renaming
  }


let arity (Exp { node = { arity; _ }; _ }) = arity

let run fs expec = Run (fs, expec)

let true_ = hfml @@ True

let false_ = hfml @@ False

let rcomp exp1 rcomp exp2 = hfml @@ RComp (exp1, rcomp, exp2)

let icomp exp1 rcomp exp2 = hfml @@ IComp (exp1, rcomp, exp2)

let lbinary fml1 binop fml2 = hfml @@ LBin (fml1, binop, fml2)

let sim_binding disj nbvars (range : exp) =
  assert (nbvars > 0);
  assert ((not disj) || nbvars > 1);
  (disj, nbvars, range)


let quant quant decl (block : fml list) =
  assert (not List.(is_empty block));
  hfml @@ Quant (quant, decl, block)


let lunary lunop f = hfml @@ LUn (lunop, f)

let block block = hfml @@ Block block

let fite f t e = hfml @@ FIte (f, t, e)

(* let let_ decls block = Let (decls, block) *)

let all = All

let some = Some_

let no_ = No

let and_ = And

let or_ = Or

let impl = Imp

let iff = Iff

let until = U

let releases = R

let since = S

let not_ = Not

let sometime = F

let always = G

let once = O

let next = X

let historically = H

let previous = P

let none = hexp @@ exp ~ar:0 @@ None_

let univ = hexp @@ exp ~ar:1 @@ Univ

let iden = hexp @@ exp ~ar:2 @@ Iden

let var ~ar n =
  assert (n >= 0);
  hexp @@ exp ~ar @@ Var n


let name ~ar x = hexp @@ exp ~ar @@ Name x

let runary ~ar runop e = hexp @@ exp ~ar @@ RUn (runop, e)

let rbinary ~ar exp1 rbinop exp2 = hexp @@ exp ~ar @@ RBin (exp1, rbinop, exp2)

let rite ~ar cdt then_ else_ = hexp @@ exp ~ar @@ RIte (cdt, then_, else_)

let compr ~ar decls block =
  assert (not (List.is_empty decls));
  assert (not (List.is_empty block));
  hexp @@ exp ~ar @@ Compr (decls, block)


let prime ~ar e = hexp @@ exp ~ar @@ Prime e

let in_ = In

let not_in = NotIn

let req = REq

let rneq = RNEq

let ieq = IEq

let ineq = INEq

let lt = Lt

let lte = Lte

let gt = Gt

let gte = Gte

let transpose = Transpose

let tclos = TClos

let rtclos = RTClos

let union = Union

let inter = Inter

let over = Over

let lproj = LProj

let rproj = RProj

let prod = Prod

let diff = Diff

let join = Join

let num n = hiexp @@ Num n

let card e = hiexp @@ Card e

let iunary op e = hiexp @@ IUn (op, e)

let ibinary exp1 op exp2 = hiexp @@ IBin (exp1, op, exp2)

let neg = Neg

let add = Add

let sub = Sub

(* 
   let%test _ = 
   let e1 = 
   runary ~ar:2 transpose 
   @@ rbinary ~ar:2 
   (name ~ar:2 @@ Name.name "r") 
   inter 
   (runary ~ar:2 tclos @@ name ~ar:2 @@ Name.name "s")
   in
   let e2 = 
   runary ~ar:2 transpose 
   @@ rbinary ~ar:2 
   (name ~ar:2 @@ Name.name "r") 
   inter 
   (runary ~ar:2 tclos @@ name ~ar:2 @@ Name.name "s")
   in
   e1 == e2

*)

(******************************************************************************
 *  Pretty-printing
 *****************************************************************************)

let kwd_styled pf = Fmtc.(styled `Bold) pf

let pp_comp_op out =
  let open Fmtc in
  function
  | In ->
      (kwd_styled pf) out "in"
  | NotIn ->
      (kwd_styled pf) out "not in"
  | REq ->
      pf out "="
  | RNEq ->
      pf out "!="


let pp_icomp_op out =
  let open Fmtc in
  function
  | Lt ->
      pf out "<"
  | IEq ->
      pf out "="
  | INEq ->
      pf out "!="
  | Lte ->
      pf out "<="
  | Gt ->
      pf out ">"
  | Gte ->
      pf out ">="


let pp_lunop out x =
  Fmtc.(kwd_styled pf) out
  @@
  match x with
  | Not ->
      "not"
  | F ->
      "eventually"
  | G ->
      "always"
  | O ->
      "once"
  | X ->
      "next"
  | H ->
      "historically"
  | P ->
      "previous"


let pp_lbinop out x =
  Fmtc.(kwd_styled pf) out
  @@
  match x with
  | And ->
      "and"
  | Or ->
      "or"
  | Imp ->
      "implies"
  | Iff ->
      "iff"
  | U ->
      "until"
  | R ->
      "releases"
  | S ->
      "since"


let pp_quant out x =
  Fmtc.(kwd_styled pf) out
  @@ match x with All -> "all" | Some_ -> "some" | No -> "no"


let pp_runop out =
  let open Fmtc in
  function
  | Transpose -> pf out "~" | TClos -> pf out "^" | RTClos -> pf out "*"


let pp_rbinop out =
  let open Fmtc in
  function
  | Union ->
      pf out "+"
  | Inter ->
      pf out "&"
  | Over ->
      pf out "++"
  | LProj ->
      pf out "<:"
  | RProj ->
      pf out ":>"
  | Prod ->
      pf out "->"
  | Diff ->
      pf out "-"
  | Join ->
      pf out "-"


let pp_iunop out =
  let open Fmtc in
  function Neg -> pf out "-"


let pp_ibinop out =
  let open Fmtc in
  function Add -> pf out "+" | Sub -> pf out "-"


let pp_var out v = Fmtc.pf out "v/%d" v

let pp_osim_binding stacked pp_exp out (disj, vars, e) =
  let open Fmtc in
  pf
    out
    "%a%a :@ %a"
    (if disj then kwd_styled string else nop)
    "disj "
    (list ~sep:(sp **> comma) pp_var)
    (Iter.to_list Int.Infix.(stacked + 1 -- (stacked + vars)))
    (pp_exp stacked)
    e


let rec pp_osim_bindings stacked pp_exp out sim_bindings =
  let open Fmtc in
  match sim_bindings with
  | [] ->
      ()
  | ((_, vars, _) as hd) :: tl ->
      pp_osim_binding stacked pp_exp out hd;
      (sp **> comma) out ();
      pp_osim_bindings (stacked + vars) pp_exp out tl


let pp_oblock stacked pp_fml out fmls =
  let open Fmtc in
  pf
    out
    "@[<b 0>{@[<hv>%a@]@,}@]"
    (list ~sep:(sp **> semi) @@ pp_fml stacked)
    fmls


let pp_ofml stacked pp_fml pp_exp pp_iexp out =
  let open Fmtc in
  function
  | True ->
      (kwd_styled pf) out "true"
  | False ->
      (kwd_styled pf) out "false"
  | RComp (e1, op, e2) ->
      pf
        out
        "@[<2>(%a@ %a@ %a)@]"
        (pp_exp stacked)
        e1
        pp_comp_op
        op
        (pp_exp stacked)
        e2
  | IComp (e1, op, e2) ->
      pf
        out
        "@[<2>(%a@ %a@ %a)@]"
        (pp_iexp stacked)
        e1
        pp_icomp_op
        op
        (pp_iexp stacked)
        e2
  | LUn (op, fml) ->
      pf out "@[<2>(%a@ %a)@]" pp_lunop op (pp_fml stacked) fml
  | LBin (f1, op, f2) ->
      pf
        out
        "@[<2>(%a@ %a@ %a)@]"
        (pp_fml stacked)
        f1
        pp_lbinop
        op
        (pp_fml stacked)
        f2
  | Quant (q, ((_, nbvars, _) as decl), blk) ->
      pf
        out
        "@[<2>(%a %a@ %a)@]"
        pp_quant
        q
        (pp_osim_binding stacked pp_exp)
        decl
        (pp_oblock (stacked + nbvars) pp_fml)
        blk
  | FIte (c, t, e) ->
      pf
        out
        "@[<hv>(%a) %a@;<1 2>@[(%a@])@;%a@;<1 2>@[(%a@])@]"
        (pp_fml stacked)
        c
        (kwd_styled string)
        "implies"
        (pp_fml stacked)
        t
        (kwd_styled string)
        "else"
        (pp_fml stacked)
        e
  | Block fmls ->
      pp_oblock stacked pp_fml out fmls


let pp_prim_oexp stacked pp_fml pp_exp out =
  let open Fmtc in
  function
  | None_ ->
      (styled Name.style pf) out "none"
  | Univ ->
      (styled Name.style pf) out "univ"
  | Iden ->
      (styled Name.style pf) out "iden"
  | Name id ->
      pf out "%a" Name.pp id
  | Var v ->
      assert (v <= stacked);
      pp_var out (stacked - v)
  | RUn (op, e) ->
      pf out "@[<2>(%a%a)@]" pp_runop op (pp_exp stacked) e
  | RBin (e1, Join, e2) ->
      (* special one for join *)
      pf out "@[<2>(%a.%a)@]" (pp_exp stacked) e1 (pp_exp stacked) e2
  | RBin (e1, op, e2) ->
      pf
        out
        "@[<2>(%a@ %a@ %a)@]"
        (pp_exp stacked)
        e1
        pp_rbinop
        op
        (pp_exp stacked)
        e2
  | RIte (c, t, e) ->
      pf
        out
        "@[<hv>%a %a@;<1 2>@[%a@]@;%a@;<1 2>@[%a@]@]"
        (pp_fml stacked)
        c
        (kwd_styled string)
        "implies"
        (pp_exp stacked)
        t
        (kwd_styled string)
        "else"
        (pp_exp stacked)
        e
  | Compr (decls, blk) ->
      let nbvars = List.fold_left (fun acc (_, nb, _) -> acc + nb) 0 decls in
      pf
        out
        "%a"
        ( braces_
        @@ pair
             ~sep:sp
             (pp_osim_bindings stacked pp_exp)
             (pp_oblock (stacked + nbvars) pp_fml) )
        (decls, blk)
  | Prime e ->
      pf out "%a'" (pp_exp stacked) e


let pp_oiexp stacked pp_exp pp_iexp out =
  let open Fmtc in
  function
  | Num n ->
      pf out "%d" n
  | Card e ->
      pf out "@[<2>(# %a)@]" (pp_exp stacked) e
  | IUn (op, iexp) ->
      pf out "@[<2>(%a%a)@]" pp_iunop op (pp_iexp stacked) iexp
  | IBin (e1, op, e2) ->
      pf
        out
        "@[<2>(%a@ %a@ %a)@]"
        (pp_iexp stacked)
        e1
        pp_ibinop
        op
        (pp_iexp stacked)
        e2


let rec pp_fml stacked out (Fml { node; _ }) =
  pp_ofml stacked pp_fml pp_exp pp_iexp out node


and pp_block stacked out fmls = pp_oblock stacked pp_fml out fmls

and pp_iexp stacked out (Iexp { node; _ }) =
  pp_oiexp stacked pp_exp pp_iexp out node


and pp_prim_exp stacked out pe = pp_prim_oexp stacked pp_fml pp_exp out pe

and pp_exp stacked out (Exp { node = e; _ }) =
  pp_prim_exp stacked out e.prim_exp


and pp_sim_binding stacked out sb = pp_osim_binding stacked pp_exp out sb

and pp_sim_bindings stacked out sb = pp_osim_bindings stacked pp_exp out sb

let pp_goal out (Run (fmls, _)) =
  let open Fmtc in
  (kwd_styled pf) out "run@ ";
  pf out "  %a" (box @@ list @@ pp_fml 0) fmls


let pp out { domain; instance; invariants; goal; _ } =
  let open Fmtc in
  pf
    out
    "%a@\n%a@\n%a@\n%a"
    Domain.pp
    domain
    Instance.pp
    instance
    ( vbox2
    @@ styled `Bold
    @@ (const string "invariant" **< cut **< (Fmtc.list @@ pp_fml 0)) )
    invariants
    (vbox @@ pp_goal)
    goal


let pp_fml_stats __out inf =
  let __stats = Fml_count.to_list fml_count in
  let __stats = List.filter (fun (_, count) -> count > inf) __stats in
  let pr s (length, nbval, sum_bl, smallest_bl, median_bl, biggest_bl) =
    Fmt.pr
      "Stats for table of %s:@\n\
       @[<v2>Length: %d@\n\
       Number of entries: %d@\n\
       Sum of bucket lengths: %d@\n\
       Smallest bucket length: %d@\n\
       Median bucket length: %d@\n\
       Biggest bucket length: %d@\n\
       @]@\n"
      s
      length
      nbval
      sum_bl
      smallest_bl
      median_bl
      biggest_bl
  in
  pr "formulas" @@ HC.stats fml_table;
  pr "expressions" @@ HC.stats exp_table;
  pr "integer expressions" @@ HC.stats iexp_table;
  HC.iter (fun data -> Fmt.pr "%a@\n" (pp_fml 0) (Fml data)) fml_table
OCaml

Innovation. Community. Security.