]> matita.cs.unibo.it Git - helm.git/blob - components/cic_exportation/cicExportation.ml
* type definitions that define a new proposition are no longer exported
[helm.git] / components / cic_exportation / cicExportation.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (* $Id: cicPp.ml 7413 2007-05-29 15:30:53Z tassi $ *)
27
28 exception CicExportationInternalError;;
29 exception NotEnoughElements;;
30
31 (* Utility functions *)
32
33 let analyze_term context t =
34  match fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)with
35     Cic.Sort _ -> `Type
36   | ty -> 
37      match
38       fst (CicTypeChecker.type_of_aux' [] context ty CicUniv.oblivion_ugraph)
39      with
40         Cic.Sort Cic.Prop -> `Proof
41      | _ -> `Term
42 ;;
43
44 let analyze_type context t =
45  let rec aux =
46   function
47      Cic.Sort s -> `Sort s
48    | Cic.Prod (_,_,t) -> aux t
49    | _ -> `SomethingElse
50  in
51  match aux t with
52     `Sort _ as res -> res
53   | `SomethingElse ->
54       match
55        fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
56       with
57           Cic.Sort Cic.Prop -> `Statement
58        | _ -> `Type
59 ;;
60
61 let ppid =
62  let reserved =
63   [ "to";
64     "mod"
65   ]
66  in
67   function n ->
68    let n = String.uncapitalize n in
69     if List.mem n reserved then n ^ "_" else n
70 ;;
71
72 let ppname =
73  function
74     Cic.Name s     -> ppid s
75   | Cic.Anonymous  -> "_"
76 ;;
77
78 (* get_nth l n   returns the nth element of the list l if it exists or *)
79 (* raises NotEnoughElements if l has less than n elements             *)
80 let rec get_nth l n =
81  match (n,l) with
82     (1, he::_) -> he
83   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
84   | (_,_) -> raise NotEnoughElements
85 ;;
86
87 let qualified_name_of_uri current_module_name ?(capitalize=false) uri =
88  let name =
89   if capitalize then
90    String.capitalize (UriManager.name_of_uri uri)
91   else
92    ppid (UriManager.name_of_uri uri) in
93  let buri = UriManager.buri_of_uri uri in
94  let index = String.rindex buri '/' in
95  let filename = String.sub buri (index + 1) (String.length buri - index - 1) in
96   if current_module_name = filename then
97    name
98   else
99    String.capitalize filename ^ "." ^ name
100 ;;
101
102 (* pp t l                                                                  *)
103 (* pretty-prints a term t of cic in an environment l where l is a list of  *)
104 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
105 (* name associated to the greatest DeBrujin index in t                     *)
106 let pp current_module_name ?metasenv =
107 let rec pp t context =
108  let module C = Cic in
109    match t with
110       C.Rel n ->
111        begin
112         try
113          (match get_nth context n with
114              Some (C.Name s,_) -> ppid s
115            | Some (C.Anonymous,_) -> "__" ^ string_of_int n
116            | None -> "_hidden_" ^ string_of_int n
117          )
118         with
119          NotEnoughElements -> string_of_int (List.length context - n)
120        end
121     | C.Var (uri,exp_named_subst) ->
122         qualified_name_of_uri current_module_name uri ^
123          pp_exp_named_subst exp_named_subst context
124     | C.Meta (n,l1) ->
125        (match metasenv with
126            None ->
127             "?" ^ (string_of_int n) ^ "[" ^ 
128              String.concat " ; "
129               (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
130              "]"
131          | Some metasenv ->
132             try
133              let _,context,_ = CicUtil.lookup_meta n metasenv in
134               "?" ^ (string_of_int n) ^ "[" ^ 
135                String.concat " ; "
136                 (List.rev
137                   (List.map2
138                     (fun x y ->
139                       match x,y with
140                          _, None
141                        | None, _ -> "_"
142                        | Some _, Some t -> pp t context
143                     ) context l1)) ^
144                "]"
145             with
146               CicUtil.Meta_not_found _ 
147             | Invalid_argument _ ->
148               "???" ^ (string_of_int n) ^ "[" ^ 
149                String.concat " ; "
150                 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
151                "]"
152        )
153     | C.Sort s ->
154        (match s with
155            C.Prop  -> "Prop"
156          | C.Set   -> "Set"
157          | C.Type _ -> "Type"
158          (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
159          | C.CProp -> "CProp" 
160        )
161     | C.Implicit (Some `Hole) -> "%"
162     | C.Implicit _ -> "?"
163     | C.Prod (b,s,t) ->
164        (match b with
165           C.Name n -> "(\\forall " ^ n ^ ":" ^ pp s context ^ "." ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
166         | C.Anonymous -> "(" ^ pp s context ^ "\\to " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
167        )
168     | C.Cast (v,t) -> pp v context
169     | C.Lambda (b,s,t) ->
170        (match analyze_type context s with
171            `Sort _
172          | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
173          | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
174     | C.LetIn (b,s,t) ->
175        let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
176        "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
177     | C.Appl (C.MutConstruct _ as he::tl) ->
178        let hes = pp he context in
179        let stl = String.concat "," (clean_args context tl) in
180         "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
181     | C.Appl li ->
182        "(" ^ String.concat " " (clean_args context li) ^ ")"
183     | C.Const (uri,exp_named_subst) ->
184        qualified_name_of_uri current_module_name uri ^
185         pp_exp_named_subst exp_named_subst context
186     | C.MutInd (uri,n,exp_named_subst) ->
187        (try
188          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
189             C.InductiveDefinition (dl,_,_,_) ->
190              let (name,_,_,_) = get_nth dl (n+1) in
191               qualified_name_of_uri current_module_name
192                (UriManager.uri_of_string
193                  (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
194               pp_exp_named_subst exp_named_subst context
195           | _ -> raise CicExportationInternalError
196         with
197            Sys.Break as exn -> raise exn
198          | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
199        )
200     | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
201        (try
202          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
203             C.InductiveDefinition (dl,_,_,_) ->
204              let _,_,_,cons = get_nth dl (n1+1) in
205               let id,_ = get_nth cons n2 in
206                qualified_name_of_uri current_module_name ~capitalize:true
207                 (UriManager.uri_of_string
208                   (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
209                pp_exp_named_subst exp_named_subst context
210           | _ -> raise CicExportationInternalError
211         with
212            Sys.Break as exn -> raise exn
213          | _ ->
214           UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
215            string_of_int n2
216        )
217     | C.MutCase (uri,n1,ty,te,patterns) ->
218        let connames_and_argsno =
219         (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
220             C.InductiveDefinition (dl,_,paramsno,_) ->
221              let (_,_,_,cons) = get_nth dl (n1+1) in
222               List.map
223                (fun (id,ty) ->
224                  (* this is just an approximation since we do not have
225                     reduction yet! *)
226                  let rec count_prods toskip =
227                   function
228                      C.Prod (_,_,bo) when toskip > 0 ->
229                       count_prods (toskip - 1) bo
230                    | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
231                    | _ -> 0
232                  in
233                   qualified_name_of_uri current_module_name ~capitalize:true
234                    (UriManager.uri_of_string
235                      (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
236                   count_prods paramsno ty
237                ) cons
238           | _ -> raise CicExportationInternalError
239         )
240        in
241         let connames_and_argsno_and_patterns =
242          let rec combine =
243             function
244                [],[] -> []
245              | [],l -> List.map (fun x -> "???",0,Some x) l
246              | l,[] -> List.map (fun (x,no) -> x,no,None) l
247              | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
248          in
249           combine (connames_and_argsno,patterns)
250         in
251          "\n(match " ^ pp te context ^ " with \n" ^
252           (String.concat "\n | "
253            (List.map
254             (fun (x,argsno,y) ->
255               let rec aux argsno context =
256                function
257                   Cic.Lambda (name,ty,bo) when argsno > 0 ->
258                    let args,res = aux (argsno - 1) (Some (name,Cic.Decl ty)::context) bo in
259                     (match name with C.Anonymous -> "_" | C.Name s -> s)::args,
260                     res
261                 | t when argsno = 0 -> [],pp t context
262                 | t -> ["{" ^ string_of_int argsno ^ " args missing}"],pp t context
263               in
264                let pattern,body =
265                 match y with
266                    None -> x,""
267                  | Some y when argsno = 0 -> x,pp y context
268                  | Some y ->
269                     let args,body = aux argsno context y in
270                     let sargs = String.concat "," args in
271                      x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),body
272                in
273                 pattern ^ " -> " ^ body
274             ) connames_and_argsno_and_patterns)) ^
275           ")\n"
276     | C.Fix (no, funs) ->
277        let names =
278         List.rev
279          (List.map
280            (function (name,_,ty,_) ->
281              Some (C.Name name,Cic.Decl ty)) funs)
282        in
283          "\nlet rec " ^
284          List.fold_right
285           (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
286             pp bo (names@context) ^ i)
287           funs "" ^
288          " in " ^
289          (match get_nth names (no + 1) with
290              Some (Cic.Name n,_) -> n
291            | _ -> assert false) ^ "\n"
292     | C.CoFix (no,funs) ->
293        let names =
294         List.rev
295          (List.map
296            (function (name,ty,_) ->
297              Some (C.Name name,Cic.Decl ty)) funs)
298        in
299          "\nCoFix " ^ " {" ^
300          List.fold_right
301           (fun (name,ty,bo) i -> "\n" ^ name ^ 
302             " : " ^ pp ty context ^ " := \n" ^
303             pp bo (names@context) ^ i)
304           funs "" ^
305          "}\n"
306 and pp_exp_named_subst exp_named_subst context =
307  if exp_named_subst = [] then "" else
308   "\\subst[" ^
309    String.concat " ; " (
310     List.map
311      (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
312      exp_named_subst
313    ) ^ "]"
314 and clean_args context =
315  HExtlib.filter_map
316   (function t ->
317     match analyze_term context t with
318        `Type -> None
319      | `Proof -> None
320      | `Term -> Some pp t context)
321 in
322  pp
323 ;;
324
325 (* ppinductiveType (typename, inductive, arity, cons)                       *)
326 (* pretty-prints a single inductive definition                              *)
327 (* (typename, inductive, arity, cons)                                       *)
328 let ppinductiveType current_module_name (typename, inductive, arity, cons) =
329  let abstr,scons =
330   List.fold_right
331    (fun (id,ty) (abstr,i) ->
332      let rec args context =
333       function
334          Cic.Prod (n,s,t) ->
335            (match analyze_type context s with
336                `Sort Cic.Prop -> args ((Some (n,Cic.Decl s))::context) t
337              | `Statement
338              | `Sort _ ->
339                  let n =
340                   match n with
341                      Cic.Anonymous -> Cic.Anonymous
342                    | Cic.Name name -> Cic.Name ("'" ^ name) in
343                  let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
344                   (match n with
345                       Cic.Anonymous -> abstr
346                     | Cic.Name name -> name::abstr),
347                   args
348              | `Type ->
349                  let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
350                   abstr,pp current_module_name s context::args)
351        | _ -> [],[]
352      in
353       let abstr',sargs = args [] ty in
354       let sargs = String.concat " * " sargs in
355        abstr'@abstr,
356        String.capitalize id ^
357         (if sargs = "" then "" else " of " ^ sargs) ^
358         (if i = "" then "\n" else "\n | ") ^ i)
359    cons ([],"")
360   in
361    let abstr =
362     let s = String.concat "," abstr in
363     if s = "" then "" else "(" ^ s ^ ") "
364    in
365    "type " ^ abstr ^ typename ^ " =\n" ^ scons
366 ;;
367
368 let ppobj current_module_name obj =
369  let module C = Cic in
370  let module U = UriManager in
371   let pp = pp current_module_name in
372   match obj with
373     C.Constant (name, Some t1, t2, params, _) ->
374       (match analyze_type [] t2 with
375           `Sort Cic.Prop
376         | `Statement -> ""
377         | `Type
378         | `Sort _ -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n")
379    | C.Constant (name, None, ty, params, _) ->
380       (match analyze_type [] ty with
381           `Sort Cic.Prop
382         | `Statement -> ""
383         | `Sort _ -> "type " ^ ppid name ^ "\n"
384         | `Type -> "let " ^ ppid name ^ " = assert false\n")
385    | C.Variable (name, bo, ty, params, _) ->
386       "Variable " ^ name ^
387        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
388        ")" ^ ":\n" ^
389        pp ty [] ^ "\n" ^
390        (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
391    | C.CurrentProof (name, conjectures, value, ty, params, _) ->
392       "Current Proof of " ^ name ^
393        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
394        ")" ^ ":\n" ^
395       let separate s = if s = "" then "" else s ^ " ; " in
396        List.fold_right
397         (fun (n, context, t) i -> 
398           let conjectures',name_context =
399                  List.fold_right 
400                   (fun context_entry (i,name_context) ->
401                     (match context_entry with
402                         Some (n,C.Decl at) ->
403                          (separate i) ^
404                            ppname n ^ ":" ^
405                             pp ~metasenv:conjectures at name_context ^ " ",
406                             context_entry::name_context
407                       | Some (n,C.Def (at,None)) ->
408                          (separate i) ^
409                            ppname n ^ ":= " ^ pp ~metasenv:conjectures
410                             at name_context ^ " ",
411                             context_entry::name_context
412                       | None ->
413                          (separate i) ^ "_ :? _ ", context_entry::name_context
414                       | _ -> assert false)
415             ) context ("",[])
416           in
417            conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
418             pp ~metasenv:conjectures t name_context ^ "\n" ^ i
419         ) conjectures "" ^
420         "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
421           pp ~metasenv:conjectures ty [] 
422    | C.InductiveDefinition (l, params, nparams, _) ->
423       List.fold_right
424        (fun x i -> ppinductiveType current_module_name x ^ i) l "\n"
425 ;;
426
427 let ppobj current_module_name obj =
428  let res = ppobj current_module_name obj in
429   if res = "" then "" else res ^ ";;\n"
430 ;;