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