PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel/names.ml

http://github.com/mzp/coq-ruby
OCaml | 344 lines | 249 code | 75 blank | 20 comment | 20 complexity | 77f60e51bbb3045647f9f931c2641420 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. (************************************************************************)
  2. (* v * The Coq Proof Assistant / The Coq Development Team *)
  3. (* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
  4. (* \VV/ **************************************************************)
  5. (* // * This file is distributed under the terms of the *)
  6. (* * GNU Lesser General Public License Version 2.1 *)
  7. (************************************************************************)
  8. (* $Id: names.ml 11750 2009-01-05 20:47:34Z herbelin $ *)
  9. open Pp
  10. open Util
  11. (*s Identifiers *)
  12. type identifier = string
  13. let id_ord = Pervasives.compare
  14. let id_of_string s = check_ident_soft s; String.copy s
  15. let string_of_id id = String.copy id
  16. (* Hash-consing of identifier *)
  17. module Hident = Hashcons.Make(
  18. struct
  19. type t = string
  20. type u = string -> string
  21. let hash_sub hstr id = hstr id
  22. let equal id1 id2 = id1 == id2
  23. let hash = Hashtbl.hash
  24. end)
  25. module IdOrdered =
  26. struct
  27. type t = identifier
  28. let compare = id_ord
  29. end
  30. module Idset = Set.Make(IdOrdered)
  31. module Idmap = Map.Make(IdOrdered)
  32. module Idpred = Predicate.Make(IdOrdered)
  33. (* Names *)
  34. type name = Name of identifier | Anonymous
  35. (* Dirpaths are lists of module identifiers. The actual representation
  36. is reversed to optimise sharing: Coq.A.B is ["B";"A";"Coq"] *)
  37. type module_ident = identifier
  38. type dir_path = module_ident list
  39. module ModIdOrdered =
  40. struct
  41. type t = identifier
  42. let compare = Pervasives.compare
  43. end
  44. module ModIdmap = Map.Make(ModIdOrdered)
  45. let make_dirpath x = x
  46. let repr_dirpath x = x
  47. let empty_dirpath = []
  48. let string_of_dirpath = function
  49. | [] -> "<>"
  50. | sl -> String.concat "." (List.map string_of_id (List.rev sl))
  51. let u_number = ref 0
  52. type uniq_ident = int * string * dir_path
  53. let make_uid dir s = incr u_number;(!u_number,String.copy s,dir)
  54. let debug_string_of_uid (i,s,p) =
  55. "<"(*^string_of_dirpath p ^"#"^*) ^ s ^"#"^ string_of_int i^">"
  56. let string_of_uid (i,s,p) =
  57. string_of_dirpath p ^"."^s
  58. module Umap = Map.Make(struct
  59. type t = uniq_ident
  60. let compare = Pervasives.compare
  61. end)
  62. type label = string
  63. type mod_self_id = uniq_ident
  64. let make_msid = make_uid
  65. let repr_msid (n, id, dp) = (n, id, dp)
  66. let debug_string_of_msid = debug_string_of_uid
  67. let refresh_msid (_,s,dir) = make_uid dir s
  68. let string_of_msid = string_of_uid
  69. let id_of_msid (_,s,_) = s
  70. let label_of_msid (_,s,_) = s
  71. type mod_bound_id = uniq_ident
  72. let make_mbid = make_uid
  73. let repr_mbid (n, id, dp) = (n, id, dp)
  74. let debug_string_of_mbid = debug_string_of_uid
  75. let string_of_mbid = string_of_uid
  76. let id_of_mbid (_,s,_) = s
  77. let label_of_mbid (_,s,_) = s
  78. let mk_label l = l
  79. let string_of_label = string_of_id
  80. let id_of_label l = l
  81. let label_of_id id = id
  82. module Labset = Idset
  83. module Labmap = Idmap
  84. type module_path =
  85. | MPfile of dir_path
  86. | MPbound of mod_bound_id
  87. | MPself of mod_self_id
  88. | MPdot of module_path * label
  89. let rec check_bound_mp = function
  90. | MPbound _ -> true
  91. | MPdot(mp,_) ->check_bound_mp mp
  92. | _ -> false
  93. let rec string_of_mp = function
  94. | MPfile sl -> "MPfile (" ^ string_of_dirpath sl ^ ")"
  95. | MPbound uid -> string_of_uid uid
  96. | MPself uid -> string_of_uid uid
  97. | MPdot (mp,l) -> string_of_mp mp ^ "." ^ string_of_label l
  98. (* we compare labels first if both are MPdots *)
  99. let rec mp_ord mp1 mp2 = match (mp1,mp2) with
  100. MPdot(mp1,l1), MPdot(mp2,l2) ->
  101. let c = Pervasives.compare l1 l2 in
  102. if c<>0 then
  103. c
  104. else
  105. mp_ord mp1 mp2
  106. | _,_ -> Pervasives.compare mp1 mp2
  107. module MPord = struct
  108. type t = module_path
  109. let compare = mp_ord
  110. end
  111. module MPset = Set.Make(MPord)
  112. module MPmap = Map.Make(MPord)
  113. (* Kernel names *)
  114. type kernel_name = module_path * dir_path * label
  115. let make_kn mp dir l = (mp,dir,l)
  116. let repr_kn kn = kn
  117. let modpath kn =
  118. let mp,_,_ = repr_kn kn in mp
  119. let label kn =
  120. let _,_,l = repr_kn kn in l
  121. let string_of_kn (mp,dir,l) =
  122. string_of_mp mp ^ "#" ^ string_of_dirpath dir ^ "#" ^ string_of_label l
  123. let pr_kn kn = str (string_of_kn kn)
  124. let kn_ord kn1 kn2 =
  125. let mp1,dir1,l1 = kn1 in
  126. let mp2,dir2,l2 = kn2 in
  127. let c = Pervasives.compare l1 l2 in
  128. if c <> 0 then
  129. c
  130. else
  131. let c = Pervasives.compare dir1 dir2 in
  132. if c<>0 then
  133. c
  134. else
  135. MPord.compare mp1 mp2
  136. module KNord = struct
  137. type t = kernel_name
  138. let compare =kn_ord
  139. end
  140. module KNmap = Map.Make(KNord)
  141. module KNpred = Predicate.Make(KNord)
  142. module KNset = Set.Make(KNord)
  143. module Cmap = KNmap
  144. module Cpred = KNpred
  145. module Cset = KNset
  146. let default_module_name = "If you see this, it's a bug"
  147. let initial_dir = make_dirpath [default_module_name]
  148. let initial_msid = (make_msid initial_dir "If you see this, it's a bug")
  149. let initial_path = MPself initial_msid
  150. type variable = identifier
  151. type constant = kernel_name
  152. type mutual_inductive = kernel_name
  153. type inductive = mutual_inductive * int
  154. type constructor = inductive * int
  155. let constant_of_kn kn = kn
  156. let make_con mp dir l = (mp,dir,l)
  157. let repr_con con = con
  158. let string_of_con = string_of_kn
  159. let con_label = label
  160. let pr_con = pr_kn
  161. let con_modpath = modpath
  162. let mind_modpath = modpath
  163. let ind_modpath ind = mind_modpath (fst ind)
  164. let constr_modpath c = ind_modpath (fst c)
  165. let ith_mutual_inductive (kn,_) i = (kn,i)
  166. let ith_constructor_of_inductive ind i = (ind,i)
  167. let inductive_of_constructor (ind,i) = ind
  168. let index_of_constructor (ind,i) = i
  169. module InductiveOrdered = struct
  170. type t = inductive
  171. let compare (spx,ix) (spy,iy) =
  172. let c = ix - iy in if c = 0 then KNord.compare spx spy else c
  173. end
  174. module Indmap = Map.Make(InductiveOrdered)
  175. module ConstructorOrdered = struct
  176. type t = constructor
  177. let compare (indx,ix) (indy,iy) =
  178. let c = ix - iy in if c = 0 then InductiveOrdered.compare indx indy else c
  179. end
  180. module Constrmap = Map.Make(ConstructorOrdered)
  181. (* Better to have it here that in closure, since used in grammar.cma *)
  182. type evaluable_global_reference =
  183. | EvalVarRef of identifier
  184. | EvalConstRef of constant
  185. (* Hash-consing of name objects *)
  186. module Hname = Hashcons.Make(
  187. struct
  188. type t = name
  189. type u = identifier -> identifier
  190. let hash_sub hident = function
  191. | Name id -> Name (hident id)
  192. | n -> n
  193. let equal n1 n2 =
  194. match (n1,n2) with
  195. | (Name id1, Name id2) -> id1 == id2
  196. | (Anonymous,Anonymous) -> true
  197. | _ -> false
  198. let hash = Hashtbl.hash
  199. end)
  200. module Hdir = Hashcons.Make(
  201. struct
  202. type t = dir_path
  203. type u = identifier -> identifier
  204. let hash_sub hident d = List.map hident d
  205. let rec equal d1 d2 = match (d1,d2) with
  206. | [],[] -> true
  207. | id1::d1,id2::d2 -> id1 == id2 & equal d1 d2
  208. | _ -> false
  209. let hash = Hashtbl.hash
  210. end)
  211. module Huniqid = Hashcons.Make(
  212. struct
  213. type t = uniq_ident
  214. type u = (string -> string) * (dir_path -> dir_path)
  215. let hash_sub (hstr,hdir) (n,s,dir) = (n,hstr s,hdir dir)
  216. let equal (n1,s1,dir1) (n2,s2,dir2) = n1 = n2 & s1 = s2 & dir1 == dir2
  217. let hash = Hashtbl.hash
  218. end)
  219. module Hmod = Hashcons.Make(
  220. struct
  221. type t = module_path
  222. type u = (dir_path -> dir_path) * (uniq_ident -> uniq_ident) *
  223. (string -> string)
  224. let rec hash_sub (hdir,huniqid,hstr as hfuns) = function
  225. | MPfile dir -> MPfile (hdir dir)
  226. | MPbound m -> MPbound (huniqid m)
  227. | MPself m -> MPself (huniqid m)
  228. | MPdot (md,l) -> MPdot (hash_sub hfuns md, hstr l)
  229. let rec equal d1 d2 = match (d1,d2) with
  230. | MPfile dir1, MPfile dir2 -> dir1 == dir2
  231. | MPbound m1, MPbound m2 -> m1 == m2
  232. | MPself m1, MPself m2 -> m1 == m2
  233. | MPdot (mod1,l1), MPdot (mod2,l2) -> equal mod1 mod2 & l1 = l2
  234. | _ -> false
  235. let hash = Hashtbl.hash
  236. end)
  237. module Hkn = Hashcons.Make(
  238. struct
  239. type t = kernel_name
  240. type u = (module_path -> module_path)
  241. * (dir_path -> dir_path) * (string -> string)
  242. let hash_sub (hmod,hdir,hstr) (md,dir,l) = (hmod md, hdir dir, hstr l)
  243. let equal (mod1,dir1,l1) (mod2,dir2,l2) =
  244. mod1 == mod2 && dir1 == dir2 && l1 == l2
  245. let hash = Hashtbl.hash
  246. end)
  247. let hcons_names () =
  248. let hstring = Hashcons.simple_hcons Hashcons.Hstring.f () in
  249. let hident = Hashcons.simple_hcons Hident.f hstring in
  250. let hname = Hashcons.simple_hcons Hname.f hident in
  251. let hdir = Hashcons.simple_hcons Hdir.f hident in
  252. let huniqid = Hashcons.simple_hcons Huniqid.f (hstring,hdir) in
  253. let hmod = Hashcons.simple_hcons Hmod.f (hdir,huniqid,hstring) in
  254. let hkn = Hashcons.simple_hcons Hkn.f (hmod,hdir,hstring) in
  255. (hkn,hkn,hdir,hname,hident,hstring)
  256. (*******)
  257. type transparent_state = Idpred.t * Cpred.t
  258. let empty_transparent_state = (Idpred.empty, Cpred.empty)
  259. let full_transparent_state = (Idpred.full, Cpred.full)
  260. let var_full_transparent_state = (Idpred.full, Cpred.empty)
  261. let cst_full_transparent_state = (Idpred.empty, Cpred.full)
  262. type 'a tableKey =
  263. | ConstKey of constant
  264. | VarKey of identifier
  265. | RelKey of 'a
  266. type inv_rel_key = int (* index in the [rel_context] part of environment
  267. starting by the end, {\em inverse}
  268. of de Bruijn indice *)
  269. type id_key = inv_rel_key tableKey