]> matita.cs.unibo.it Git - helm.git/blobdiff - components/cic_exportation/cicExportation.ml
Handling of left parameters of constructors/indutive type definitions improved.
[helm.git] / components / cic_exportation / cicExportation.ml
index e5f1face1e9eb854049ba089b723f8d0a8705321..7be9ea3d9f0e30b566fb67e1e6b793b86b76d9f5 100644 (file)
@@ -62,7 +62,8 @@ let analyze_type context t =
 let ppid =
  let reserved =
   [ "to";
-    "mod"
+    "mod";
+    "val"
   ]
  in
   function n ->
@@ -85,16 +86,18 @@ let rec get_nth l n =
   | (_,_) -> raise NotEnoughElements
 ;;
 
-let qualified_name_of_uri current_module_name ?(capitalize=false) uri =
+let qualified_name_of_uri current_module_uri ?(capitalize=false) uri =
  let name =
   if capitalize then
    String.capitalize (UriManager.name_of_uri uri)
   else
    ppid (UriManager.name_of_uri uri) in
- let buri = UriManager.buri_of_uri uri in
- let index = String.rindex buri '/' in
- let filename = String.sub buri (index + 1) (String.length buri - index - 1) in
-  if current_module_name = filename then
+  let filename =
+   let suri = UriManager.buri_of_uri uri in
+   let s = String.sub suri 5 (String.length suri - 5) in
+   let s = Pcre.replace ~pat:"/" ~templ:"_" s in
+    String.uncapitalize s in
+  if current_module_uri = UriManager.buri_of_uri uri then
    name
   else
    String.capitalize filename ^ "." ^ name
@@ -104,7 +107,7 @@ let qualified_name_of_uri current_module_name ?(capitalize=false) uri =
 (* pretty-prints a term t of cic in an environment l where l is a list of  *)
 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
 (* name associated to the greatest DeBrujin index in t                     *)
-let pp current_module_name ?metasenv =
+let pp current_module_uri ?metasenv =
 let rec pp t context =
  let module C = Cic in
    match t with
@@ -120,7 +123,7 @@ let rec pp t context =
          NotEnoughElements -> string_of_int (List.length context - n)
        end
     | C.Var (uri,exp_named_subst) ->
