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