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