]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/interface/cicPp.ml
Initial revision
[helm.git] / helm / interface / cicPp.ml
diff --git a/helm/interface/cicPp.ml b/helm/interface/cicPp.ml
new file mode 100644 (file)
index 0000000..9329786
--- /dev/null
@@ -0,0 +1,183 @@
+(******************************************************************************)
+(*                                                                            *)
+(*                               PROJECT HELM                                 *)
+(*                                                                            *)
+(*                Claudio Sacerdoti Coen <sacerdot@cs.unibo.it>               *)
+(*                                 24/01/2000                                 *)
+(*                                                                            *)
+(* This module implements a very simple Coq-like pretty printer that, given   *)
+(* an object of cic (internal representation) returns a string describing the *)
+(* object in a syntax similar to that of coq                                  *)
+(*                                                                            *)
+(******************************************************************************)
+
+exception CicPpInternalError;;
+
+(* Utility functions *)
+
+let string_of_name =
+ function
+    Cic.Name s     -> s
+  | Cic.Anonimous  -> "_"
+;;
+
+(* get_nth l n   returns the nth element of the list l if it exists or raise *)
+(* a CicPpInternalError if l has less than n elements or n < 1               *)
+let rec get_nth l n =
+ match (n,l) with
+    (1, he::_) -> he
+  | (n, he::tail) when n > 1 -> get_nth tail (n-1)
+  | (_,_) -> raise CicPpInternalError
+;;
+
+(* 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 rec pp t l =
+ let module C = Cic in
+   match t with
+      C.Rel n ->
+       (match get_nth l n with
+           C.Name s -> s
+         | _        -> raise CicPpInternalError
+       )
+    | C.Var uri -> UriManager.name_of_uri uri
+    | C.Meta n -> "?" ^ (string_of_int n)
+    | C.Sort s ->
+       (match s with
+           C.Prop -> "Prop"
+         | C.Set  -> "Set"
+         | C.Type -> "Type"
+       )
+    | C.Implicit -> "?"
+    | C.Prod (b,s,t) ->
+       (match b with
+          C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t (b::l)
+        | C.Anonimous -> "(" ^ pp s l ^ "->" ^ pp t (b::l) ^ ")"
+       )
+    | C.Cast (v,t) -> pp v l
+    | C.Lambda (b,s,t) ->
+       "[" ^ string_of_name b ^ ":" ^ pp s l ^ "]" ^ pp t (b::l)
+    | C.Appl li ->
+       "(" ^
+       (List.fold_right
+        (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
+        li ""
+       ) ^ ")"
+    | C.Const (uri,_) -> UriManager.name_of_uri uri
+    | C.Abst uri -> UriManager.name_of_uri uri
+    | C.MutInd (uri,_,n) ->
+       (match CicCache.get_obj uri with
+           C.InductiveDefinition (dl,_,_) ->
+            let (name,_,_,_) = get_nth dl (n+1) in
+             name
+         | _ -> raise CicPpInternalError
+       )
+    | C.MutConstruct (uri,_,n1,n2) ->
+       (match CicCache.get_obj uri with
+           C.InductiveDefinition (dl,_,_) ->
+            let (_,_,_,cons) = get_nth dl (n1+1) in
+             let (id,_,_) = get_nth cons n2 in
+              id
+         | _ -> raise CicPpInternalError
+       )
+    | C.MutCase (uri,_,n1,ty,te,patterns) ->
+       let connames =
+        (match CicCache.get_obj uri with
+            C.InductiveDefinition (dl,_,_) ->
+             let (_,_,_,cons) = get_nth dl (n1+1) in
+              List.map (fun (id,_,_) -> id) cons
+          | _ -> raise CicPpInternalError
+        )
+       in
+        "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
+          List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
+           (List.combine connames patterns) "" ^
+          "\nend"
+    | C.Fix (no, funs) ->
+       let snames = List.map (fun (name,_,_,_) -> name) funs in
+        let names = List.rev (List.map (function name -> C.Name name) snames) in
+         "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
+         List.fold_right
+          (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
+            " : " ^ pp ty l ^ " := \n" ^
+            pp bo (names@l) ^ i)
+          funs "" ^
+         "}\n"
+    | C.CoFix (no,funs) ->
+       let snames = List.map (fun (name,_,_) -> name) funs in
+        let names = List.rev (List.map (function name -> C.Name name) snames) in
+         "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
+         List.fold_right
+          (fun (name,ty,bo) i -> "\n" ^ name ^ 
+            " : " ^ pp ty l ^ " := \n" ^
+            pp bo (names@l) ^ i)
+          funs "" ^
+         "}\n"
+;;
+
+(* ppinductiveType (typename, inductive, arity, cons) names                 *)
+(* pretty-prints a single inductive definition (typename, inductive, arity, *)
+(*  cons) where the cic terms in the inductive definition need to be        *)
+(*  evaluated in the environment names that is the list of typenames of the *)
+(*  mutual inductive definitions defined in the block of mutual inductive   *)
+(*  definitions to which this one belongs to                                *)
+let ppinductiveType (typename, inductive, arity, cons) names =
+  (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
+  (*CSC: bug found: was pp arity names ^ " =\n   " ^*)
+  pp arity [] ^ " =\n   " ^
+  List.fold_right
+   (fun (id,ty,_) i -> id ^ " : " ^ pp ty names ^ 
+    (if i = "" then "\n" else "\n | ") ^ i)
+   cons ""
+;;
+
+(* 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 module C = Cic in
+ let module U = UriManager in
+  match obj with
+    C.Definition (id, t1, t2, params) ->
+      "Definition of " ^ id ^
+      "(" ^
+      List.fold_right
+       (fun (_,x) i ->
+         List.fold_right
+          (fun x i ->
+            U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
+          ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
+       ) params "" ^ ")" ^
+      ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
+   | C.Axiom (id, ty, params) ->
+      "Axiom " ^ id ^ "(" ^
+      List.fold_right
+       (fun (_,x) i ->
+         List.fold_right
+          (fun x i ->
+            U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
+          ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
+       ) params "" ^
+      "):\n" ^ pp ty []
+   | C.Variable (name, ty) ->
+      "Variable " ^ name ^ ":\n" ^ pp ty []
+   | C.CurrentProof (name, conjectures, value, ty) ->
+      "Current Proof:\n" ^
+      List.fold_right
+       (fun (n, t) i -> "?" ^ (string_of_int n) ^ ": " ^ pp t [] ^ "\n" ^ i)
+       conjectures "" ^
+      "\n" ^ pp value [] ^ " : " ^ pp ty [] 
+   | C.InductiveDefinition (l, params, nparams) ->
+      "Parameters = " ^
+      List.fold_right
+       (fun (_,x) i ->
+         List.fold_right
+          (fun x i ->
+            U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
+          ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
+       ) params "" ^ "\n" ^
+      "NParams = " ^ string_of_int nparams ^ "\n" ^
+      let names = List.rev (List.map (fun (n,_,_,_) -> C.Name n) l) in
+       List.fold_right (fun x i -> ppinductiveType x names ^ i) l ""
+;;