]> matita.cs.unibo.it Git - helm.git/blob - components/cic_exportation/cicExportation.ml
1. type definitions of propositions are no longer exported even if parametric
[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)
49    | Cic.Lambda (_,_,t) -> aux t
50    | _ -> `SomethingElse
51  in
52  match aux t with
53     `Sort _ as res -> res
54   | `SomethingElse ->
55       match
56        fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
57       with
58           Cic.Sort Cic.Prop -> `Statement
59        | _ -> `Type
60 ;;
61
62 let ppid =
63  let reserved =
64   [ "to";
65     "mod"
66   ]
67  in
68   function n ->
69    let n = String.uncapitalize n in
70     if List.mem n reserved then n ^ "_" else n
71 ;;
72
73 let ppname =
74  function
75     Cic.Name s     -> ppid s
76   | Cic.Anonymous  -> "_"
77 ;;
78
79 (* get_nth l n   returns the nth element of the list l if it exists or *)
80 (* raises NotEnoughElements if l has less than n elements             *)
81 let rec get_nth l n =
82  match (n,l) with
83     (1, he::_) -> he
84   | (n, he::tail) when n > 1 -> get_nth tail (n-1)
85   | (_,_) -> raise NotEnoughElements
86 ;;
87
88 let qualified_name_of_uri current_module_name ?(capitalize=false) uri =
89  let name =
90   if capitalize then
91    String.capitalize (UriManager.name_of_uri uri)
92   else
93    ppid (UriManager.name_of_uri uri) in
94  let buri = UriManager.buri_of_uri uri in
95  let index = String.rindex buri '/' in
96  let filename = String.sub buri (index + 1) (String.length buri - index - 1) in
97   if current_module_name = filename then
98    name
99   else
100    String.capitalize filename ^ "." ^ name
101 ;;
102
103 (* pp t l                                                                  *)
104 (* pretty-prints a term t of cic in an environment l where l is a list of  *)
105 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
106 (* name associated to the greatest DeBrujin index in t                     *)
107 let pp current_module_name ?metasenv =
108 let rec pp t context =
109  let module C = Cic in
110    match t with
111       C.Rel n ->
112        begin
113         try
114          (match get_nth context n with
115              Some (C.Name s,_) -> ppid s
116            | Some (C.Anonymous,_) -> "__" ^ string_of_int n
117            | None -> "_hidden_" ^ string_of_int n
118          )
119         with
120          NotEnoughElements -> string_of_int (List.length context - n)
121        end
122     | C.Var (uri,exp_named_subst) ->
123         qualified_name_of_uri current_module_name uri ^
124          pp_exp_named_subst exp_named_subst context
125     | C.Meta (n,l1) ->
126        (match metasenv with
127            None ->
128             "?" ^ (string_of_int n) ^ "[" ^ 
129              String.concat " ; "
130               (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
131              "]"
132          | Some metasenv ->
133             try
134              let _,context,_ = CicUtil.lookup_meta n metasenv in
135               "?" ^ (string_of_int n) ^ "[" ^ 
136                String.concat " ; "
137                 (List.rev
138                   (List.map2
139                     (fun x y ->
140                       match x,y with
141                          _, None
142                        | None, _ -> "_"
143                        | Some _, Some t -> pp t context
144                     ) context l1)) ^
145                "]"
146             with
147               CicUtil.Meta_not_found _ 
148             | Invalid_argument _ ->
149               "???" ^ (string_of_int n) ^ "[" ^ 
150                String.concat " ; "
151                 (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^
152                "]"
153        )
154     | C.Sort s ->
155        (match s with
156            C.Prop  -> "Prop"
157          | C.Set   -> "Set"
158          | C.Type _ -> "Type"
159          (*| C.Type u -> ("Type" ^ CicUniv.string_of_universe u)*)
160          | C.CProp -> "CProp" 
161        )
162     | C.Implicit (Some `Hole) -> "%"
163     | C.Implicit _ -> "?"
164     | C.Prod (b,s,t) ->
165        (match b with
166           C.Name n -> "(\\forall " ^ n ^ ":" ^ pp s context ^ "." ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
167         | C.Anonymous -> "(" ^ pp s context ^ "\\to " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
168        )
169     | C.Cast (v,t) -> pp v context
170     | C.Lambda (b,s,t) ->
171        (match analyze_type context s with
172            `Sort _
173          | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
174          | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
175     | C.LetIn (b,s,t) ->
176        let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
177        "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
178     | C.Appl (C.MutConstruct _ as he::tl) ->
179        let hes = pp he context in
180        let stl = String.concat "," (clean_args context tl) in
181         "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
182     | C.Appl li ->
183        "(" ^ String.concat " " (clean_args context li) ^ ")"
184     | C.Const (uri,exp_named_subst) ->
185        qualified_name_of_uri current_module_name uri ^
186         pp_exp_named_subst exp_named_subst context
187     | C.MutInd (uri,n,exp_named_subst) ->
188        (try
189          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
190             C.InductiveDefinition (dl,_,_,_) ->
191              let (name,_,_,_) = get_nth dl (n+1) in
192               qualified_name_of_uri current_module_name
193                (UriManager.uri_of_string
194                  (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
195               pp_exp_named_subst exp_named_subst context
196           | _ -> raise CicExportationInternalError
197         with
198            Sys.Break as exn -> raise exn
199          | _ -> UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n + 1)
200        )
201     | C.MutConstruct (uri,n1,n2,exp_named_subst) ->
202        (try
203          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
204             C.InductiveDefinition (dl,_,_,_) ->
205              let _,_,_,cons = get_nth dl (n1+1) in
206               let id,_ = get_nth cons n2 in
207                qualified_name_of_uri current_module_name ~capitalize:true
208                 (UriManager.uri_of_string
209                   (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
210                pp_exp_named_subst exp_named_subst context
211           | _ -> raise CicExportationInternalError
212         with
213            Sys.Break as exn -> raise exn
214          | _ ->
215           UriManager.string_of_uri uri ^ "#1/" ^ string_of_int (n1 + 1) ^ "/" ^
216            string_of_int n2
217        )
218     | C.MutCase (uri,n1,ty,te,patterns) ->
219        let connames_and_argsno =
220         (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
221             C.InductiveDefinition (dl,_,paramsno,_) ->
222              let (_,_,_,cons) = get_nth dl (n1+1) in
223               List.map
224                (fun (id,ty) ->
225                  (* this is just an approximation since we do not have
226                     reduction yet! *)
227                  let rec count_prods toskip =
228                   function
229                      C.Prod (_,_,bo) when toskip > 0 ->
230                       count_prods (toskip - 1) bo
231                    | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
232                    | _ -> 0
233                  in
234                   qualified_name_of_uri current_module_name ~capitalize:true
235                    (UriManager.uri_of_string
236                      (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
237                   count_prods paramsno ty
238                ) cons
239           | _ -> raise CicExportationInternalError
240         )
241        in
242         let connames_and_argsno_and_patterns =
243          let rec combine =
244             function
245                [],[] -> []
246              | [],l -> List.map (fun x -> "???",0,Some x) l
247              | l,[] -> List.map (fun (x,no) -> x,no,None) l
248              | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
249          in
250           combine (connames_and_argsno,patterns)
251         in
252          "\n(match " ^ pp te context ^ " with \n" ^
253           (String.concat "\n | "
254            (List.map
255             (fun (x,argsno,y) ->
256               let rec aux argsno context =
257                function
258                   Cic.Lambda (name,ty,bo) when argsno > 0 ->
259                    let args,res = aux (argsno - 1) (Some (name,Cic.Decl ty)::context) bo in
260                     (match name with C.Anonymous -> "_" | C.Name s -> s)::args,
261                     res
262                 | t when argsno = 0 -> [],pp t context
263                 | t -> ["{" ^ string_of_int argsno ^ " args missing}"],pp t context
264               in
265                let pattern,body =
266                 match y with
267                    None -> x,""
268                  | Some y when argsno = 0 -> x,pp y context
269                  | Some y ->
270                     let args,body = aux argsno context y in
271                     let sargs = String.concat "," args in
272                      x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),body
273                in
274                 pattern ^ " -> " ^ body
275             ) connames_and_argsno_and_patterns)) ^
276           ")\n"
277     | C.Fix (no, funs) ->
278        let names =
279         List.rev
280          (List.map
281            (function (name,_,ty,_) ->
282              Some (C.Name name,Cic.Decl ty)) funs)
283        in
284          "\nlet rec " ^
285          List.fold_right
286           (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
287             pp bo (names@context) ^ i)
288           funs "" ^
289          " in " ^
290          (match get_nth names (no + 1) with
291              Some (Cic.Name n,_) -> n
292            | _ -> assert false) ^ "\n"
293     | C.CoFix (no,funs) ->
294        let names =
295         List.rev
296          (List.map
297            (function (name,ty,_) ->
298              Some (C.Name name,Cic.Decl ty)) funs)
299        in
300          "\nCoFix " ^ " {" ^
301          List.fold_right
302           (fun (name,ty,bo) i -> "\n" ^ name ^ 
303             " : " ^ pp ty context ^ " := \n" ^
304             pp bo (names@context) ^ i)
305           funs "" ^
306          "}\n"
307 and pp_exp_named_subst exp_named_subst context =
308  if exp_named_subst = [] then "" else
309   "\\subst[" ^
310    String.concat " ; " (
311     List.map
312      (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
313      exp_named_subst
314    ) ^ "]"
315 and clean_args context =
316  HExtlib.filter_map
317   (function t ->
318     match analyze_term context t with
319        `Type -> None
320      | `Proof -> None
321      | `Term -> Some pp t context)
322 in
323  pp
324 ;;
325
326 let ppty current_module_name =
327  let rec args context =
328   function
329      Cic.Prod (n,s,t) ->
330        (match analyze_type context s with
331            `Sort Cic.Prop -> args ((Some (n,Cic.Decl s))::context) t
332          | `Statement
333          | `Sort _ ->
334              let n =
335               match n with
336                  Cic.Anonymous -> Cic.Anonymous
337                | Cic.Name name -> Cic.Name ("'" ^ name) in
338              let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
339               (match n with
340                   Cic.Anonymous -> abstr
341                 | Cic.Name name -> name::abstr),
342               args
343          | `Type ->
344              let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
345               abstr,pp current_module_name s context::args)
346    | _ -> [],[]
347  in
348   args
349 ;;
350
351 (* ppinductiveType (typename, inductive, arity, cons)                       *)
352 (* pretty-prints a single inductive definition                              *)
353 (* (typename, inductive, arity, cons)                                       *)
354 let ppinductiveType current_module_name (typename, inductive, arity, cons) =
355  match analyze_type [] arity with
356     `Sort Cic.Prop -> ""
357   | `Statement
358   | `Type -> assert false
359   | `Sort _ ->
360     let abstr,scons =
361      List.fold_right
362       (fun (id,ty) (abstr,i) ->
363          let abstr',sargs = ppty current_module_name [] ty in
364          let sargs = String.concat " * " sargs in
365           abstr'@abstr,
366           String.capitalize id ^
367            (if sargs = "" then "" else " of " ^ sargs) ^
368            (if i = "" then "\n" else "\n | ") ^ i)
369       cons ([],"")
370      in
371       let abstr =
372        let s = String.concat "," abstr in
373        if s = "" then "" else "(" ^ s ^ ") "
374       in
375       "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n"
376 ;;
377
378 let ppobj current_module_name obj =
379  let module C = Cic in
380  let module U = UriManager in
381   let pp = pp current_module_name in
382   match obj with
383     C.Constant (name, Some t1, t2, params, _) ->
384       (match analyze_type [] t2 with
385           `Sort Cic.Prop
386         | `Statement -> ""
387         | `Type -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n"
388         | `Sort _ ->
389             match analyze_type [] t1 with
390                `Sort Cic.Prop -> ""
391              | _ ->
392                let abstr,args = ppty current_module_name [] t1 in
393                let abstr =
394                 let s = String.concat "," abstr in
395                 if s = "" then "" else "(" ^ s ^ ") "
396                in
397                 "type " ^ abstr ^ ppid name ^ " = " ^ String.concat "->" args ^
398                 "\n")
399    | C.Constant (name, None, ty, params, _) ->
400       (match analyze_type [] ty with
401           `Sort Cic.Prop
402         | `Statement -> ""
403         | `Sort _ -> "type " ^ ppid name ^ "\n"
404         | `Type -> "let " ^ ppid name ^ " = assert false\n")
405    | C.Variable (name, bo, ty, params, _) ->
406       "Variable " ^ name ^
407        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
408        ")" ^ ":\n" ^
409        pp ty [] ^ "\n" ^
410        (match bo with None -> "" | Some bo -> ":= " ^ pp bo [])
411    | C.CurrentProof (name, conjectures, value, ty, params, _) ->
412       "Current Proof of " ^ name ^
413        "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^
414        ")" ^ ":\n" ^
415       let separate s = if s = "" then "" else s ^ " ; " in
416        List.fold_right
417         (fun (n, context, t) i -> 
418           let conjectures',name_context =
419                  List.fold_right 
420                   (fun context_entry (i,name_context) ->
421                     (match context_entry with
422                         Some (n,C.Decl at) ->
423                          (separate i) ^
424                            ppname n ^ ":" ^
425                             pp ~metasenv:conjectures at name_context ^ " ",
426                             context_entry::name_context
427                       | Some (n,C.Def (at,None)) ->
428                          (separate i) ^
429                            ppname n ^ ":= " ^ pp ~metasenv:conjectures
430                             at name_context ^ " ",
431                             context_entry::name_context
432                       | None ->
433                          (separate i) ^ "_ :? _ ", context_entry::name_context
434                       | _ -> assert false)
435             ) context ("",[])
436           in
437            conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^
438             pp ~metasenv:conjectures t name_context ^ "\n" ^ i
439         ) conjectures "" ^
440         "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
441           pp ~metasenv:conjectures ty [] 
442    | C.InductiveDefinition (l, params, nparams, _) ->
443       List.fold_right
444        (fun x i -> ppinductiveType current_module_name x ^ i) l ""
445 ;;
446
447 let ppobj current_module_name obj =
448  let res = ppobj current_module_name obj in
449   if res = "" then "" else res ^ ";;\n"
450 ;;