-        qualified_name_of_uri current_module_name uri ^
+        qualified_name_of_uri current_module_uri uri ^
          pp_exp_named_subst exp_named_subst context
     | C.Meta (n,l1) ->
        (match metasenv with
@@ -163,8 +166,10 @@ let rec pp t context =
     | C.Implicit _ -> "?"
     | C.Prod (b,s,t) ->
        (match b with
-          C.Name n -> "(\\forall " ^ n ^ ":" ^ pp s context ^ "." ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
-        | C.Anonymous -> "(" ^ pp s context ^ "\\to " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
+          C.Name n ->
+           let n = "'" ^ String.uncapitalize n in
+            "(" ^ pp s context ^ " -> " ^ pp t ((Some (Cic.Name n,Cic.Decl s))::context) ^ ")"
+        | C.Anonymous -> "(" ^ pp s context ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")"
        )
     | C.Cast (v,t) -> pp v context
     | C.Lambda (b,s,t) ->
@@ -175,21 +180,29 @@ let rec pp t context =
     | C.LetIn (b,s,t) ->
        let ty,_ = CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph in
        "(let " ^ ppname b ^ " = " ^ pp s context ^ " in " ^ pp t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")"
-    | C.Appl (C.MutConstruct _ as he::tl) ->
+    | C.Appl (C.MutInd _ as he::tl) ->
        let hes = pp he context in
-       let stl = String.concat "," (clean_args context tl) in
+       let stl = String.concat "," (clean_args_for_ty context tl) in
+        (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes
+    | C.Appl (C.MutConstruct (uri,n,_,_) as he::tl) ->
+       let nparams =
+        match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
+           C.InductiveDefinition (_,_,nparams,_) -> nparams
+         | _ -> assert false in
+       let hes = pp he context in
+       let stl = String.concat "," (clean_args nparams context tl) in
         "(" ^ hes ^ (if stl = "" then "" else "(" ^ stl ^ ")") ^ ")"
     | C.Appl li ->
-       "(" ^ String.concat " " (clean_args context li) ^ ")"
+       "(" ^ String.concat " " (clean_args context li) ^ ")"
     | C.Const (uri,exp_named_subst) ->
-       qualified_name_of_uri current_module_name uri ^
+       qualified_name_of_uri current_module_uri uri ^
         pp_exp_named_subst exp_named_subst context
     | C.MutInd (uri,n,exp_named_subst) ->
        (try
          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
             C.InductiveDefinition (dl,_,_,_) ->
              let (name,_,_,_) = get_nth dl (n+1) in
-              qualified_name_of_uri current_module_name
+              qualified_name_of_uri current_module_uri
                (UriManager.uri_of_string
                  (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
               pp_exp_named_subst exp_named_subst context
@@ -204,7 +217,7 @@ let rec pp t context =
             C.InductiveDefinition (dl,_,_,_) ->
              let _,_,_,cons = get_nth dl (n1+1) in
               let id,_ = get_nth cons n2 in
-               qualified_name_of_uri current_module_name ~capitalize:true
+               qualified_name_of_uri current_module_uri ~capitalize:true
                 (UriManager.uri_of_string
                   (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
                pp_exp_named_subst exp_named_subst context
@@ -216,64 +229,87 @@ let rec pp t context =
            string_of_int n2
        )
     | C.MutCase (uri,n1,ty,te,patterns) ->
-       let connames_and_argsno =
-        (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
-            C.InductiveDefinition (dl,_,paramsno,_) ->
-             let (_,_,_,cons) = get_nth dl (n1+1) in
-              List.map
-               (fun (id,ty) ->
-                 (* this is just an approximation since we do not have
-                    reduction yet! *)
-                 let rec count_prods toskip =
-                  function
-                     C.Prod (_,_,bo) when toskip > 0 ->
-                      count_prods (toskip - 1) bo
-                   | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
-                   | _ -> 0
-                 in
-                  qualified_name_of_uri current_module_name ~capitalize:true
-                   (UriManager.uri_of_string
-                     (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
-                  count_prods paramsno ty
-               ) cons
-          | _ -> raise CicExportationInternalError
-        )
-       in
-        let connames_and_argsno_and_patterns =
-         let rec combine =
-            function
-               [],[] -> []
-             | [],l -> List.map (fun x -> "???",0,Some x) l
-             | l,[] -> List.map (fun (x,no) -> x,no,None) l
-             | (x,no)::tlx,y::tly -> (x,no,Some y)::(combine (tlx,tly))
-         in
-          combine (connames_and_argsno,patterns)
-        in
-         "\n(match " ^ pp te context ^ " with \n" ^
-          (String.concat "\n | "
-           (List.map
-            (fun (x,argsno,y) ->
-              let rec aux argsno context =
-               function
-                  Cic.Lambda (name,ty,bo) when argsno > 0 ->
-                   let args,res = aux (argsno - 1) (Some (name,Cic.Decl ty)::context) bo in
-                    (match name with C.Anonymous -> "_" | C.Name s -> s)::args,
-                    res
-                | t when argsno = 0 -> [],pp t context
-                | t -> ["{" ^ string_of_int argsno ^ " args missing}"],pp t context
+       let paramsno =
+        match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
+           C.InductiveDefinition (_,_,paramsno,_) -> paramsno
+         | _ -> assert false in
+       (match analyze_term context te with
+           `Type -> assert false
+         | `Proof ->
+             (match patterns with
+                 [] -> "assert false"   (* empty type elimination *)
+               | [he] -> pp he context  (* singleton elimination *)
+               | _ -> assert false)
+         | `Term ->
+            if patterns = [] then "assert false"
+            else
+             (let connames_and_argsno =
+               (match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
+                   C.InductiveDefinition (dl,_,paramsno,_) ->
+                    let (_,_,_,cons) = get_nth dl (n1+1) in
+                     List.map
+                      (fun (id,ty) ->
+                        (* this is just an approximation since we do not have
+                           reduction yet! *)
+                        let rec count_prods toskip =
+                         function
+                            C.Prod (_,_,bo) when toskip > 0 ->
+                             count_prods (toskip - 1) bo
+                          | C.Prod (_,_,bo) -> 1 + count_prods 0 bo
+                          | _ -> 0
+                        in
+                         qualified_name_of_uri current_module_uri
+                          ~capitalize:true
+                          (UriManager.uri_of_string
+                            (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")),
+                         count_prods paramsno ty
+                      ) cons
+                 | _ -> raise CicExportationInternalError
+               )
               in
-               let pattern,body =
-                match y with
-                   None -> x,""
-                 | Some y when argsno = 0 -> x,pp y context
-                 | Some y ->
-                    let args,body = aux argsno context y in
-                    let sargs = String.concat "," args in
-                     x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),body
+               let connames_and_argsno_and_patterns =
+                let rec combine =
+                   function
+                      [],[] -> []
+                    | (x,no)::tlx,y::tly -> (x,no,y)::(combine (tlx,tly))
+                    | _,_ -> assert false
+                in
+                 combine (connames_and_argsno,patterns)
                in
-                pattern ^ " -> " ^ body
-            ) connames_and_argsno_and_patterns)) ^
-          ")\n"
+                "\n(match " ^ pp te context ^ " with \n   " ^
+                 (String.concat "\n | "
+                  (List.map
+                   (fun (x,argsno,y) ->
+                     let rec aux argsno context =
+                      function
+                         Cic.Lambda (name,ty,bo) when argsno > 0 ->
+                          let args,res =
+                           aux (argsno - 1) (Some (name,Cic.Decl ty)::context)
+                            bo
+                          in
+                           (match analyze_type context ty with
+                               `Statement
+                             | `Sort _ -> args,res
+                             | `Type ->
+                                 (match name with
+                                     C.Anonymous -> "_"
+                                   | C.Name s -> s)::args,res)
+                       | t when argsno = 0 -> [],pp t context
+                       | t ->
+                          ["{" ^ string_of_int argsno ^ " args missing}"],
+                           pp t context
+                     in
+                      let pattern,body =
+                       if argsno = 0 then x,pp y context
+                       else
+                        let args,body = aux argsno context y in
+                        let sargs = String.concat "," args in
+                         x ^ (if sargs = "" then "" else "(" ^ sargs^ ")"),
+                          body
+                      in
+                       pattern ^ " -> " ^ body
+                   ) connames_and_argsno_and_patterns)) ^
+                 ")\n"))
     | C.Fix (no, funs) ->
        let names =
         List.rev
@@ -281,7 +317,7 @@ let rec pp t context =
            (function (name,_,ty,_) ->
              Some (C.Name name,Cic.Decl ty)) funs)
        in
-         "\nlet rec " ^
+         "let rec " ^
          List.fold_right
           (fun (name,ind,ty,bo) i -> name ^ " = \n" ^
             pp bo (names@context) ^ i)
@@ -289,7 +325,7 @@ let rec pp t context =
          " in " ^
          (match get_nth names (no + 1) with
              Some (Cic.Name n,_) -> n
-           | _ -> assert false) ^ "\n"
+           | _ -> assert false)
     | C.CoFix (no,funs) ->
        let names =
         List.rev
@@ -312,37 +348,60 @@ and pp_exp_named_subst exp_named_subst context =
      (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context)
      exp_named_subst
    ) ^ "]"
-and clean_args context =
+and clean_args nparams context =
+ let nparams = ref nparams in
+ HExtlib.filter_map
+  (function t ->
+    decr nparams;
+    match analyze_term context t with
+       `Term when !nparams < 0 -> Some pp t context
+     | `Term
+     | `Type
+     | `Proof -> None)
+and clean_args_for_ty context =
  HExtlib.filter_map
   (function t ->
     match analyze_term context t with
-       `Type -> None
+       `Type -> Some pp t context
      | `Proof -> None
-     | `Term -> Some pp t context)
+     | `Term -> None)
 in
  pp
 ;;
 
-let ppty current_module_name =
- let rec args context =
+let ppty current_module_uri =
+ (* nparams is the number of left arguments
+    left arguments should either become parameters or be skipped altogether *)
+ let rec args nparams context =
   function
      Cic.Prod (n,s,t) ->
+      let n =
+       match n with
+          Cic.Anonymous -> Cic.Anonymous
+        | Cic.Name n -> Cic.Name (String.uncapitalize n)
+      in
        (match analyze_type context s with
-           `Sort Cic.Prop -> args ((Some (n,Cic.Decl s))::context) t
-         | `Statement
+           `Statement
+         | `Sort Cic.Prop ->
+             args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
+         | `Type when nparams > 0 ->
+             args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
+         | `Type ->
+             let abstr,args =
+              args (nparams - 1) ((Some (n,Cic.Decl s))::context) t in
+               abstr,pp current_module_uri s context::args
          | `Sort _ ->
              let n =
               match n with
                  Cic.Anonymous -> Cic.Anonymous
                | Cic.Name name -> Cic.Name ("'" ^ name) in
-             let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
+             let abstr,args =
+              args (nparams - 1) ((Some (n,Cic.Decl s))::context) t
+             in
               (match n with
                   Cic.Anonymous -> abstr
                 | Cic.Name name -> name::abstr),
-              args
-         | `Type ->
-             let abstr,args = args ((Some (n,Cic.Decl s))::context) t in
-              abstr,pp current_module_name s context::args)
+              args)
    | _ -> [],[]
  in
   args
@@ -351,34 +410,38 @@ let ppty current_module_name =
 (* ppinductiveType (typename, inductive, arity, cons)                       *)
 (* pretty-prints a single inductive definition                              *)
 (* (typename, inductive, arity, cons)                                       *)
-let ppinductiveType current_module_name (typename, inductive, arity, cons) =
+let ppinductiveType current_module_uri nparams (typename, inductive, arity, cons)
+=
  match analyze_type [] arity with
     `Sort Cic.Prop -> ""
   | `Statement
   | `Type -> assert false
   | `Sort _ ->
-    let abstr,scons =
-     List.fold_right
-      (fun (id,ty) (abstr,i) ->
-         let abstr',sargs = ppty current_module_name [] ty in
-         let sargs = String.concat " * " sargs in
-          abstr'@abstr,
-          String.capitalize id ^
-           (if sargs = "" then "" else " of " ^ sargs) ^
-           (if i = "" then "\n" else "\n | ") ^ i)
-      cons ([],"")
-     in
-      let abstr =
-       let s = String.concat "," abstr in
-       if s = "" then "" else "(" ^ s ^ ") "
+    if cons = [] then
+     "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
+    else (
+     let abstr,scons =
+      List.fold_right
+       (fun (id,ty) (_abstr,i) -> (* we should verify _abstr = abstr' *)
+          let abstr',sargs = ppty current_module_uri nparams [] ty in
+          let sargs = String.concat " * " sargs in
+           abstr',
+           String.capitalize id ^
+            (if sargs = "" then "" else " of " ^ sargs) ^
+            (if i = "" then "" else "\n | ") ^ i)
+       cons ([],"")
       in
-      "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n"
+       let abstr =
+        let s = String.concat "," abstr in
+        if s = "" then "" else "(" ^ s ^ ") "
+       in
+       "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
 ;;
 
-let ppobj current_module_name obj =
+let ppobj current_module_uri obj =
  let module C = Cic in
  let module U = UriManager in
-  let pp = pp current_module_name in
+  let pp = pp current_module_uri in
   match obj with
     C.Constant (name, Some t1, t2, params, _) ->
       (match analyze_type [] t2 with
@@ -389,7 +452,7 @@ let ppobj current_module_name obj =
             match analyze_type [] t1 with
                `Sort Cic.Prop -> ""
              | _ ->
-               let abstr,args = ppty current_module_name [] t1 in
+               let abstr,args = ppty current_module_uri 0 [] t1 in
                let abstr =
                 let s = String.concat "," abstr in
                 if s = "" then "" else "(" ^ s ^ ") "
@@ -441,10 +504,10 @@ let ppobj current_module_name obj =
           pp ~metasenv:conjectures ty [] 
    | C.InductiveDefinition (l, params, nparams, _) ->
       List.fold_right
-       (fun x i -> ppinductiveType current_module_name x ^ i) l ""
+       (fun x i -> ppinductiveType current_module_uri nparams x ^ i) l ""
 ;;
 
-let ppobj current_module_name obj =
- let res = ppobj current_module_name obj in
-  if res = "" then "" else res ^ ";;\n"
+let ppobj current_module_uri obj =
+ let res = ppobj current_module_uri obj in
+  if res = "" then "" else res ^ ";;\n\n"
 ;;