]> matita.cs.unibo.it Git - helm.git/blobdiff - components/cic_exportation/cicExportation.ml
Type arguments are better uncapitalized.
[helm.git] / components / cic_exportation / cicExportation.ml
index 8acb873a914399939d03b900914adfb7819077a9..37eff255b87c3132f644aad51c71e6b7cadc10ba 100644 (file)
@@ -44,12 +44,13 @@ let analyze_term context t =
 let analyze_type context t =
  let rec aux =
   function
-     Cic.Sort _ -> `Sort
-   | Cic.Prod (_,_,t) -> aux t
+     Cic.Sort s -> `Sort s
+   | Cic.Prod (_,_,t)
+   | Cic.Lambda (_,_,t) -> aux t
    | _ -> `SomethingElse
  in
  match aux t with
-    `Sort -> `Sort
+    `Sort _ as res -> res
   | `SomethingElse ->
       match
        fst(CicTypeChecker.type_of_aux' [] context t CicUniv.oblivion_ugraph)
@@ -84,11 +85,26 @@ let rec get_nth l n =
   | (_,_) -> raise NotEnoughElements
 ;;
 
+let qualified_name_of_uri current_module_name ?(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
+   name
+  else
+   String.capitalize filename ^ "." ^ name
+;;
+
 (* pp t l                                                                  *)
 (* 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 ?metasenv =
+let pp current_module_name ?metasenv =
 let rec pp t context =
  let module C = Cic in
    match t with
@@ -104,7 +120,8 @@ let rec pp t context =
          NotEnoughElements -> string_of_int (List.length context - n)
        end
     | C.Var (uri,exp_named_subst) ->
-       UriManager.name_of_uri uri ^ pp_exp_named_subst exp_named_subst context
+        qualified_name_of_uri current_module_name uri ^
+         pp_exp_named_subst exp_named_subst context
     | C.Meta (n,l1) ->
        (match metasenv with
            None ->
@@ -152,7 +169,7 @@ let rec pp t context =
     | C.Cast (v,t) -> pp v context
     | C.Lambda (b,s,t) ->
        (match analyze_type context s with
-           `Sort
+           `Sort _
          | `Statement -> pp t ((Some (b,Cic.Decl s))::context)
          | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")")
     | C.LetIn (b,s,t) ->
@@ -165,13 +182,17 @@ let rec pp t context =
     | C.Appl li ->
        "(" ^ String.concat " " (clean_args context li) ^ ")"
     | C.Const (uri,exp_named_subst) ->
-       ppid (UriManager.name_of_uri uri) ^ pp_exp_named_subst exp_named_subst context
+       qualified_name_of_uri current_module_name 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
-              ppid name ^ pp_exp_named_subst exp_named_subst context
+              qualified_name_of_uri current_module_name
+               (UriManager.uri_of_string
+                 (UriManager.buri_of_uri uri ^ "/" ^ name ^ ".con")) ^
+              pp_exp_named_subst exp_named_subst context
           | _ -> raise CicExportationInternalError
         with
            Sys.Break as exn -> raise exn
@@ -181,9 +202,12 @@ let rec pp t context =
        (try
          match fst(CicEnvironment.get_obj CicUniv.empty_ugraph uri) with
             C.InductiveDefinition (dl,_,_,_) ->
-             let (_,_,_,cons) = get_nth dl (n1+1) in
-              let (id,_) = get_nth cons n2 in
-               String.capitalize id ^ pp_exp_named_subst exp_named_subst context
+             let _,_,_,cons = get_nth dl (n1+1) in
+              let id,_ = get_nth cons n2 in
+               qualified_name_of_uri current_module_name ~capitalize:true
+                (UriManager.uri_of_string
+                  (UriManager.buri_of_uri uri ^ "/" ^ id ^ ".con")) ^
+               pp_exp_named_subst exp_named_subst context
           | _ -> raise CicExportationInternalError
         with
            Sys.Break as exn -> raise exn
@@ -192,61 +216,82 @@ 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
-                  String.capitalize id, 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
+       (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_name
+                          ~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
+                      [],[] -> []
+                    | [],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
-                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 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 =
+                       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
+                      in
+                       pattern ^ " -> " ^ body
+                   ) connames_and_argsno_and_patterns)) ^
+                 ")\n"))
     | C.Fix (no, funs) ->
        let names =
         List.rev
