]> matita.cs.unibo.it Git - helm.git/blob - matita/components/binaries/matex/engine.ml
- First commit of MaTeX:
[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 S = String
15
16 module U = NUri
17 module R = NReference
18 module C = NCic
19 module P = NCicPp
20 module E = NCicEnvironment
21
22 module G = Options
23 module T = TeX
24 module O = TeXOutput
25
26 let status = new P.status
27
28 (* internal functions *******************************************************)
29
30 let rec segments_of_string ss l s =
31    match try Some (S.index s '/') with Not_found -> None with
32       | None   -> s :: ss
33       | Some i -> segments_of_string (S.sub s 0 i :: ss) (l-i-1) (S.sub s (i+1) (l-i-1))
34
35 let segments_of_uri u =
36    let s = U.string_of_uri u in
37    let s = F.chop_extension s in
38    let l = S.length s in
39    let i = S.index s ':' in
40    let s = S.sub s (i+2) (l-i-2) in
41    segments_of_string [] (l-i-2) s
42
43 let rec mk_string sep r = function
44    | []      -> r 
45    | s :: ss -> mk_string sep (s ^ sep ^ r) ss 
46
47 let alpha c s = s
48
49 let malformed s =
50    failwith ("MaTeX: malformed term: " ^ s)
51
52 let not_supported () =
53    failwith "MaTeX: object not supported"
54
55 let proc_sort = function
56    | C.Prop             -> [T.Macro "Prop"]
57    | C.Type [`Type, u]  -> [T.Macro "Type"; T.arg (U.string_of_uri u)]
58    | C.Type [`CProp, u] -> [T.Macro "Crop"; T.arg (U.string_of_uri u)]
59    | C.Type _           -> malformed "1"
60
61 let proc_gref r = T.Macro "GRef"
62
63 let rec proc_term c = function
64    | C.Appl []
65    | C.Meta _
66    | C.Implicit _         -> malformed "2" 
67    | C.Rel m              ->
68       let name = L.nth c (m-1) in
69       [T.Macro "LRef"; T.arg name]
70    | C.Appl ts            ->
71       let riss = L.rev_map (proc_term c) ts in
72       T.Macro "Appl" :: T.mk_rev_args riss
73   | C.Prod (s, w, t)      ->
74       let s = alpha c s in
75       let is_w = proc_term c w in
76       let is_t = proc_term (s::c) t in
77       T.Macro "Prod" :: T.arg s :: T.Group is_w :: is_t
78   | C.Lambda (s, w, t)    -> 
79       let s = alpha c s in
80       let is_w = proc_term c w in
81       let is_t = proc_term (s::c) t in
82       T.Macro "Abst" :: T.arg s :: T.Group is_w :: is_t
83   | C.LetIn (s, w, v, t)  -> 
84       let s = alpha c s in
85       let is_w = proc_term c w in
86       let is_v = proc_term c v in
87       let is_t = proc_term (s::c) t in
88       T.Macro "Abbr" :: T.arg s :: T.Group is_w :: T.Group is_v :: is_t
89   | C.Sort s              ->
90       proc_sort s
91   | C.Const r             ->
92       [proc_gref r]
93   | C.Match (w, u, v, ts) ->
94       let is_w = [proc_gref w] in
95       let is_u = proc_term c u in
96       let is_v = proc_term c v in
97       let riss = L.rev_map (proc_term c) ts in
98       T.Macro "Case" :: T.Group is_w :: T.Group is_u :: T.Group is_v :: T.mk_rev_args riss
99
100 let proc_term c t = try proc_term c t with
101    | E.ObjectNotFound _ 
102    | Failure "nth" 
103    | Invalid_argument "List.nth" -> malformed "3"
104
105 let open_out_tex s =
106    open_out (F.concat !G.out_dir (s ^ T.file_ext))
107
108 let proc_obj u =
109    let ss = segments_of_uri u in
110    let _, _, _, _, obj = E.get_checked_obj status u in 
111    match obj with
112       | C.Constant (_, _, None, u, _)   -> not_supported ()
113       | C.Constant (_, _, Some t, u, _) ->
114          let name = mk_string "." "body" ss in
115          let och = open_out_tex name in
116          O.out_text och (proc_term [] t);
117          O.out_text och T.newline;
118          close_out och;
119          let name = mk_string "." "type" ss in
120          let och = open_out_tex name in
121          O.out_text och (proc_term [] u);
122          O.out_text och T.newline;
123          close_out och
124       | C.Fixpoint (_, _, _)            -> not_supported ()
125       | C.Inductive (_, _, _, _)        -> not_supported ()
126
127 (* interface functions ******************************************************)
128
129 let process = proc_obj