-
Notifications
You must be signed in to change notification settings - Fork 6
hashconsing example using the hashconsing library #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
craff
wants to merge
2
commits into
master
Choose a base branch
from
hashcons2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (tests | ||
| (names hashcons2) | ||
| (libraries bindlib hashcons)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| (** Experiment with hashconsing, using Conchon & Filliâtre's Hashcons | ||
| library *) | ||
|
|
||
| open Bindlib | ||
| open Hashcons | ||
|
|
||
| type term = term_aux hash_consed | ||
|
|
||
| and term_aux = | ||
| | TVar of term var | ||
| | TApp of term * term | ||
| | TLam of (term, term) binder | ||
| | HVar of int | ||
|
|
||
| type t = term | ||
|
|
||
| module type HashedWithTbl = sig | ||
| include HashedType | ||
| type tbl | ||
| val hashcons : t -> t hash_consed | ||
| val init : int -> unit | ||
| end | ||
|
|
||
| module rec H : HashedWithTbl with type t = HH.key and type tbl = HH.t = struct | ||
| type t = term_aux | ||
| type tbl = HH.t | ||
| let tbl = ref None | ||
| let get_tbl () = match !tbl with | ||
| | None -> assert false | ||
| | Some tbl -> tbl | ||
|
|
||
| let init n = if !tbl = None then tbl := Some (HH.create n) | ||
|
|
||
| let hashcons t = HH.hashcons (get_tbl ()) t | ||
|
|
||
| let canonical_var f = | ||
| let n = if binder_occur f then binder_rank f + 1 else 0 in | ||
| HH.hashcons (get_tbl ()) (HVar n) | ||
|
|
||
| let hash_binder f = | ||
| Hashtbl.hash (binder_occur f, (subst f (canonical_var f)).hkey) | ||
|
|
||
| let hash t = | ||
| match t with | ||
| | HVar(n) -> Hashtbl.hash (`HVar, n) | ||
| | TVar(x) -> hash_var x | ||
| | TApp(t,u) -> Hashtbl.hash (`TApp, t.hkey, u.hkey) | ||
| | TLam(b) -> Hashtbl.hash (`TLam, hash_binder b) | ||
|
|
||
| let equal_binders b1 b2 = | ||
| binder_rank b1 = binder_rank b2 && | ||
| binder_occur b1 = binder_occur b2 && | ||
| (subst b1 (canonical_var b1)).tag = | ||
| (subst b2 (canonical_var b2)).tag | ||
|
|
||
| let equal t1 t2 = | ||
| t1 == t2 || | ||
| match (t1, t2) with | ||
| | (HVar(n1) , HVar(n2) ) -> n1 = n2 | ||
| | (TVar(x1) , TVar(x2) ) -> eq_vars x1 x2 | ||
| | (TApp(t1,u1), TApp(t2,u2)) -> | ||
| t1.tag = t2.tag && u1.tag = u2.tag | ||
| | (TLam(b1) , TLam(b2) ) -> equal_binders b1 b2 | ||
| | (_ , _ ) -> false | ||
| end | ||
|
|
||
| and HH : (S with type key = term_aux) = Make(H) | ||
|
|
||
| let _ = H.init 4096 | ||
|
|
||
| let hashcons = H.hashcons | ||
|
|
||
| let mkfree : term var -> term = fun x -> | ||
| hashcons (TVar(x)) | ||
|
|
||
| let var : term var -> term box = | ||
| box_var (* call mkfree and therefore hashcons when unboxing *) | ||
|
|
||
| let lam : string -> (term box -> term box) -> term box = fun x f -> | ||
| let x = Bindlib.new_var mkfree x in | ||
| box_apply hashcons (box_apply (fun x -> TLam x) (bind_var x (f (var x)))) | ||
|
|
||
| let app : term box -> term box -> term box = fun t u -> | ||
| box_apply hashcons (box_apply2 (fun t u -> TApp(t,u)) t u) | ||
|
|
||
| (* short cut for closed term application *) | ||
| let _App(t1,t2) = hashcons(TApp(t1,t2)) | ||
|
|
||
| let rec eval t = | ||
| match t.node with | ||
| | TVar(_) -> failwith "open term" | ||
| | HVar(_) -> failwith "invalid term" | ||
| | TLam(_) -> t | ||
| | TApp(t1,t2) -> | ||
| let t1 = eval t1 in | ||
| let t2 = eval t2 in | ||
| let b = match t1.node with TLam(b) -> b | _ -> failwith "ill-typed" in | ||
| eval (subst b t2) | ||
|
|
||
| (* Tests. *) | ||
|
|
||
| let idt = unbox (lam "x" (fun x -> x)) | ||
| let delta = unbox (lam "x" (fun x -> app x x)) | ||
| let idt' = unbox (lam "y" (fun x -> x)) | ||
| let delta' = unbox (lam "y" (fun x -> app x x)) | ||
| let zero = unbox (lam "f" (fun _ -> lam "x" (fun x -> x))) | ||
| let succ = unbox (lam "n" (fun n -> lam "f" (fun f -> lam "x" (fun x -> | ||
| app (app n f) (app f x))))) | ||
| let two = unbox (lam "f" (fun f -> lam "x" (fun x -> app f (app f x)))) | ||
| let add = unbox (lam "n" (fun n -> lam "m" (fun m -> lam "f" (fun f -> | ||
| lam "x" (fun x -> app (app n f) (app (app m f) x)))))) | ||
|
|
||
| let dog = eval (_App(_App(_App(two,two),succ),zero)) | ||
| let four = eval (_App(_App(_App(_App(add,two),two),succ),zero)) | ||
|
|
||
| let _ = assert (idt.tag = idt'.tag) | ||
| let _ = assert (idt.node == idt'.node) | ||
|
|
||
| let _ = assert (delta.tag = delta'.tag && | ||
| delta.node == delta'.node) | ||
|
|
||
| let idt'' = eval (_App(delta, _App(delta', idt))) | ||
|
|
||
| let rec print_in ctxt oc t = | ||
| match t.node with | ||
| | TVar(x) -> | ||
| Printf.fprintf oc "%s" (name_of x) | ||
| | TLam(b) -> | ||
| let (x,t,ctxt) = Bindlib.unbind_in ctxt b in | ||
| Printf.fprintf oc "λ%s.%a" (name_of x) (print_in ctxt) t | ||
| | TApp(t,u) -> | ||
| Printf.fprintf oc "(%a %a)" (print_in ctxt) t (print_in ctxt) u | ||
| | HVar(n) -> | ||
| Printf.fprintf oc "{%i}" n | ||
|
|
||
| let print_closed = print_in empty_ctxt | ||
|
|
||
| let _ = | ||
| Printf.printf "%a\n%!" print_closed idt; | ||
| Printf.printf "%a\n%!" print_closed idt'; | ||
| Printf.printf "%a\n%!" print_closed delta; | ||
| Printf.printf "%a\n%!" print_closed delta'; | ||
| Printf.printf "%a\n%!" print_closed idt''; | ||
| Printf.printf "%a\n%!" print_closed two; | ||
| Printf.printf "%a\n%!" print_closed four; | ||
| Printf.printf "%a\n%!" print_closed dog | ||
|
|
||
| let _ = assert (idt.tag = idt''.tag) | ||
| let _ = assert (idt.node == idt''.node) | ||
|
|
||
| let _ = assert (four.tag = dog.tag) | ||
| let _ = assert (four.node == dog.node) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably add a version bound.