]> matita.cs.unibo.it Git - helm.git/blob - matita/components/binaries/matex/engine.ml
- improved support for case
[helm.git] / matita / components / binaries / matex / engine.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 F = Filename
13 module L = List
14 module P = Printf
15 module S = String
16
17 module U = NUri
18 module R = NReference
19 module C = NCic
20 module E = NCicEnvironment
21 module V = NCicTypeChecker
22
23 module X = Ground
24 module G = Options
25 module K = Kernel
26 module T = TeX
27 module O = TeXOutput
28 module A = Anticipate
29 module N = Alpha
30
31 type status = {
32    i: string;   (* item name *)
33    n: string;   (* reference name *)
34    s: int list; (* scope *)
35    c: C.context (* context for kernel calls *)
36
37
38 (* internal functions *******************************************************)
39
40 let internal s =
41    X.error ("engine: malformed stack: " ^ s)
42
43 let malformed s =
44    X.error ("engine: malformed term: " ^ s)
45
46 let missing s =
47    X.log ("engine: missing macro for " ^ s)
48
49 (* generic term processing *)
50
51 let rec rename s = function
52    | []                        -> s
53    | (s1, s2) :: _ when s1 = s -> s2
54    | _ :: tl                   -> rename s tl
55
56 let mk_lname s = s
57
58 let mk_gname s =
59    rename s !G.alpha_gref
60
61 let mk_ptr st name = 
62    if G.is_global_id name then P.sprintf "%s.%s" st.i name else ""
63
64 let get_macro s l =
65    let rec aux = function
66       | []                                 -> ""
67       | (r, m, a) :: _ when r = s && a = l -> m
68       | _ :: tl                            -> aux tl
69    in
70    aux !G.macro_gref
71
72 let get_head = function
73    | C.Const c :: ts ->
74       let s, _ = K.resolve_reference c in
75       let macro = get_macro s (L.length ts) in
76       if macro <> "" then Some (macro, s, ts) else begin 
77          if !G.log_missing then missing s;
78          None
79       end 
80    | _               -> None
81
82 let proc_sort st is = function
83    | C.Prop             -> T.Macro "PROP" :: is
84    | C.Type [`Type, u]  -> T.Macro "TYPE" :: T.arg (U.string_of_uri u) :: is
85    | C.Type [`CProp, u] -> T.Macro "CROP" :: T.arg (U.string_of_uri u) :: is
86    | C.Type _           -> malformed "T1"
87
88 let rec proc_term st is = function
89    | C.Appl []
90    | C.Meta _
91    | C.Implicit _           -> malformed "T2" 
92    | C.Rel m                ->
93       let s = K.resolve_lref st.c m in
94       T.Macro "LREF" :: T.arg (mk_lname s) :: T.free (mk_ptr st s) :: is
95    | C.Appl ts              ->
96       begin match get_head ts with
97          | Some (macro, s, ts) ->
98             let riss = L.rev_map (proc_term st []) ts in
99             T.Macro macro :: T.free s :: T.mk_rev_args riss is
100          | None                ->
101             let riss = L.rev_map (proc_term st []) ts in
102             T.Macro "APPL" :: T.mk_rev_args riss is
103       end
104    | C.Prod (s, w, t)       ->
105       let is_w = proc_term st [] w in
106       let c = K.add_dec s w st.c in
107       let is_t = proc_term {st with c=c} is t in
108       let macro = if K.not_prop1 c t then "PROD" else "FALL" in
109       T.Macro macro :: T.arg (mk_lname s) :: T.free (mk_ptr st s) :: T.Group is_w :: is_t
110    | C.Lambda (s, w, t)     -> 
111       let is_w = proc_term st [] w in
112       let is_t = proc_term {st with c=K.add_dec s w st.c} is t in
113       T.Macro "ABST" :: T.arg (mk_lname s) :: T.free (mk_ptr st s) :: T.Group is_w :: is_t
114    | C.LetIn (s, w, v, t)   ->
115       let is_w = proc_term st [] w in
116       let is_v = proc_term st [] v in
117       let is_t = proc_term {st with c=K.add_def s w v st.c} is t in
118       T.Macro "ABBR" :: T.arg (mk_lname s) :: T.free (mk_ptr st s) :: T.Group is_w :: T.Group is_v :: is_t
119    | C.Sort s               ->
120       proc_sort st is s
121    | C.Const c              ->
122       let s, name = K.resolve_reference c in
123       T.Macro "GREF" :: T.arg (mk_gname name) :: T.free s :: is
124    | C.Match (w, u, v, ts)  ->
125       let is_w = proc_term st [] (C.Const w) in
126       let is_u = proc_term st [] u in
127       let is_v = proc_term st [] v in
128       let riss = X.rev_mapi (proc_case st [] w) K.fst_con ts in
129       let macro = if ts = [] then "CAZE" else "CASE" in
130       T.Macro macro :: T.Group is_w :: T.Group is_u :: T.Group is_v :: T.mk_rev_args riss is
131
132 and proc_case st is w i t =
133    let v = R.mk_constructor i w in
134    let is_v = proc_term st [] (C.Const v) in
135    let is_t = proc_term st [] t in
136    T.Macro "PAIR" :: T.Group is_v :: T.Group is_t :: is
137
138 let proc_term st is t = try proc_term st is t with
139    | E.ObjectNotFound _ 
140    | Invalid_argument "List.nth"
141    | Failure "nth" 
142    | Failure "name_of_reference" -> malformed "T3"
143
144 (* proof processing *)
145
146 let typeof st = function
147    | C.Appl [t]
148    | t          -> K.whd_typeof st.c t
149
150 let init i = {
151    i = i;
152    n =  ""; s = [1]; c = [];
153 }
154
155 let push st n = {st with
156    n = n; s = 1 :: st.s;
157 }
158
159 let next st f = {st with
160    c = f st.c;
161    n = ""; s = match st.s with [] -> failwith "hd" | i :: tl -> succ i :: tl
162 }
163
164 let scope st =
165    X.rev_map_concat string_of_int "." "" (L.tl st.s)
166
167 let mk_exit st ris =
168    if st.n <> "" || L.tl st.s = [] then ris else
169    T.free (scope st) :: T.Macro "EXIT" :: ris
170
171 let mk_open st ris =
172    if st.n = "" then ris else
173    T.free (scope st) :: T.free (mk_ptr st st.n) :: T.arg (mk_lname st.n) :: T.Macro "OPEN" :: ris
174
175 let mk_dec st kind w s ris =
176    let w = if !G.no_types then [] else w in
177    T.Group w :: T.free (mk_ptr st s) :: T.arg (mk_lname s) :: T.Macro kind :: ris
178
179 let mk_inferred st t ris =
180    let u = typeof st t in
181    let is_u = proc_term st [] u in
182    mk_dec st "DECL" is_u st.n ris
183
184 let rec proc_proof st ris t = match t with
185    | C.Appl []
186    | C.Meta _
187    | C.Implicit _  
188    | C.Sort _
189    | C.Prod _              -> malformed "P1"
190    | C.Const _
191    | C.Rel _               -> proc_proof st ris (C.Appl [t])
192    | C.Lambda (s, w, t)    ->
193       let is_w = proc_term st [] w in
194       let ris = mk_open st ris in
195       proc_proof (next st (K.add_dec s w)) (mk_dec st "PRIM" is_w s ris) t
196    | C.Appl (t0 :: ts)     ->
197       let rts = X.rev_neg_filter (K.not_prop2 st.c) [t0] ts in
198       let ris = T.Macro "STEP" :: mk_inferred st t ris in
199       let tts = L.rev_map (proc_term st []) rts in
200       mk_exit st (T.rev_mk_args tts ris)
201    | C.Match (w, u, v, ts) ->
202       let rts = X.rev_neg_filter (K.not_prop2 st.c) [v] ts in
203       let ris = T.Macro "DEST" :: mk_inferred st t ris in
204       let tts = L.rev_map (proc_term st []) rts in
205       mk_exit st (T.rev_mk_args tts ris)
206    | C.LetIn (s, w, v, t)  -> 
207       let is_w = proc_term st [] w in
208       let ris = mk_open st ris in
209       if K.not_prop1 st.c w then
210          let is_v = proc_term st [] v in
211          let ris = T.Group is_v :: T.Macro "BODY" :: mk_dec st "DECL" is_w s ris in
212          proc_proof (next st (K.add_def s w v)) ris t
213       else
214          let ris_v = proc_proof (push st s) ris v in
215          proc_proof (next st (K.add_def s w v)) ris_v t
216
217 let proc_proof st rs t = try proc_proof st rs t with 
218    | E.ObjectNotFound _ 
219    | Invalid_argument "List.nth"
220    | Failure "nth"
221    | Failure "name_of_reference" -> malformed "P2"
222    | V.TypeCheckerFailure s
223    | V.AssertFailure s           -> malformed (Lazy.force s)
224    | Failure "hd"
225    | Failure "tl"                -> internal "P2"
226
227 (* top level processing *)
228
229 let note = T.Note "This file was automatically generated by MaTeX: do not edit"
230
231 let proc_item item s ss t =
232    let st = init ss in
233    let tt = N.process_top_term s t in (* alpha-conversion *)
234    let is = [T.Macro "end"; T.arg item] in
235    note :: T.Macro "begin" :: T.arg item :: T.arg (mk_gname s) :: T.free ss :: proc_term st is tt
236
237 let proc_top_proof s ss t =
238    let st = init ss in
239    let t0 = A.process_top_term s t in  (* anticipation *)
240    let tt = N.process_top_term s t0 in (* alpha-conversion *)
241    let ris = [T.free ss; T.arg (mk_gname s); T.arg "proof"; T.Macro "begin"; note] in
242    L.rev (T.arg "proof" :: T.Macro "end" :: proc_proof st ris tt)
243
244 let open_out_tex s =
245    let fname = s ^ T.file_ext in
246    begin match !G.list_och with
247       | None     -> ()
248       | Some och -> P.fprintf och "%s\n" fname
249    end;
250    open_out (F.concat !G.out_dir fname)
251
252 let proc_pair s ss u = function
253    | None   -> 
254       let name = X.rev_map_concat X.id "." "type" ss in
255       let och = open_out_tex name in
256          O.out_text och (proc_item "axiom" s name u);
257       close_out och
258    | Some t ->
259       let text_u, text_t =
260          if K.not_prop1 [] u then proc_item "declaration", proc_item "definition"
261          else proc_item "proposition", proc_top_proof
262       in
263       let name = X.rev_map_concat X.id "." "type" ss in
264       let och = open_out_tex name in
265          O.out_text och (text_u s name u);
266       close_out och;
267       let name = X.rev_map_concat X.id "." "body" ss in
268       let och = open_out_tex name in
269          O.out_text och (text_t s name t);
270       close_out och
271
272 let proc_fun ss (r, s, i, u, t) =
273    proc_pair s (s :: ss) u (Some t)
274
275 let proc_constructor ss (r, s, u) =
276    proc_pair s (s :: ss) u None
277
278 let proc_type ss (r, s, u, cs) =
279    proc_pair s (s :: ss) u None;
280    L.iter (proc_constructor ss) cs
281
282 let proc_obj u =
283    let ss = K.segments_of_uri u in
284    let _, _, _, _, obj = E.get_checked_obj G.status u in 
285    match obj with 
286       | C.Constant (_, s, xt, u, _) -> proc_pair s ss u xt
287       | C.Fixpoint (_, fs, _)       -> L.iter (proc_fun ss) fs
288       | C.Inductive (_, _, ts, _)   -> L.iter (proc_type ss) ts
289
290 (* interface functions ******************************************************)
291
292 let process = proc_obj