]> matita.cs.unibo.it Git - helm.git/blob - matita/components/binaries/matex/alpha.ml
4c4eccef70c0a81fad22842244f41547d10ea80f
[helm.git] / matita / components / binaries / matex / alpha.ml
1 (*
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.                     
5     ||I||                                                                
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_______________________________________________________________ *)
11
12 module L = List
13 module P = Printf
14 module S = Scanf
15 module N = String
16 module H = Hashtbl
17
18 module U = NUri
19 module C = NCic
20 module R = NReference
21 module E = NCicEnvironment
22 module T = NCicTypeChecker
23
24 module X = Ground
25 module G = Options
26 module K = Kernel
27
28 type global = (string, int) H.t
29
30 type local = (string * int) list
31
32 type status = {
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 *)
36 }
37
38 (* internal functions *******************************************************)
39
40 let malformed s =
41    X.error ("alpha: malformed term: " ^ s)
42
43 let ok s =
44    X.log ("alpha: ok " ^ s)
45
46 let init () = {
47    g = H.create 11; d = []; c = [];
48 }
49
50 let rec trim r =
51    let r1, r2 = S.sscanf r "%s@_%s" X.id2 in
52    if r2 = "" then r1 else trim r1
53
54 let split s = 
55    let rec aux i l =
56       let j = pred i in
57       if j >=0 && s.[j] >= '0' && s.[j] <= '9' then aux j (succ l) else i, l
58    in 
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
63
64 let rec strip = function
65    | C.Appl (t :: _) 
66    | C.Prod (_, _, t) -> strip t
67    | t                -> t
68
69 let get_constant c t = match strip (K.whd c t) with
70    | C.Sort (C.Prop)             ->
71       P.sprintf "Prop"
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)
76    | C.Const c                   ->
77       let s, _ = K.resolve_reference c in
78       s
79    | _                           -> ""
80
81 let read l s r =
82    let rec aux = function
83       | []              -> ""
84       | (a, b, c) :: tl ->
85          if s = a && (r = b || r = c) then c else aux tl
86    in
87    aux l 
88
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
96       if !G.log_alpha then
97          X.log (P.sprintf "alpha: not found %s: type \"%s\" sort \"%s\"" r s0 s1);
98       r
99    end
100
101 let rec get r = function
102    | []           -> r
103    | (h, d) :: tl ->
104       if fst r = h && snd r <= d then h, succ d else get r tl
105
106 let mk_name (s, i) =
107    if i < 0 then s else P.sprintf "%s%u" s i
108
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
113
114 let global_apha st s =
115 try 
116    let i = H.find st.g s in
117    H.replace st.g s (succ i);
118    P.sprintf "%s.%u" s i
119 with Not_found ->
120    H.add st.g s 0;
121    s
122
123 let alpha st s w t =
124    let r = local_alpha st s w t in
125    let s = mk_name r in
126    r, if G.is_global_id s then global_apha st s else s
127
128 let add_name r d = r :: d
129
130 let rec proc_term st t = match t with
131    | C.Meta _
132    | C.Implicit _             
133    | C.Sort _
134    | C.Rel _
135    | C.Const _             -> t
136    | C.Appl ts             ->
137       let tts = proc_terms st ts in
138       K.appl tts
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
143       K.case w uu vv tts
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
148       K.prod ss ww tt
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
153       K.lambda ss ww tt
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
159       K.letin ss ww vv tt
160
161 and proc_terms st ts =
162    let rtts = L.rev_map (proc_term st) ts in
163    L.rev rtts
164
165 let proc_named_term s st t =
166 try
167    let tt = proc_term st t in
168    if !G.test then begin
169       let _ = K.typeof st.c tt in
170       ok s
171    end;
172    tt
173 with
174    | E.ObjectNotFound s 
175    | T.TypeCheckerFailure s
176    | T.AssertFailure s           -> malformed (Lazy.force s)
177
178 (* interface functions ******************************************************)
179
180 let process_top_term s t = proc_named_term s (init ()) t
181