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