| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 57 |
|
| 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 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 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 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 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 |
|
| 196 |
|
| 197 |
|
| 198 |
let equal cmp t t' = |
| 199 |
t == t' || raw_eq cmp t t' || equiv cmp cmp t t' |
| 200 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 230 |
let incl cmp interlbl def t t' = |
| 231 |
equal cmp t (inter cmp interlbl def t t') |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 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 |
|
| 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"}") |