package electrod

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

Source file Tuple_set.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
(*******************************************************************************
 * 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
 ******************************************************************************)

open Containers

(*$
  ;;
  inject

  open Test
*)

module TS = Tuple.Set

type t = TS.t

let pp out b =
  Fmtc.pf out "@[<hov 2>{";
  TS.pp (* ~start:"" ~stop:"" *) ~sep:" " Tuple.pp out b;
  Fmtc.pf out "}@]"


module P = Intf.Print.Mixin (struct
  type nonrec t = t

  let pp = pp
end)

include P

let to_list = TS.elements

let to_seq = TS.to_seq

let of_seq = TS.of_seq

let empty = TS.empty

let of_tuples tuples =
  match tuples with
  | [] ->
      empty
  | t :: ts ->
      let ar = Tuple.arity t in
      assert (List.for_all (fun t2 -> Tuple.arity t2 = ar) ts);
      TS.of_list tuples


let is_empty = TS.is_empty

let inferred_arity b = if is_empty b then 0 else Tuple.arity @@ TS.choose b

let singleton = TS.singleton

let add = TS.add

let tuples t = t

let inter b1 b2 = TS.inter b1 b2

let size bnd = TS.cardinal bnd

let subset b1 b2 = TS.subset b1 b2

let equal b1 b2 = TS.equal b1 b2

(* |> Fun.tap (fun res -> *)
(*       Msg.debug *)
(*         (fun m -> m "equal %a %a -> %B" *)
(*                     pp b1 pp b2 res)) *)

let compare b1 b2 = TS.compare b1 b2

let product b1 b2 =
  let prod =
    Iter.product (TS.to_seq b1) (TS.to_seq b2)
    |> Iter.map Fun.(uncurry Tuple.( @@@ ))
    |> TS.of_seq
  in
  assert (TS.cardinal prod = TS.cardinal b1 * TS.cardinal b2);
  prod


let union b1 b2 = TS.union b1 b2

let diff = TS.diff

let map f ts = TS.to_seq ts |> Iter.map f |> TS.of_seq

let filter = TS.filter

(*$Q transpose
  any_tupleset (fun ts -> \
  let ar = inferred_arity ts in\
  Q.assume (ar = 2 || ar = 0);\
  equal ts (transpose @@ transpose ts))
*)
let transpose b =
  let ar = inferred_arity b in
  assert (ar = 2 || ar = 0);
  map Tuple.transpose b


(* r ++ s (so we need the first column of s) *)
let override r s =
  let in_r_but_not_in_s1 =
    filter
      (fun tr ->
        not
        @@ TS.exists (fun ts1 -> Tuple.(Atom.equal (ith 0 tr) (ith 0 ts1))) s)
      r
  in
  TS.union s in_r_but_not_in_s1


(* [s <: r] *)
let lproj s r = filter (fun tr -> TS.mem Tuple.([ ith 0 tr ] |> of_list1) s) r

let rproj r s = lproj s @@ transpose r

let diagonal b = map Tuple.(fun e -> e @@@ e) b

(*$Q join
  any_tupleset1 (fun ts -> \
  Q.assume (size ts <> 0);\
  let diag = diagonal ts in\
  equal diag @@ join diag diag\
  )
*)
let join b1 b2 =
  let module S = Iter in
  let ar1 = inferred_arity b1 in
  let ar2 = inferred_arity b2 in
  assert (ar1 <> 1 || ar2 <> 1);
  let s1 = to_seq b1 in
  let s2 = to_seq b2 in
  S.product s1 s2
  |> S.filter_map (fun (t1, t2) ->
         if Atom.equal (Tuple.ith (ar1 - 1) t1) (Tuple.ith 0 t2)
         then Some (Tuple.join t1 t2)
         else None)
  |> of_seq


let transitive_closure b =
  let ar = inferred_arity b in
  assert (ar = 2 || ar = 0);
  if ar = 0
  then b
  else
    let old = ref b in
    let cur = ref (union b (join b b)) in
    let b_to_the_k = ref (join b b) in
    while not @@ TS.equal !old !cur do
      old := !cur;
      b_to_the_k := join b !b_to_the_k;
      cur := union !cur !b_to_the_k
      (* Msg.debug (fun m -> *)
      (*     m "current 2 =  %a " pp !cur); *)
      (* Msg.debug (fun m -> *)
      (*     m "old 2 =  %a " pp !old); *)
      (* Msg.debug (fun m -> m "egalité? %b " (TS.equal !old !cur)) *)
    done;
    !cur


(*$Q transitive_closure_is
  any_tupleset2 (fun ts -> \
  equal (transitive_closure_is ts) (transitive_closure ts)\
  )
*)
(* computes the transitive closure of tue tuple set b using iterative squares *)
let transitive_closure_is b =
  let ar = inferred_arity b in
  assert (ar = 2 || ar = 0);
  if ar = 0
  then b
  else
    let old = ref b in
    let cur = ref (union b (join b b)) in
    while not @@ TS.equal !old !cur do
      old := !cur;
      cur := union !cur (join !cur !cur)
      (* Msg.debug (fun m -> *)
      (*     m "current 2 =  %a " pp !cur); *)
      (* Msg.debug (fun m -> *)
      (*     m "old 2 =  %a " pp !old); *)
      (* Msg.debug (fun m -> m "egalité? %b " (TS.equal !old !cur)) *)
    done;
    !cur


(* let mem_aux (t, bnd) = *)
(*   TS.mem t bnd *)

(* let mem t bnd = *)
(*   CCCache.(with_cache *)
(*              (lru ~eq:(Pair.equal Tuple.equal equal) *)
(*                 ~hash:(Hash.pair Tuple.hash hash) 597) *)
(*              mem_aux) (t, bnd) *)

let mem t bnd = TS.mem t bnd

let rename atom_renaming ts = TS.map (Tuple.rename atom_renaming) ts
OCaml

Innovation. Community. Security.