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_______________________________________________________________ *)
20 module E = NCicEnvironment
21 module T = NCicTypeChecker
27 type global = (string, int) H.t
29 type local = (string * int) list
32 g: global; (* global context for alpha-conversion *)
33 d: local; (* local context for alpha-conversion *)
34 c: C.context; (* local context for kernel calls *)
37 (* internal functions *******************************************************)
40 X.error ("alpha: malformed term: " ^ s)
43 X.log ("alpha: ok " ^ s)
46 g = H.create 11; d = []; c = [];
50 let r1, r2 = S.sscanf r "%s@_%s" X.id2 in
51 if r2 = "" then r1 else trim r1
56 if j >=0 && s.[j] >= '0' && s.[j] <= '9' then aux j (succ l) else i, l
58 let i, l = aux (N.length s) 0 in
59 let s1, s2 = N.sub s 0 i, N.sub s i l in
60 let s1 = if s1 = "" then "_" else s1 in
61 s1, if l = 0 then G.nan else int_of_string s2
63 let rec strip = function
65 | C.Prod (_, _, t) -> strip t
68 let get_constant c t = match strip (K.whd c t) with
71 | C.Sort (C.Type [`Type, u]) ->
72 P.sprintf "Type[%s]" (U.string_of_uri u)
73 | C.Sort (C.Type [`CProp, u]) ->
74 P.sprintf "CProp[%s]" (U.string_of_uri u)
76 let s, _ = K.resolve_reference c in
81 let rec aux = function
84 if s = a && (r = b || r = c) then c else aux tl
88 let type_check r c w =
89 let s0 = get_constant c w in
90 let r0 = read !G.alpha_type s0 r in
91 if r0 <> "" then r0 else
92 let s1 = get_constant c (K.typeof c w) in
93 let r0 = read !G.alpha_sort s1 r in
94 if r0 <> "" then r0 else begin
96 X.log (P.sprintf "alpha: not found %s: type \"%s\" sort \"%s\"" r s0 s1);
100 let rec get r = function
103 if fst r = h && snd r <= d then h, succ d else get r tl
106 if i < 0 then s else P.sprintf "%s%u" s i
108 let local_alpha st s w t =
109 if K.does_not_occur K.fst_var st.c t then G.dno_id, G.nan else
110 let r, i = split (trim s) in
111 get (type_check r st.c w, i) st.d
113 let global_apha st s =
115 let i = H.find st.g s in
116 H.replace st.g s (succ i);
117 P.sprintf "%s_%u" s i
123 let r = local_alpha st s w t in
125 r, if G.is_global_id s then global_apha st s else s
127 let add_name r d = r :: d
129 let rec proc_term st t = match t with
136 let tts = proc_terms st ts in
138 | C.Match (w, u, v, ts) ->
139 let uu = proc_term st u in
140 let vv = proc_term st v in
141 let tts = proc_terms st ts in
143 | C.Prod (s, w, t) ->
144 let rr, ss = alpha st s w t in
145 let ww = proc_term st w in
146 let tt = proc_term {st with d=add_name rr st.d; c=K.add_dec ss w st.c} t in
148 | C.Lambda (s, w, t) ->
149 let rr, ss = alpha st s w t in
150 let ww = proc_term st w in
151 let tt = proc_term {st with d=add_name rr st.d; c=K.add_dec ss w st.c} t in
153 | C.LetIn (s, w, v, t) ->
154 let rr, ss = alpha st s w t in
155 let ww = proc_term st w in
156 let vv = proc_term st v in
157 let tt = proc_term {st with d=add_name rr st.d; c=K.add_def ss w v st.c} t in
160 and proc_terms st ts =
161 let rtts = L.rev_map (proc_term st) ts in
164 let proc_named_term s st t =
166 let tt = proc_term st t in
167 if !G.check then begin
168 let _ = K.typeof st.c tt in
174 | T.TypeCheckerFailure s
175 | T.AssertFailure s -> malformed (Lazy.force s)
177 (* interface functions ******************************************************)
179 let process_top_term s t = proc_named_term s (init ()) t