]> matita.cs.unibo.it Git - helm.git/blob - matita/components/binaries/matex/engine.ml
- plain anticipation for CIC proofs terms
[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;   (* object name *)
32    s: int list; (* scope *)
33
34
35 (* internal functions *******************************************************)
36
37 let alpha c s = s
38
39 let malformed s =
40    X.error ("engine: malformed term: " ^ s)
41
42 let not_supported () =
43    X.error "engine: object not supported"
44
45 (* generic term processing *)
46
47 let proc_sort = function
48    | C.Prop             -> [T.Macro "PROP"]
49    | C.Type [`Type, u]  -> [T.Macro "TYPE"; T.arg (U.string_of_uri u)]
50    | C.Type [`CProp, u] -> [T.Macro "CROP"; T.arg (U.string_of_uri u)]
51    | C.Type _           -> malformed "T1"
52
53 let rec proc_term c = function
54    | C.Appl []
55    | C.Meta _
56    | C.Implicit _           -> malformed "T2" 
57    | C.Rel m                ->
58       let name = K.resolve_lref c m in
59       [T.Macro "LREF"; T.arg name; T.free name]
60    | C.Appl ts              ->
61       let riss = L.rev_map (proc_term c) ts in
62       T.Macro "APPL" :: T.mk_rev_args riss
63    | C.Prod (s, w, t)       ->
64       let s = alpha c s in
65       let is_w = proc_term c w in
66       let is_t = proc_term (K.add_dec s w c) t in
67       T.Macro "PROD" :: T.arg s :: T.Group is_w :: is_t
68    | C.Lambda (s, w, t)     -> 
69       let s = alpha c s in
70       let is_w = proc_term c w in
71       let is_t = proc_term (K.add_dec s w c) t in
72       T.Macro "ABST" :: T.arg s :: T.Group is_w :: is_t
73    | C.LetIn (s, w, v, t)   -> 
74       let s = alpha c s in
75       let is_w = proc_term c w in
76       let is_v = proc_term c v in
77       let is_t = proc_term (K.add_def s w v c) t in
78       T.Macro "ABBR" :: T.arg s :: T.Group is_w :: T.Group is_v :: is_t
79    | C.Sort s               ->
80       proc_sort s
81    | C.Const (R.Ref (u, r)) ->
82       let ss = K.segments_of_uri u in
83       let _, _, _, _, obj = E.get_checked_obj G.status u in  
84       let ss, name = K.name_of_reference ss (obj, r) in
85       [T.Macro "GREF"; T.arg name; T.free (X.rev_concat "." "type" ss)]
86    | C.Match (w, u, v, ts)  ->
87       let is_w = proc_term c (C.Const w) in
88       let is_u = proc_term c u in
89       let is_v = proc_term c v in
90       let riss = L.rev_map (proc_term c) ts in
91       T.Macro "CASE" :: T.Group is_w :: T.Group is_u :: T.Group is_v :: T.mk_rev_args riss
92
93 let proc_term c t = try proc_term c t with
94    | E.ObjectNotFound _ 
95    | Invalid_argument "List.nth"
96    | Failure "nth" 
97    | Failure "name_of_reference" -> malformed "T3"
98
99 (* proof processing *)
100
101 let init n = {
102    n = n; s = [];
103 }
104
105 let mk_dec w s is =
106    let w = if !G.no_types then [] else w in
107    T.Group w :: T.free s :: T.arg s :: T.Macro "DECL" :: is
108
109 let rec proc_proof st ris c t = match t with
110    | C.Appl []
111    | C.Meta _
112    | C.Implicit _  
113    | C.Sort _
114    | C.Prod _              -> malformed "P1"
115    | C.Const _
116    | C.Rel _               -> proc_proof st ris c (C.Appl [t])
117    | C.Lambda (s, w, t)    -> 
118       let s = alpha c s in
119       let is_w = proc_term c w in
120       proc_proof st (T.Macro "PRIM" :: mk_dec is_w s ris) (K.add_dec s w c) t
121    | C.Appl ts              ->
122       let rts = X.rev_neg_filter (A.not_prop2 c) [] ts in
123       let ris = T.Macro "STEP" :: ris in
124       let tts = L.rev_map (proc_term c) rts in
125       T.rev_mk_args tts ris
126    | C.Match (w, u, v, ts) ->
127       let rts = X.rev_neg_filter (A.not_prop2 c) [v] ts in
128       let ris = T.Macro "DEST" :: ris in
129       let tts = L.rev_map (proc_term c) rts in
130       T.rev_mk_args tts ris
131    | C.LetIn (s, w, v, t)  -> 
132       let s = alpha c s in
133       let is_w = proc_term c w in
134       if A.not_prop1 c w then
135          let is_v = proc_term c v in
136          proc_proof st (T.Group is_v :: T.Macro "BODY" :: mk_dec is_w s ris) (K.add_def s w v c) t
137       else
138          let ris_v = proc_proof st ris c v in
139          proc_proof st (mk_dec is_w s ris_v) (K.add_def s w v c) t
140
141 let proc_proof c t = try proc_proof (init "") [] c t with 
142    | E.ObjectNotFound _ 
143    | Invalid_argument "List.nth"
144    | Failure "nth" 
145    | Failure "name_of_reference" -> malformed "P2"
146    | V.TypeCheckerFailure s
147    | V.AssertFailure s           -> malformed (Lazy.force s)
148
149 (* top level processing *)
150
151 let proc_top_type s t = 
152    [T.Macro "Object"; T.arg s; T.free s; T.Group (proc_term [] t)]
153
154 let proc_top_body s t = proc_term [] t
155
156 let proc_top_proof s t =
157    let tt = A.process_top_term s t in (* anticipation *)
158    L.rev (proc_proof [] tt)
159
160 let open_out_tex s =
161    open_out (F.concat !G.out_dir (s ^ T.file_ext))
162
163 let proc_pair s ss u xt =
164    let name = X.rev_concat "." "type" ss in
165    let och = open_out_tex name in
166    O.out_text och (proc_top_type s u);
167    close_out och;
168    match xt with
169       | None   -> ()
170       | Some t ->
171          let name = X.rev_concat "." "body" ss in
172          let och = open_out_tex name in
173          let text = if A.not_prop1 [] u then proc_top_body else proc_top_proof in
174          O.out_text och (text s t);
175          close_out och
176
177 let proc_fun ss (r, s, i, u, t) =
178    proc_pair s (s :: ss) u (Some t)
179
180 let proc_obj u =
181    let ss = K.segments_of_uri u in
182    let _, _, _, _, obj = E.get_checked_obj G.status u in 
183    match obj with 
184       | C.Constant (_, s, xt, u, _) -> proc_pair s ss u xt
185       | C.Fixpoint (_, fs, _)       -> L.iter (proc_fun ss) fs
186       | C.Inductive (_, _, _, _)    -> not_supported ()
187
188 (* interface functions ******************************************************)
189
190 let process = proc_obj