@@ -296,63 +341,92 @@ in
  pp
 ;;
 
+let ppty current_module_name =
+ let rec args 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
+         | `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
+              (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)
+   | _ -> [],[]
+ in
+  args
+;;
+
 (* ppinductiveType (typename, inductive, arity, cons)                       *)
 (* pretty-prints a single inductive definition                              *)
 (* (typename, inductive, arity, cons)                                       *)
-let ppinductiveType (typename, inductive, arity, cons) =
- let abstr,scons =
-  List.fold_right
-   (fun (id,ty) (abstr,i) ->
-     let rec args context =
-      function
-         Cic.Prod (n,s,t) ->
-           (match analyze_type context s with
-               `Statement
-             | `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
-                  (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 s context::args)
-       | _ -> [],[]
-     in
-      let abstr',sargs = args [] 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 ^ ") "
-   in
-   "type " ^ abstr ^ typename ^ " =\n" ^ scons
+let ppinductiveType current_module_name (typename, inductive, arity, cons) =
+ match analyze_type [] arity with
+    `Sort Cic.Prop -> ""
+  | `Statement
+  | `Type -> assert false
+  | `Sort _ ->
+    if cons = [] then
+     "type " ^ String.uncapitalize typename ^ " = unit (* empty type *)\n"
+    else (
+     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 "" else "\n | ") ^ i)
+       cons ([],"")
+      in
+       let abstr =
+        let s = String.concat "," abstr in
+        if s = "" then "" else "(" ^ s ^ ") "
+       in
+       "type " ^ abstr ^ String.uncapitalize typename ^ " =\n" ^ scons ^ "\n")
 ;;
 
-(* ppobj obj  returns a string with describing the cic object obj in a syntax *)
-(* similar to the one used by Coq                                             *)
-let ppobj obj =
+let ppobj current_module_name obj =
  let module C = Cic in
  let module U = UriManager in
+  let pp = pp current_module_name in
   match obj with
     C.Constant (name, Some t1, t2, params, _) ->
       (match analyze_type [] t2 with
-          `Statement -> ""
-        | `Type
-        | `Sort -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n")
+          `Sort Cic.Prop
+        | `Statement -> ""
+        | `Type -> "let " ^ ppid name ^ " =\n" ^ pp t1 [] ^ "\n"
+        | `Sort _ ->
+            match analyze_type [] t1 with
+               `Sort Cic.Prop -> ""
+             | _ ->
+               let abstr,args = ppty current_module_name [] t1 in
+               let abstr =
+                let s = String.concat "," abstr in
+                if s = "" then "" else "(" ^ s ^ ") "
+               in
+                "type " ^ abstr ^ ppid name ^ " = " ^ String.concat "->" args ^
+                "\n")
    | C.Constant (name, None, ty, params, _) ->
       (match analyze_type [] ty with
-          `Statement -> ""
-        | `Sort -> "type " ^ ppid name ^ "\n"
+          `Sort Cic.Prop
+        | `Statement -> ""
+        | `Sort _ -> "type " ^ ppid name ^ "\n"
         | `Type -> "let " ^ ppid name ^ " = assert false\n")
    | C.Variable (name, bo, ty, params, _) ->
       "Variable " ^ name ^
@@ -392,10 +466,11 @@ let ppobj obj =
         "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^
           pp ~metasenv:conjectures ty [] 
    | C.InductiveDefinition (l, params, nparams, _) ->
-      List.fold_right (fun x i -> ppinductiveType x ^ i) l "\n"
+      List.fold_right
+       (fun x i -> ppinductiveType current_module_name x ^ i) l ""
 ;;
 
-let ppobj obj =
- let res = ppobj obj in
-  if res = "" then "" else res ^ ";;\n"
+let ppobj current_module_name obj =
+ let res = ppobj current_module_name obj in
+  if res = "" then "" else res ^ ";;\n\n"
 ;;