]> matita.cs.unibo.it Git - helm.git/blob - matita/components/binaries/matex/engine.ml
0cfb1108781b7ef04c36f4788b22b3e440cb244d
[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
30 type status = {
31    n: string;   (* reference name *)
32    s: int list; (* scope *)
33
34
35 (* internal functions *******************************************************)
36
37 let alpha c s = s
38
39 let internal s =
40    X.error ("engine: malformed stack: " ^ s)
41
42 let malformed s =
43    X.error ("engine: malformed term: " ^ s)
44
45 let not_supported () =
46    X.error "engine: object not supported"
47
48 (* generic term processing *)
49
50 let proc_sort = function
51    | C.Prop             -> [T.Macro "PROP"]
52    | C.Type [`Type, u]  -> [T.Macro "TYPE"; T.arg (U.string_of_uri u)]
53    | C.Type [`CProp, u] -> [T.Macro "CROP"; T.arg (U.string_of_uri u)]
54    | C.Type _           -> malformed "T1"
55
56 let rec proc_term c = function
57    | C.Appl []
58    | C.Meta _
59    | C.Implicit _           -> malformed "T2" 
60    | C.Rel m                ->
61       let name = K.resolve_lref c m in
62       [T.Macro "LREF"; T.arg name; T.free name]
63    | C.Appl ts              ->
64       let riss = L.rev_map (proc_term c) ts in
65       T.Macro "APPL" :: T.mk_rev_args riss
66    | C.Prod (s, w, t)       ->
67       let s = alpha c s in
68       let is_w = proc_term c w in
69       let is_t = proc_term (K.add_dec s w c) t in
70       T.Macro "PROD" :: T.arg s :: T.Group is_w :: is_t
71    | C.Lambda (s, w, t)     -> 
72       let s = alpha c s in
73       let is_w = proc_term c w in
74       let is_t = proc_term (K.add_dec s w c) t in
75       T.Macro "ABST" :: T.arg s :: T.Group is_w :: is_t
76    | C.LetIn (s, w, v, t)   -> 
77       let s = alpha c s in
78       let is_w = proc_term c w in
79       let is_v = proc_term c v in
80       let is_t = proc_term (K.add_def s w v c) t in
81       T.Macro "ABBR" :: T.arg s :: T.Group is_w :: T.Group is_v :: is_t
82    | C.Sort s               ->
83       proc_sort s
84    | C.Const (R.Ref (u, r)) ->
85       let ss = K.segments_of_uri u in
86       let _, _, _, _, obj = E.get_checked_obj G.status u in  
87       let ss, name = K.name_of_reference ss (obj, r) in
88       [T.Macro "GREF"; T.arg name; T.free (X.rev_map_concat X.id "." "type" ss)]
89    | C.Match (w, u, v, ts)  ->
90       let is_w = proc_term c (C.Const w) in
91       let is_u = proc_term c u in
92       let is_v = proc_term c v in
93       let riss = L.rev_map (proc_term c) ts in
94       T.Macro "CASE" :: T.Group is_w :: T.Group is_u :: T.Group is_v :: T.mk_rev_args riss
95
96 let proc_term c t = try proc_term c t with
97    | E.ObjectNotFound _ 
98    | Invalid_argument "List.nth"
99    | Failure "nth" 
100    | Failure "name_of_reference" -> malformed "T3"
101
102 (* proof processing *)
103
104 let typeof c = function
105    | C.Appl [t]
106    | t          -> A.typeof c t
107
108 let init () = {
109    n =  ""; s = [1]
110 }
111
112 let push st n = {
113    n = n; s = 1 :: st.s;
114 }
115
116 let next st = {
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 st.n :: T.arg st.n :: T.Macro "OPEN" :: ris
130
131 let mk_dec kind w s ris =
132    let w = if !G.no_types then [] else w in
133    T.Group w :: T.free s :: T.arg s :: T.Macro kind :: ris
134
135 let mk_inferred st c t ris =
136    let u = typeof c t in
137    let is_u = proc_term c u in
138    mk_dec "DECL" is_u st.n ris
139
140 let rec proc_proof st ris c 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 (C.Appl [t])
148    | C.Lambda (s, w, t)    -> 
149       let s = alpha c s in
150       let is_w = proc_term c w in
151       let ris = mk_open st ris in
152       proc_proof (next st) (mk_dec "PRIM" is_w s ris) (K.add_dec s w c) t
153    | C.Appl ts              ->
154       let rts = X.rev_neg_filter (A.not_prop2 c) [] ts in
155       let ris = T.Macro "STEP" :: mk_inferred st c t ris in
156       let tts = L.rev_map (proc_term c) rts in
157       mk_exit st (T.rev_mk_args tts ris)
158    | C.Match (w, u, v, ts) ->
159       let rts = X.rev_neg_filter (A.not_prop2 c) [v] ts in
160       let ris = T.Macro "DEST" :: mk_inferred st c t ris in
161       let tts = L.rev_map (proc_term c) rts in
162       mk_exit st (T.rev_mk_args tts ris)
163    | C.LetIn (s, w, v, t)  -> 
164       let s = alpha c s in
165       let is_w = proc_term c w in
166       let ris = mk_open st ris in
167       if A.not_prop1 c w then
168          let is_v = proc_term c v in
169          let ris = T.Group is_v :: T.Macro "BODY" :: mk_dec "DECL" is_w s ris in
170          proc_proof (next st) ris (K.add_def s w v c) t
171       else
172          let ris_v = proc_proof (push st s) ris c v in
173          proc_proof (next st) ris_v (K.add_def s w v c) t
174
175 let proc_proof rs c t = try proc_proof (init ()) rs c t with 
176    | E.ObjectNotFound _ 
177    | Invalid_argument "List.nth"
178    | Failure "nth"
179    | Failure "name_of_reference" -> malformed "P2"
180    | V.TypeCheckerFailure s
181    | V.AssertFailure s           -> malformed (Lazy.force s)
182    | Failure "hd"
183    | Failure "tl"                -> internal "P2"
184
185 (* top level processing *)
186
187 let proc_top_type s t = 
188    [T.Macro "Object"; T.arg s; T.free s; T.Group (proc_term [] t)]
189
190 let proc_top_body s t = proc_term [] t
191
192 let proc_top_proof s t =
193    let tt = A.process_top_term s t in (* anticipation *)
194    L.rev (T.arg "proof" :: T.Macro "end" :: proc_proof [T.arg "proof"; T.Macro "begin"] [] tt)
195
196 let open_out_tex s =
197    open_out (F.concat !G.out_dir (s ^ T.file_ext))
198
199 let proc_pair s ss u xt =
200    let name = X.rev_map_concat X.id "." "type" ss in
201    let och = open_out_tex name in
202    O.out_text och (proc_top_type s u);
203    close_out och;
204    match xt with
205       | None   -> ()
206       | Some t ->
207          let name = X.rev_map_concat X.id "." "body" ss in
208          let och = open_out_tex name in
209          let text = if A.not_prop1 [] u then proc_top_body else proc_top_proof in
210          O.out_text och (text s t);
211          close_out och
212
213 let proc_fun ss (r, s, i, u, t) =
214    proc_pair s (s :: ss) u (Some t)
215
216 let proc_obj u =
217    let ss = K.segments_of_uri u in
218    let _, _, _, _, obj = E.get_checked_obj G.status u in 
219    match obj with 
220       | C.Constant (_, s, xt, u, _) -> proc_pair s ss u xt
221       | C.Fixpoint (_, fs, _)       -> L.iter (proc_fun ss) fs
222       | C.Inductive (_, _, _, _)    -> not_supported ()
223
224 (* interface functions ******************************************************)
225
226 let process = proc_obj