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.
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_______________________________________________________________ *)
20 module E = NCicEnvironment
21 module V = NCicTypeChecker
31 g: string; (* global object name *)
32 l: string; (* local reference name *)
33 s: int list; (* scope *)
34 w: bool; (* is first construction *)
37 (* internal functions *******************************************************)
42 X.error ("engine: malformed term: " ^ s)
44 let not_supported () =
45 X.error "engine: object not supported"
47 (* generic term processing *)
49 let proc_sort = function
50 | C.Prop -> [T.Macro "PROP"]
51 | C.Type [`Type, u] -> [T.Macro "TYPE"; T.arg (U.string_of_uri u)]
52 | C.Type [`CProp, u] -> [T.Macro "CROP"; T.arg (U.string_of_uri u)]
53 | C.Type _ -> malformed "T1"
55 let rec proc_term c = function
58 | C.Implicit _ -> malformed "T2"
60 let name = K.resolve_lref c m in
61 [T.Macro "LREF"; T.arg name; T.free name]
63 let riss = L.rev_map (proc_term c) ts in
64 T.Macro "APPL" :: T.mk_rev_args riss
67 let is_w = proc_term c w in
68 let is_t = proc_term (K.add_dec s w c) t in
69 T.Macro "PROD" :: T.arg s :: T.Group is_w :: is_t
70 | C.Lambda (s, w, t) ->
72 let is_w = proc_term c w in
73 let is_t = proc_term (K.add_dec s w c) t in
74 T.Macro "ABST" :: T.arg s :: T.Group is_w :: is_t
75 | C.LetIn (s, w, v, t) ->
77 let is_w = proc_term c w in
78 let is_v = proc_term c v in
79 let is_t = proc_term (K.add_def s w v c) t in
80 T.Macro "ABBR" :: T.arg s :: T.Group is_w :: T.Group is_v :: is_t
83 | C.Const (R.Ref (u, r)) ->
84 let ss = K.segments_of_uri u in
85 let _, _, _, _, obj = E.get_checked_obj G.status u in
86 let ss, name = K.name_of_reference ss (obj, r) in
87 [T.Macro "GREF"; T.arg name; T.free (X.rev_concat "." "type" ss)]
88 | C.Match (w, u, v, ts) ->
89 let is_w = proc_term c (C.Const w) in
90 let is_u = proc_term c u in
91 let is_v = proc_term c v in
92 let riss = L.rev_map (proc_term c) ts in
93 T.Macro "CASE" :: T.Group is_w :: T.Group is_u :: T.Group is_v :: T.mk_rev_args riss
95 let proc_term c t = try proc_term c t with
97 | Invalid_argument "List.nth"
99 | Failure "name_of_reference" -> malformed "T3"
101 (* proof processing *)
104 g = n; l = "_"; s = []; w = true
107 let push st n = {st with
108 l = n; s = 1 :: st.s; w = true
111 let pop st = {st with
112 w = false; s = match st.s with [] -> [] | i :: tl -> succ i :: tl
115 let next st = {st with
120 if st.w then T.Macro "PRIM" :: T.arg "" :: T.Macro "OPEN" :: ris else ris
123 let w = if !G.no_types then [] else w in
124 T.Group w :: T.free s :: T.arg s :: T.Macro "DECL" :: ris
126 let rec proc_proof st ris c t = match t with
131 | C.Prod _ -> malformed "P1"
133 | C.Rel _ -> proc_proof st ris c (C.Appl [t])
134 | C.Lambda (s, w, t) ->
136 let is_w = proc_term c w in
137 let ris = mk_open st ris in
138 proc_proof (next st) (T.Macro "PRIM" :: mk_dec is_w s ris) (K.add_dec s w c) t
140 let rts = X.rev_neg_filter (A.not_prop2 c) [] ts in
141 let ris = T.Macro "STEP" :: ris in
142 let tts = L.rev_map (proc_term c) rts in
143 T.rev_mk_args tts ris
144 | C.Match (w, u, v, ts) ->
145 let rts = X.rev_neg_filter (A.not_prop2 c) [v] ts in
146 let ris = T.Macro "DEST" :: ris in
147 let tts = L.rev_map (proc_term c) rts in
148 T.rev_mk_args tts ris
149 | C.LetIn (s, w, v, t) ->
151 let is_w = proc_term c w in
152 if A.not_prop1 c w then
153 let is_v = proc_term c v in
154 proc_proof (next st) (T.Group is_v :: T.Macro "BODY" :: mk_dec is_w s ris) (K.add_def s w v c) t
156 let ris_v = proc_proof (push st s) ris c v in
157 proc_proof (pop st) (mk_dec is_w s ris_v) (K.add_def s w v c) t
159 let proc_proof c t = try proc_proof (init "") [] c t with
161 | Invalid_argument "List.nth"
163 | Failure "name_of_reference" -> malformed "P2"
164 | V.TypeCheckerFailure s
165 | V.AssertFailure s -> malformed (Lazy.force s)
167 (* top level processing *)
169 let proc_top_type s t =
170 [T.Macro "Object"; T.arg s; T.free s; T.Group (proc_term [] t)]
172 let proc_top_body s t = proc_term [] t
174 let proc_top_proof s t =
175 let tt = A.process_top_term s t in (* anticipation *)
176 L.rev (proc_proof [] tt)
179 open_out (F.concat !G.out_dir (s ^ T.file_ext))
181 let proc_pair s ss u xt =
182 let name = X.rev_concat "." "type" ss in
183 let och = open_out_tex name in
184 O.out_text och (proc_top_type s u);
189 let name = X.rev_concat "." "body" ss in
190 let och = open_out_tex name in
191 let text = if A.not_prop1 [] u then proc_top_body else proc_top_proof in
192 O.out_text och (text s t);
195 let proc_fun ss (r, s, i, u, t) =
196 proc_pair s (s :: ss) u (Some t)
199 let ss = K.segments_of_uri u in
200 let _, _, _, _, obj = E.get_checked_obj G.status u in
202 | C.Constant (_, s, xt, u, _) -> proc_pair s ss u xt
203 | C.Fixpoint (_, fs, _) -> L.iter (proc_fun ss) fs
204 | C.Inductive (_, _, _, _) -> not_supported ()
206 (* interface functions ******************************************************)
208 let process = proc_obj