package logtk
Core types and algorithms for logic
Install
Dune Dependency
Authors
Maintainers
Sources
1.5.1.tar.gz
md5=cc320f66f10555c54822da624419e003
sha512=f8d5f7a5ae790bf0388d74261673803cf375f91f92f7b413b70db1ce5841ef55343a208f98727c8551d66f1840ab892f1c0c943a34861d14d79ce469b235a2f2
doc/src/logtk/NPDtree.ml.html
Source file NPDtree.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
(* This file is free software, part of Zipperposition. See file "license" for more details. *) (** {1 Non-Perfect Discrimination Tree} *) module T = Term module S = Subst module TC = T.Classic let prof_npdtree_retrieve = Util.mk_profiler "NPDtree_retrieve" let prof_npdtree_term_unify = Util.mk_profiler "NPDtree_term_unify" let prof_npdtree_term_generalizations = Util.mk_profiler "NPDtree_term_generalizations" let prof_npdtree_term_specializations = Util.mk_profiler "NPDtree_term_specializations" (** {2 Term traversal} *) (** Term traversal in prefix order. This is akin to lazy transformation to a flatterm. *) type iterator = { cur_term : T.t; stack : T.t list list; (* skip: drop head, next: first of head *) } let open_term ~stack t = match T.view t with | T.Var _ | T.DB _ | T.AppBuiltin _ | T.Fun _ | T.Const _ -> Some {cur_term=t; stack=[]::stack;} | _ when not (Unif.Ty.type_is_unifiable (T.ty t)) || Type.is_fun (T.ty t) -> Some {cur_term=t; stack=[]::stack;} (* opaque constant/partial application *) | T.App (f, _) when (T.is_var f) -> Some {cur_term=t; stack=[]::stack;} (* higher-order term *) | T.App (_, l) -> Some {cur_term=t; stack=l::stack;} type view_head = | As_star | As_app of ID.t * T.t list let view_head (t:T.t) : view_head = if not (T.is_app t) || not (Unif.Ty.type_is_unifiable (T.ty t)) || Type.is_fun (T.ty t) || T.is_ho_app t then As_star else ( let s,l = T.as_app t in begin match T.view s with | T.Const id -> As_app (id, l) | _ -> As_star end ) let rec next_rec stack = match stack with | [] -> None | []::stack' -> next_rec stack' | (t::next')::stack' -> open_term ~stack:(next'::stack') t let skip iter = match iter.stack with | [] -> None | _next::stack' -> next_rec stack' let next iter = next_rec iter.stack (* Iterate on a term *) let iterate term = open_term ~stack:[] term (** {2 Unix index} *) module Make(E : Index.EQUATION) = struct module E = E type rhs = E.rhs module Leaf = Index.MakeLeaf(E) type t = { star : t option; (* by variable *) map : t ID.Map.t; (* by symbol *) leaf : Leaf.t; (* leaves *) } (* The discrimination tree *) let empty () = {map=ID.Map.empty; star=None; leaf=Leaf.empty;} let is_empty n = n.star = None && ID.Map.is_empty n.map && Leaf.is_empty n.leaf exception NoSuchTrie let find_sub map key = try ID.Map.find key map with Not_found -> raise NoSuchTrie (** get/add/remove the leaf for the given term. The continuation k takes the leaf, and returns a leaf option that replaces the old leaf. This function returns the new trie. *) let goto_leaf trie t k = (* the root of the tree *) let root = trie in (* function to go to the given leaf, building it if needed. [iter] is an iterator on the current subterm *) let rec goto trie iter rebuild = match iter with | None -> begin match k trie.leaf with | leaf' when leaf' == trie.leaf -> root (* no change, return same tree *) | leaf' -> rebuild {trie with leaf=leaf'; } end | Some i -> match view_head i.cur_term with | As_star -> let subtrie = match trie.star with | None -> empty () | Some trie' -> trie' in let rebuild subtrie = if is_empty subtrie then rebuild {trie with star=None; } else rebuild {trie with star=Some subtrie ;} in goto subtrie (next i) rebuild | As_app (s, _) -> let subtrie = try find_sub trie.map s with NoSuchTrie -> empty () in let rebuild subtrie = if is_empty subtrie then rebuild {trie with map=ID.Map.remove s trie.map; } else rebuild {trie with map=ID.Map.add s subtrie trie.map ;} in goto subtrie (next i) rebuild in goto trie (iterate t) (fun t -> t) let add trie eqn = let t, _, _ = E.extract eqn in let k leaf = Leaf.add leaf t eqn in goto_leaf trie t k let remove trie eqn = let t, _, _ = E.extract eqn in let k leaf = Leaf.remove leaf t eqn in goto_leaf trie t k let add_seq dt seq = Iter.fold add dt seq let add_list dt = List.fold_left add dt let remove_seq dt seq = Iter.fold remove dt seq let retrieve ?(subst=S.empty) ~sign dt t k = Util.enter_prof prof_npdtree_retrieve; (* extended callback *) let k' (t', eqn, subst) = let _, r, sign' = E.extract eqn in if sign = sign' then k (t', r, eqn, subst) in (* recursive traversal of the trie, following paths compatible with t *) let rec traverse trie iter = match iter with | None -> Util.exit_prof prof_npdtree_retrieve; Leaf.fold_match ~subst (Scoped.set dt trie.leaf) t k'; Util.enter_prof prof_npdtree_retrieve; | Some i -> match view_head i.cur_term with | As_star -> begin match trie.star with | None -> () | Some subtrie -> traverse subtrie (next i) (* match "*" against "*" *) end | As_app (s, _) -> begin try let subtrie = find_sub trie.map s in traverse subtrie (next i) with NoSuchTrie -> () end; begin match trie.star with | None -> () | Some subtrie -> traverse subtrie (skip i) (* skip subterm *) end in try traverse (fst dt) (iterate (fst t)); Util.exit_prof prof_npdtree_retrieve; with e -> Util.exit_prof prof_npdtree_retrieve; raise e (** iterate on all (term -> value) in the tree *) let rec iter dt k = Leaf.iter dt.leaf k; begin match dt.star with | None -> () | Some trie' -> iter trie' k end; ID.Map.iter (fun _ trie' -> iter trie' k) dt.map let size dt = let n = ref 0 in iter dt (fun _ _ -> incr n); !n let _as_graph = CCGraph.make (fun t -> let prefix s = match t.star with | None -> s | Some t' -> Iter.cons ("*", t') s and s2 = ID.Map.to_seq t.map |> Iter.map (fun (sym, t') -> ID.to_string sym, t') in prefix s2) let to_dot out t = let pp = CCGraph.Dot.pp ~eq:(==) ~tbl:(CCGraph.mk_table ~eq:(==) ~hash:Hashtbl.hash 128) ~attrs_v:(fun t -> let len = Leaf.size t.leaf in let shape = if len>0 then "box" else "circle" in [`Shape shape; `Label (string_of_int len)]) ~attrs_e:(fun e -> [`Label e]) ~name:"NPDtree" ~graph:_as_graph in Format.fprintf out "@[<2>%a@]@." pp t; () end (** {2 General purpose index} *) module SIMap = Iter.Map.Make(struct type t = ID.t * int let compare (s1,i1) (s2,i2) = if i1 = i2 then ID.compare s1 s2 else i1-i2 end) module MakeTerm(X : Set.OrderedType) = struct module Leaf = Index.MakeLeaf(X) type elt = X.t type t = { star : t option; (* by variable *) map : t SIMap.t; (* by symbol+arity *) leaf : Leaf.t; (* leaves *) } (** The discrimination tree *) let empty () = {map=SIMap.empty; star=None; leaf=Leaf.empty;} let is_empty n = n.star = None && SIMap.is_empty n.map && Leaf.is_empty n.leaf exception NoSuchTrie let find_sub map key = try SIMap.find key map with Not_found -> raise NoSuchTrie (** get/add/remove the leaf for the given term. The continuation k takes the leaf, and returns a leaf option that replaces the old leaf. This function returns the new trie. *) let goto_leaf trie t k = (* the root of the tree *) let root = trie in (* function to go to the given leaf, building it if needed. *) let rec goto trie iter rebuild = match iter with | None -> begin match k trie.leaf with | leaf' when leaf' == trie.leaf -> root (* no change, return same tree *) | leaf' -> rebuild {trie with leaf=leaf'; } end | Some i -> match view_head i.cur_term with | As_star -> let subtrie = match trie.star with | None -> empty () | Some trie' -> trie' in let rebuild subtrie = if is_empty subtrie then rebuild {trie with star=None; } else rebuild {trie with star=Some subtrie ;} in goto subtrie (next i) rebuild | As_app (s,l) -> let arity = List.length l in let subtrie = try find_sub trie.map (s,arity) with NoSuchTrie -> empty () in let rebuild subtrie = if is_empty subtrie then rebuild {trie with map=SIMap.remove (s,arity) trie.map; } else rebuild {trie with map=SIMap.add (s,arity) subtrie trie.map ;} in goto subtrie (next i) rebuild in goto trie (iterate t) (fun t -> t) let add trie t data = let k leaf = Leaf.add leaf t data in goto_leaf trie t k let add_ trie = CCFun.uncurry (add trie) let add_seq = Iter.fold add_ let add_list = List.fold_left add_ let remove trie t data = let k leaf = Leaf.remove leaf t data in goto_leaf trie t k let remove_ trie = CCFun.uncurry (remove trie) let remove_seq dt seq = Iter.fold remove_ dt seq let remove_list dt seq = List.fold_left remove_ dt seq (* skip one term in the tree. Calls [k] with [acc] on corresponding subtries. *) let skip_tree trie k = (* [n]: number of branches to skip (corresponding to subterms) *) let rec skip trie n k = if n = 0 then k trie else ( begin match trie.star with | None -> () | Some trie' -> skip trie' (n-1) k end; SIMap.iter (fun (_,arity) trie' -> skip trie' (n+arity-1) k) trie.map ) in skip trie 1 k let retrieve_unifiables ?(subst=Unif_subst.empty) dt t k = Util.enter_prof prof_npdtree_term_unify; (* recursive traversal of the trie, following paths compatible with t *) let rec traverse trie iter = match iter with | None -> Util.exit_prof prof_npdtree_term_unify; Leaf.fold_unify ~subst (Scoped.set dt trie.leaf) t k; Util.enter_prof prof_npdtree_term_unify; | Some i -> match view_head i.cur_term with | As_star -> (* skip one term in all branches of the trie *) skip_tree trie (fun subtrie -> traverse subtrie (next i)) | As_app (s,l) -> let arity = List.length l in begin try let subtrie = SIMap.find (s,arity) trie.map in traverse subtrie (next i) with Not_found -> () end; begin match trie.star with | None -> () | Some subtrie -> traverse subtrie (skip i) (* skip subterm of [t] *) end in try traverse (fst dt) (iterate (fst t)); Util.exit_prof prof_npdtree_term_unify; with e -> Util.exit_prof prof_npdtree_term_unify; raise e let retrieve_generalizations ?(subst=S.empty) dt t k = Util.enter_prof prof_npdtree_term_generalizations; (* recursive traversal of the trie, following paths compatible with t *) let rec traverse trie iter = match iter with | None -> Util.exit_prof prof_npdtree_term_generalizations; Leaf.fold_match ~subst (Scoped.set dt trie.leaf) t k; Util.enter_prof prof_npdtree_term_generalizations; | Some i -> match view_head i.cur_term with | As_star -> begin match trie.star with | None -> () | Some subtrie -> traverse subtrie (next i) (* match "*" against "*" only *) end | As_app (s,l) -> let arity = List.length l in begin try let subtrie = SIMap.find (s,arity) trie.map in traverse subtrie (next i) with Not_found -> () end; begin match trie.star with | None -> () | Some subtrie -> traverse subtrie (skip i) (* skip subterm *) end in try traverse (fst dt) (iterate (fst t)); Util.exit_prof prof_npdtree_term_generalizations; with e -> Util.exit_prof prof_npdtree_term_generalizations; raise e let retrieve_specializations ?(subst=S.empty) dt t k = Util.enter_prof prof_npdtree_term_specializations; (* recursive traversal of the trie, following paths compatible with t *) let rec traverse trie iter = match iter with | None -> Util.exit_prof prof_npdtree_term_specializations; Leaf.fold_matched ~subst (Scoped.set dt trie.leaf) t k; Util.enter_prof prof_npdtree_term_specializations; | Some i -> match view_head i.cur_term with | As_star -> (* match * against any subterm *) skip_tree trie (fun subtrie -> traverse subtrie (next i)) | As_app (s,l) -> (* only same symbol *) let arity = List.length l in begin try let subtrie = SIMap.find (s,arity) trie.map in traverse subtrie (next i) with Not_found -> () end in try traverse (fst dt) (iterate (fst t)); Util.exit_prof prof_npdtree_term_specializations; with e -> Util.exit_prof prof_npdtree_term_specializations; raise e (** iterate on all (term -> value) in the tree *) let rec iter dt k = Leaf.iter dt.leaf k; begin match dt.star with | None -> () | Some trie' -> iter trie' k end; SIMap.iter (fun _ trie' -> iter trie' k) dt.map let rec fold dt k acc = let acc = Leaf.fold dt.leaf acc k in let acc = match dt.star with | None -> acc | Some trie' -> fold trie' k acc in SIMap.fold (fun _ trie' acc -> fold trie' k acc) dt.map acc let size dt = let n = ref 0 in iter dt (fun _ _ -> incr n); !n let name = "npdtree" let _as_graph = CCGraph.make (fun t -> let prefix s = match t.star with | None -> s | Some t' -> Iter.cons ("*", t') s and s2 = SIMap.to_seq t.map |> Iter.map (fun ((sym,i), t') -> let label = CCFormat.sprintf "%a/%d" ID.pp sym i in label, t') in prefix s2) (* TODO: print leaf itself *) let to_dot _ out t = Util.debugf 2 "@[<2>print graph of size %d@]" (fun k->k (size t)); let pp = CCGraph.Dot.pp ~eq:(==) ~tbl:(CCGraph.mk_table ~eq:(==) ~hash:Hashtbl.hash 128) ~attrs_v:(fun t -> let len = Leaf.size t.leaf in let shape = if len>0 then "box" else "circle" in [`Shape shape; `Label (string_of_int len)]) ~attrs_e:(fun e -> [`Label e]) ~name:"NPDtree" ~graph:_as_graph in Format.fprintf out "@[<2>%a@]@." pp t; () end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>