Commit 6149608a authored by Kim Nguyễn's avatar Kim Nguyễn
Browse files

Add a memoization table to remember previously applied substitutions.

parent 3aab1b01
...@@ -2814,12 +2814,40 @@ module Positive = struct ...@@ -2814,12 +2814,40 @@ module Positive = struct
apply_subst ~subst:subst ~after:(fun y -> define x y;x) t apply_subst ~subst:subst ~after:(fun y -> define x y;x) t
(* Pre-condition : alpha \not\in \delta *) (* Pre-condition : alpha \not\in \delta *)
module MemoSubst = Hashtbl.Make (struct
type t = descr * (Var.t * descr) list
let hash (t, l) =
List.fold_left
(fun acc (v,t) -> Var.hash v + 17 * Descr.hash t + 31 * acc)
(Descr.hash t) l
let equal (t1, l1) (t2, l2) =
Descr.equal t1 t2 && (try List.for_all2 (fun (v1, t1) (v2, t2) ->
Var.equal v1 v2 && Descr.equal t1 t2) l1 l2 with _ -> false)
end)
let memo_subst = MemoSubst.create 17
let () = at_exit (fun () ->
Format.eprintf "%i@\n%!" (MemoSubst.length memo_subst))
let substitute t (alpha,s) = let substitute t (alpha,s) =
let k = (t, [(alpha, s)]) in
try
MemoSubst.find memo_subst k
with Not_found ->
let r =
let vs = ty s in let vs = ty s in
let subst d = if Var.equal d alpha then vs else var d in let subst d = if Var.equal d alpha then vs else var d in
apply_subst ~subst:subst t apply_subst ~subst:subst t
in
MemoSubst.add memo_subst k r; r
let substitute_list t l = let substitute_list t l =
let k = (t,l) in
try
MemoSubst.find memo_subst k
with Not_found ->
let r =
let subst d = let subst d =
try try
ty ty
...@@ -2828,6 +2856,9 @@ module Positive = struct ...@@ -2828,6 +2856,9 @@ module Positive = struct
with Not_found -> var d with Not_found -> var d
in in
apply_subst ~subst:subst t apply_subst ~subst:subst t
in
MemoSubst.add memo_subst k r; r
let substitute_free delta t = let substitute_free delta t =
let h = Hashtbl.create 17 in let h = Hashtbl.create 17 in
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment