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