Advertisement
Guest User

Untitled

a guest
Oct 18th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.33 KB | None | 0 0
  1. module type Symbol = sig
  2.   type t
  3.  
  4.   val symbol : string -> t
  5.   val name : t -> string
  6.  
  7.   type 'a table
  8.   val empty : 'a table
  9.   val enter : 'a table -> t -> 'a -> 'a table
  10.   val look : 'a table -> t -> 'a option
  11. end
  12.  
  13. module Symbol : Symbol = struct
  14.  
  15.   open Core.Std
  16.  
  17.   type t = int
  18.  
  19.   let nextsym = ref 0
  20.  
  21.   let symtbl = String.Table.create ()
  22.   let inv_symtbl = Int.Table.create ()
  23.   let symbol s = match Hashtbl.find symtbl s with
  24.     | Some x -> x
  25.     | None ->
  26.       let sym = !nextsym
  27.       in (nextsym := sym + 1;
  28.           Hashtbl.set symtbl ~key:s ~data:sym;
  29.           Hashtbl.set inv_symtbl ~key:sym ~data:s;
  30.           sym)
  31.  
  32.   let name sym = match Hashtbl.find inv_symtbl sym with
  33.     | Some x -> x
  34.     | None -> ""
  35.  
  36.   type 'a table = 'a Int.Table.t
  37.   let empty : 'a table = Int.Table.create () ~size: 4
  38.   let enter tbl s data = Hashtbl.set tbl ~key:s ~data
  39.   let look tbl s = Hashtbl.find tbl s
  40. end
  41.  
  42. (*
  43.  
  44. Error: Signature mismatch:
  45. module type Symbol =
  46.        ...
  47.   sig
  48.        Values do not match:
  49.     type t
  50.          val empty : '_a table
  51.     val symbol : string -> t
  52.        is not included in
  53.     val name : t -> string
  54.          val empty : 'a table
  55.     type 'a table
  56.     val empty : 'a table
  57.     val enter : 'a table -> t -> 'a -> 'a table
  58.     val look : 'a table -> t -> 'a option
  59.   end
  60.  
  61. *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement