2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department, University of Bologna, Italy.
6 ||T|| HELM is free software; you can redistribute it and/or
7 ||A|| modify it under the terms of the GNU General Public License
8 \ / version 2 or (at your option) any later version.
9 \ / This software is distributed as is, NO WARRANTY.
10 V_______________________________________________________________ *)
21 module E = NCicEnvironment
22 module T = NCicTypeChecker
28 type global = (string, int) H.t
30 type local = (string * int) list
33 g: global; (* global context for alpha-conversion *)
34 d: local; (* local context for alpha-conversion *)
35 c: C.context; (* local context for kernel calls *)
38 (* internal functions *******************************************************)
41 X.error ("alpha: malformed term: " ^ s)
44 X.log ("alpha: ok " ^ s)
47 g = H.create 11; d = []; c = [];
51 let r1, r2 = S.sscanf r "%s@_%s" X.id2 in
52 if r2 = "" then r1 else trim r1
57 if j >=0 && s.[j] >= '0' && s.[j] <= '9' then aux j (succ l) else i, l
59 let i, l = aux (N.length s) 0 in
60 let s1, s2 = N.sub s 0 i, N.sub s i l in
61 let s1 = if s1 = "" then "_" else s1 in
62 s1, if l = 0 then G.nan else int_of_string s2
64 let rec strip = function
66 | C.Prod (_, _, t) -> strip t
69 let get_constant c t = match strip (K.whd c t) with
72 | C.Sort (C.Type [`Type, u]) ->
73 P.sprintf "Type[%s]" (U.string_of_uri u)
74 | C.Sort (C.Type [`CProp, u]) ->
75 P.sprintf "CProp[%s]" (U.string_of_uri u)
77 let s, _ = K.resolve_reference c in
82 let rec aux = function
85 if s = a && (r = b || r = c) then c else aux tl
89 let type_check r c w =
90 let s0 = get_constant c w in
91 let r0 = read !G.alpha_type s0 r in
92 if r0 <> "" then r0 else
93 let s1 = get_constant c (K.typeof c w) in
94 let r0 = read !G.alpha_sort s1 r in
95 if r0 <> "" then r0 else begin
97 X.log (P.sprintf "alpha: not found %s: type \"%s\" sort \"%s\"" r s0 s1);
101 let rec get r = function
104 if fst r = h && snd r <= d then h, succ d else get r tl
107 if i < 0 then s else P.sprintf "%s%u" s i
109 let local_alpha st s w t =
110 if K.does_not_occur K.fst_var st.c t then G.dno_id, G.nan else
111 let r, i = split (trim s) in
112 get (type_check r st.c w, i) st.d
114 let global_apha st s =
116 let i = H.find st.g s in
117 H.replace st.g s (succ i);
118 P.sprintf "%s.%u" s i
124 let r = local_alpha st s w t in
126 r, if G.is_global_id s then global_apha st s else s
128 let add_name r d = r :: d
130 let rec proc_term st t = match t with
137 let tts = proc_terms st ts in
139 | C.Match (w, u, v, ts) ->
140 let uu = proc_term st u in
141 let vv = proc_term st v in
142 let tts = proc_terms st ts in
144 | C.Prod (s, w, t) ->
145 let rr, ss = alpha st s w t in
146 let ww = proc_term st w in
147 let tt = proc_term {st with d=add_name rr st.d; c=K.add_dec ss w st.c} t in
149 | C.Lambda (s, w, t) ->
150 let rr, ss = alpha st s w t in
151 let ww = proc_term st w in
152 let tt = proc_term {st with d=add_name rr st.d; c=K.add_dec ss w st.c} t in
154 | C.LetIn (s, w, v, t) ->
155 let rr, ss = alpha st s w t in
156 let ww = proc_term st w in
157 let vv = proc_term st v in
158 let tt = proc_term {st with d=add_name rr st.d; c=K.add_def ss w v st.c} t in
161 and proc_terms st ts =
162 let rtts = L.rev_map (proc_term st) ts in
165 let proc_named_term s st t =
167 let tt = proc_term st t in
168 if !G.test then begin
169 let _ = K.typeof st.c tt in
175 | T.TypeCheckerFailure s
176 | T.AssertFailure s -> malformed (Lazy.force s)
178 (* interface functions ******************************************************)
180 let process_top_term s t = proc_named_term s (init ()) t