Debian Codegraph @

kernel/rtree.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Util
12
13 (* Type of regular trees:
14 - Var denotes tree variables (like de Bruijn indices)
15 the first int is the depth of the occurrence, and the second int
16 is the index in the array of trees introduced at that depth.
17 Warning: Var's indices both start at 0!
18 - Node denotes the usual tree node, labelled with 'a, to the
19 exception that it takes an array of arrays as argument
20 - Rec(j,v1..vn) introduces infinite tree. It denotes
21 v(j+1) with parameters 0..n-1 replaced by
22 Rec(0,v1..vn)..Rec(n-1,v1..vn) respectively.
23 *)
24 type 'a t =
25 Var of int * int
26 | Node of 'a * 'a t array array
27 | Rec of int * 'a t array
28
29 (* Building trees *)
30 let mk_rec_calls i = Array.init i (fun j -> Var(0,j))
31 let mk_node lab sons = Node (lab, sons)
32
33 (* The usual lift operation *)
34 let rec lift_rtree_rec depth n = function
35 Var (i,j) as t -> if i < depth then t else Var (i+n,j)
36 | Node (l,sons) -> Node (l,Array.map (Array.map (lift_rtree_rec depth n)) sons)
37 | Rec(j,defs) ->
38 Rec(j, Array.map (lift_rtree_rec (depth+1) n) defs)
39
40 let lift n t = if Int.equal n 0 then t else lift_rtree_rec 0 n t
41
42 let rec subst mk sub = function
43 | Var (i, j) ->
44 begin match Esubst.expand_rel (i + 1) sub with
45 | Util.Inl (k, v) -> mk k j v
46 | Util.Inr (m, _) -> Var (m - 1, j)
47 end
48 | Node (l,sons) -> Node (l,Array.map (Array.map (subst mk sub)) sons)
49 | Rec(j, defs) ->
50 Rec(j, Array.map (subst mk (Esubst.subs_lift sub)) defs)
51
52 type 'a clos = Clos of 'a t array * 'a clos Esubst.subs
53
54 type 'a expansion = ExpVar of int * int | ExpNode of 'a * 'a clos Esubst.subs * 'a t array array
55
56 (* To avoid looping, we must check that every body introduces a node
57 or a parameter *)
58 let rec expand0 sub = function
59 | Var (i, j) ->
60 begin match Esubst.expand_rel (i + 1) sub with
61 | Util.Inl (k, v) ->
62 let Clos (v, sub) = v in
63 expand0 (Esubst.subs_shft (k, sub)) (Rec (j, v))
64 | Util.Inr (m, _) -> ExpVar (m - 1, j)
65 end
66 | Rec (j, defs) ->
67 let sub = Esubst.subs_cons (Clos (defs, sub)) sub in
68 expand0 sub defs.(j)
69 | Node (l, sons) -> ExpNode (l, sub, sons)
70
71 let expand t = match expand0 (Esubst.subs_id 0) t with
72 | ExpVar (i, j) -> Var (i, j)
73 | ExpNode (l, sub, sons) ->
74 let rec mk k j (Clos (v, sub)) = subst mk (Esubst.subs_shft (k, sub)) (Rec (j, v)) in
75 let map t = subst mk sub t in
76 let sons = Array.map (fun v -> Array.map map v) sons in
77 Node (l, sons)
78
79 (* Given a vector of n bodies, builds the n mutual recursive trees.
80 Recursive calls are made with parameters (0,0) to (0,n-1). We check
81 the bodies actually build something by checking it is not
82 directly one of the parameters of depth 0. Some care is taken to
83 accept definitions like rec X=Y and Y=f(X,Y) *)
84 let mk_rec defs =
85 let rec check histo d = match expand d with
86 | Var (0, j) ->
87 if Int.Set.mem j histo then failwith "invalid rec call"
88 else check (Int.Set.add j histo) defs.(j)
89 | _ -> ()
90 in
91 Array.mapi (fun i d -> check (Int.Set.singleton i) d; Rec(i,defs)) defs
92 (*
93 let v(i,j) = lift i (mk_rec_calls(j+1)).(j);;
94 let r = (mk_rec[|(mk_rec[|v(1,0)|]).(0)|]).(0);;
95 let r = mk_rec[|v(0,1);v(1,0)|];;
96 the last one should be accepted
97 *)
98
99 (* Tree destructors, expanding loops when necessary *)
100 let dest_var t = match expand0 (Esubst.subs_id 0) t with
101 | ExpVar (i, j) -> (i, j)
102 | _ -> failwith "Rtree.dest_var"
103
104 let dest_node t =
105 match expand t with
106 Node (l,sons) -> (l,sons)
107 | _ -> failwith "Rtree.dest_node"
108
109 let dest_head t = match expand0 (Esubst.subs_id 0) t with
110 | ExpVar _ -> failwith "Rtree.dest_head"
111 | ExpNode (l, _, _) -> l
112
113 let is_node t =
114 match expand t with
115 Node _ -> true
116 | _ -> false
117
118 let rec map f t = match t with
119 Var(i,j) -> Var(i,j)
120 | Node (a,sons) -> Node (f a, Array.map (Array.map (map f)) sons)
121 | Rec(j,defs) -> Rec (j, Array.map (map f) defs)
122
123 module Smart =
124 struct
125
126 let map f t = match t with
127 Var _ -> t
128 | Node (a,sons) ->
129 let a'=f a and sons' = Array.Smart.map (Array.Smart.map (map f)) sons in
130 if a'==a && sons'==sons then t
131 else Node (a',sons')
132 | Rec(j,defs) ->
133 let defs' = Array.Smart.map (map f) defs in
134 if defs'==defs then t
135 else Rec(j,defs')
136
137 end
138
139 module Kind =
140 struct
141
142 type 'a rtree = 'a t
143 type 'a t = { node : 'a rtree; subs : 'a clos Esubst.subs }
144
145 let var i j = Var (i, j)
146 let node l sons = Node (l, sons)
147
148 type 'a kind = Var of int * int | Node of 'a * 'a t array array
149
150 let make t = { node = t; subs = Esubst.subs_id 0 }
151
152 let kind t : 'a kind = match expand0 t.subs t.node with
153 | ExpVar (i, j) -> Var (i, j)
154 | ExpNode (l, subs, sons) ->
155 let map node = { node; subs } in
156 let sons = Array.map (fun v -> Array.map map v) sons in
157 Node (l, sons)
158
159 let repr t = match expand0 t.subs t.node with
160 | ExpVar (i, j) -> var i j
161 | ExpNode (l, subs, sons) ->
162 let rec mk k j (Clos (v, sub)) = subst mk (Esubst.subs_shft (k, sub)) (Rec (j, v)) in
163 let map t = subst mk subs t in
164 let sons = Array.map (fun v -> Array.map map v) sons in
165 node l sons
166
167 end
168
169 (** Structural equality test, parametrized by an equality on elements *)
170
171 let rec raw_eq cmp t t' = match t, t' with
172 | Var (i,j), Var (i',j') -> Int.equal i i' && Int.equal j j'
173 | Node (x, a), Node (x', a') -> cmp x x' && Array.equal (Array.equal (raw_eq cmp)) a a'
174 | Rec (i, a), Rec (i', a') -> Int.equal i i' && Array.equal (raw_eq cmp) a a'
175 | _ -> false
176
177 let raw_eq2 cmp (t,u) (t',u') = raw_eq cmp t t' && raw_eq cmp u u'
178
179 (** Equivalence test on expanded trees. It is parametrized by two
180 equalities on elements:
181 - [cmp] is used when checking for already seen trees
182 - [cmp'] is used when comparing node labels. *)
183
184 let equiv cmp cmp' =
185 let rec compare histo t t' =
186 List.mem_f (raw_eq2 cmp) (t,t') histo ||
187 match expand t, expand t' with
188 | Node(x,v), Node(x',v') ->
189 cmp' x x' &&
190 Int.equal (Array.length v) (Array.length v') &&
191 Array.for_all2 (Array.for_all2 (compare ((t,t')::histo))) v v'
192 | _ -> false
193 in compare []
194
195 (** The main comparison on rtree tries first physical equality, then
196 the structural one, then the logical equivalence *)
197
198 let equal cmp t t' =
199 t == t' || raw_eq cmp t t' || equiv cmp cmp t t'
200
201 (** Intersection of rtrees of same arity *)
202 let rec inter cmp interlbl def n histo t t' =
203 try
204 let (i,j) = List.assoc_f (raw_eq2 cmp) (t,t') histo in
205 Var (n-i-1,j)
206 with Not_found ->
207 match t, t' with
208 | Var (i,j), Var (i',j') ->
209 assert (Int.equal i i' && Int.equal j j'); t
210 | Node (x, a), Node (x', a') ->
211 (match interlbl x x' with
212 | None -> mk_node def [||]
213 | Some x'' -> Node (x'', Array.map2 (Array.map2 (inter cmp interlbl def n histo)) a a'))
214 | Rec (i,v), Rec (i',v') ->
215 (* If possible, we preserve the shape of input trees *)
216 if Int.equal i i' && Int.equal (Array.length v) (Array.length v') then
217 let histo = ((t,t'),(n,i))::histo in
218 Rec(i, Array.map2 (inter cmp interlbl def (n+1) histo) v v')
219 else
220 (* Otherwise, mutually recursive trees are transformed into nested trees *)
221 let histo = ((t,t'),(n,0))::histo in
222 Rec(0, [|inter cmp interlbl def (n+1) histo (expand t) (expand t')|])
223 | Rec _, _ -> inter cmp interlbl def n histo (expand t) t'
224 | _ , Rec _ -> inter cmp interlbl def n histo t (expand t')
225 | _ -> assert false
226
227 let inter cmp interlbl def t t' = inter cmp interlbl def 0 [] t t'
228
229 (** Inclusion of rtrees. We may want a more efficient implementation. *)
230 let incl cmp interlbl def t t' =
231 equal cmp t (inter cmp interlbl def t t')
232
233 (** Tests if a given tree is infinite, i.e. has a branch of infinite length.
234 This corresponds to a cycle when visiting the expanded tree.
235 We use a specific comparison to detect already seen trees. *)
236
237 let is_infinite cmp t =
238 let rec is_inf histo t =
239 List.mem_f (raw_eq cmp) t histo ||
240 match expand t with
241 | Node (_,v) -> Array.exists (Array.exists (is_inf (t::histo))) v
242 | _ -> false
243 in
244 is_inf [] t
245
246 (* Pretty-print a tree (not so pretty) *)
247 open Pp
248
249 let rec pr_tree prl t =
250 match t with
251 | Var (i,j) -> str"#"++int i++str":"++int j
252 | Node(lab,[||]) -> prl lab
253 | Node(lab,v) ->
254 hov 0 (prl lab++str","++spc()++
255 str"["++
256 hv 0 (prvect_with_sep pr_comma (fun a ->
257 str"("++
258 hv 0 (prvect_with_sep pr_comma (pr_tree prl) a)++
259 str")") v)++
260 str"]")
261 | Rec(i,v) ->
262 if Int.equal (Array.length v) 0 then str"Rec{}"
263 else if Int.equal (Array.length v) 1 then
264 hv 2 (str"Rec{"++pr_tree prl v.(0)++str"}")
265 else
266 hv 2 (str"Rec{"++int i++str","++brk(1,0)++
267 prvect_with_sep pr_comma (pr_tree prl) v++str"}")