X-Git-Url: http://matita.cs.unibo.it/gitweb/?a=blobdiff_plain;f=components%2Fcic_exportation%2FcicExportation.ml;h=8af058f9692d3bb8590fac15264d8236893c859b;hb=24dd4569daf1d35bffaa813b8164058d8643f14d;hp=8acb873a914399939d03b900914adfb7819077a9;hpb=530f6d29b866eb41b1a4195b15df8feaf6d5d6de;p=helm.git diff --git a/components/cic_exportation/cicExportation.ml b/components/cic_exportation/cicExportation.ml index 8acb873a9..8af058f96 100644 --- a/components/cic_exportation/cicExportation.ml +++ b/components/cic_exportation/cicExportation.ml @@ -44,12 +44,12 @@ let analyze_term context t = let analyze_type context t = let rec aux = function - Cic.Sort _ -> `Sort + Cic.Sort s -> `Sort s | Cic.Prod (_,_,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) @@ -61,7 +61,10 @@ let analyze_type context t = let ppid = let reserved = [ "to"; - "mod" + "mod"; + "val"; + "in"; + "function" ] in function n -> @@ -84,12 +87,25 @@ let rec get_nth l n = | (_,_) -> raise NotEnoughElements ;; -(* 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 rec pp t context = +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 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 +;; + +let pp current_module_uri ?metasenv ~in_type = +let rec pp ~in_type t context = let module C = Cic in match t with C.Rel n -> @@ -104,13 +120,17 @@ 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_uri uri ^ + pp_exp_named_subst exp_named_subst context | C.Meta (n,l1) -> (match metasenv with None -> "?" ^ (string_of_int n) ^ "[" ^ String.concat " ; " - (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^ + (List.rev_map + (function + None -> "_" + | Some t -> pp ~in_type:false t context) l1) ^ "]" | Some metasenv -> try @@ -123,7 +143,7 @@ let rec pp t context = match x,y with _, None | None, _ -> "_" - | Some _, Some t -> pp t context + | Some _, Some t -> pp ~in_type:false t context ) context l1)) ^ "]" with @@ -131,7 +151,8 @@ let rec pp t context = | Invalid_argument _ -> "???" ^ (string_of_int n) ^ "[" ^ String.concat " ; " - (List.rev_map (function None -> "_" | Some t -> pp t context) l1) ^ + (List.rev_map (function None -> "_" | Some t -> + pp ~in_type:false t context) l1) ^ "]" ) | C.Sort s -> @@ -146,32 +167,55 @@ 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.Cast (v,t) -> pp v context + C.Name n -> + let n = "'" ^ String.uncapitalize n in + "(" ^ pp ~in_type:true s context ^ " -> " ^ + pp ~in_type:true t ((Some (Cic.Name n,Cic.Decl s))::context) ^ ")" + | C.Anonymous -> + "(" ^ pp ~in_type:true s context ^ " -> " ^ + pp ~in_type:true t ((Some (b,Cic.Decl s))::context) ^ ")") + | C.Cast (v,t) -> pp ~in_type v context | C.Lambda (b,s,t) -> (match analyze_type context s with - `Sort - | `Statement -> pp t ((Some (b,Cic.Decl s))::context) - | `Type -> "(function " ^ ppname b ^ " -> " ^ pp t ((Some (b,Cic.Decl s))::context) ^ ")") + `Sort _ + | `Statement -> pp ~in_type t ((Some (b,Cic.Decl s))::context) + | `Type -> + "(function " ^ ppname b ^ " -> " ^ + pp ~in_type t ((Some (b,Cic.Decl s))::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) -> - let hes = pp he context in - let stl = String.concat "," (clean_args context tl) in + let ty,_ = CicTypeChecker.type_of_aux' [] context s CicUniv.oblivion_ugraph in + "(let " ^ ppname b ^ " = " ^ pp ~in_type:false s context ^ " in " ^ + pp ~in_type t ((Some (b,Cic.Def (s,Some ty)))::context) ^ ")" + | C.Appl (he::tl) when in_type -> + let hes = pp ~in_type he context in + let stl = String.concat "," (clean_args_for_ty context tl) in + (if stl = "" then "" else "(" ^ stl ^ ") ") ^ hes + | C.Appl (C.MutInd _ as he::tl) -> + let hes = pp ~in_type he context 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 ~in_type 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 0 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_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 - ppid name ^ pp_exp_named_subst exp_named_subst context + 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 | _ -> raise CicExportationInternalError with Sys.Break as exn -> raise exn @@ -181,9 +225,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_uri ~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,89 +239,131 @@ 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 - ) + if in_type then + "unit (* TOO POLYMORPHIC TYPE *)" + else ( + let needs_obj_magic = + (* BUG HERE: we should consider also the right parameters *) + match CicReduction.whd context ty with + Cic.Lambda (_,_,t) -> not (DoubleTypeInference.does_not_occur 1 t) + | _ -> false (* it can be a Rel, e.g. in *_rec *) 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 ~in_type:false 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 ~in_type:false 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 name = + match name with + Cic.Anonymous -> Cic.Anonymous + | Cic.Name n -> Cic.Name (ppid n) in + 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 ~in_type:false t context + | t -> + ["{" ^ string_of_int argsno ^ " args missing}"], + pp ~in_type:false t context + in + let pattern,body = + if argsno = 0 then x,pp ~in_type:false 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 ^ " -> " ^ + if needs_obj_magic then + "Obj.magic (" ^ body ^ ")" + else + body + ) connames_and_argsno_and_patterns)) ^ + ")\n"))) | C.Fix (no, funs) -> - let names = - List.rev - (List.map - (function (name,_,ty,_) -> - Some (C.Name name,Cic.Decl ty)) funs) + let names,_ = + List.fold_left + (fun (types,len) (n,_,ty,_) -> + (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types, + len+1) + ) ([],0) funs in - "\nlet rec " ^ + "let rec " ^ List.fold_right (fun (name,ind,ty,bo) i -> name ^ " = \n" ^ - pp bo (names@context) ^ i) + pp ~in_type:false bo (names@context) ^ i) funs "" ^ " 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 - (List.map - (function (name,ty,_) -> - Some (C.Name name,Cic.Decl ty)) funs) + let names,_ = + List.fold_left + (fun (types,len) (n,ty,_) -> + (Some (C.Name n,(C.Decl (CicSubstitution.lift len ty)))::types, + len+1) + ) ([],0) funs in "\nCoFix " ^ " {" ^ List.fold_right (fun (name,ty,bo) i -> "\n" ^ name ^ - " : " ^ pp ty context ^ " := \n" ^ - pp bo (names@context) ^ i) + " : " ^ pp ~in_type:true ty context ^ " := \n" ^ + pp ~in_type:false bo (names@context) ^ i) funs "" ^ "}\n" and pp_exp_named_subst exp_named_subst context = @@ -282,84 +371,178 @@ and pp_exp_named_subst exp_named_subst context = "\\subst[" ^ String.concat " ; " ( List.map - (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp t context) + (function (uri,t) -> UriManager.name_of_uri uri ^ " \\Assign " ^ pp ~in_type:false 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 ~in_type:false 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 ~in_type:true t context) | `Proof -> None - | `Term -> Some pp t context) + | `Term -> None) in - pp + pp ~in_type +;; + +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 + `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 ~in_type:true current_module_uri s context::args + | `Sort _ when nparams <= 0 -> + let n = Cic.Name "unit (* EXISTENTIAL TYPE *)" in + args (nparams - 1) ((Some (n,Cic.Decl s))::context) t + | `Sort _ -> + let n = + match n with + Cic.Anonymous -> Cic.Anonymous + | Cic.Name name -> Cic.Name ("'" ^ name) 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) + | _ -> [],[] + in + args +;; + +exception DoNotExtract;; + +let pp_abstracted_ty current_module_uri = + let rec args context = + function + Cic.Lambda (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 + `Statement + | `Type + | `Sort Cic.Prop -> + args ((Some (n,Cic.Decl s))::context) t + | `Sort _ -> + let n = + match n with + Cic.Anonymous -> Cic.Anonymous + | Cic.Name name -> Cic.Name ("'" ^ name) in + let abstr,res = + args ((Some (n,Cic.Decl s))::context) t + in + (match n with + Cic.Anonymous -> abstr + | Cic.Name name -> name::abstr), + res) + | ty -> + match analyze_type context ty with + ` Sort _ + | `Statement -> raise DoNotExtract + | `Type -> + (* BUG HERE: this can be a real System F type *) + let head = pp ~in_type:true current_module_uri ty context in + [],head + 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_uri nparams (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) -> (* 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 + 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_uri obj = let module C = Cic in let module U = UriManager in + let pp ~in_type = pp ~in_type current_module_uri 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 ~in_type:false t1 [] ^ "\n" + | `Sort _ -> + match analyze_type [] t1 with + `Sort Cic.Prop -> "" + | _ -> + (try + let abstr,res = pp_abstracted_ty current_module_uri [] t1 in + let abstr = + let s = String.concat "," abstr in + if s = "" then "" else "(" ^ s ^ ") " + in + "type " ^ abstr ^ ppid name ^ " = " ^ res ^ "\n" + with + DoNotExtract -> "")) | 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 ^ "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^ ")" ^ ":\n" ^ - pp ty [] ^ "\n" ^ - (match bo with None -> "" | Some bo -> ":= " ^ pp bo []) + pp ~in_type:true ty [] ^ "\n" ^ + (match bo with None -> "" | Some bo -> ":= " ^ pp ~in_type:false bo []) | C.CurrentProof (name, conjectures, value, ty, params, _) -> "Current Proof of " ^ name ^ "(" ^ String.concat ";" (List.map UriManager.string_of_uri params) ^ @@ -374,12 +557,13 @@ let ppobj obj = Some (n,C.Decl at) -> (separate i) ^ ppname n ^ ":" ^ - pp ~metasenv:conjectures at name_context ^ " ", + pp ~in_type:true ~metasenv:conjectures + at name_context ^ " ", context_entry::name_context | Some (n,C.Def (at,None)) -> (separate i) ^ - ppname n ^ ":= " ^ pp ~metasenv:conjectures - at name_context ^ " ", + ppname n ^ ":= " ^ pp ~in_type:false + ~metasenv:conjectures at name_context ^ " ", context_entry::name_context | None -> (separate i) ^ "_ :? _ ", context_entry::name_context @@ -387,15 +571,16 @@ let ppobj obj = ) context ("",[]) in conjectures' ^ " |- " ^ "?" ^ (string_of_int n) ^ ": " ^ - pp ~metasenv:conjectures t name_context ^ "\n" ^ i + pp ~in_type:true ~metasenv:conjectures t name_context ^ "\n" ^ i ) conjectures "" ^ - "\n" ^ pp ~metasenv:conjectures value [] ^ " : " ^ - pp ~metasenv:conjectures ty [] + "\n" ^ pp ~in_type:false ~metasenv:conjectures value [] ^ " : " ^ + pp ~in_type:true ~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_uri nparams x ^ i) l "" ;; -let ppobj obj = - let res = ppobj 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" ;;