1 (******************************************************************************)
5 (* Claudio Sacerdoti Coen <sacerdot@cs.unibo.it> *)
8 (* This module implements a very simple Coq-like pretty printer that, given *)
9 (* an object of cic (internal representation) returns a string describing the *)
10 (* object in a syntax similar to that of coq *)
12 (******************************************************************************)
14 exception CicPpInternalError;;
16 (* Utility functions *)
21 | Cic.Anonimous -> "_"
24 (* get_nth l n returns the nth element of the list l if it exists or raise *)
25 (* a CicPpInternalError if l has less than n elements or n < 1 *)
29 | (n, he::tail) when n > 1 -> get_nth tail (n-1)
30 | (_,_) -> raise CicPpInternalError
34 (* pretty-prints a term t of cic in an environment l where l is a list of *)
35 (* identifier names used to resolve DeBrujin indexes. The head of l is the *)
36 (* name associated to the greatest DeBrujin index in t *)
41 (match get_nth l n with
43 | _ -> raise CicPpInternalError
45 | C.Var uri -> UriManager.name_of_uri uri
46 | C.Meta n -> "?" ^ (string_of_int n)
56 C.Name n -> "(" ^ n ^ ":" ^ pp s l ^ ")" ^ pp t (b::l)
57 | C.Anonimous -> "(" ^ pp s l ^ "->" ^ pp t (b::l) ^ ")"
59 | C.Cast (v,t) -> pp v l
61 "[" ^ string_of_name b ^ ":" ^ pp s l ^ "]" ^ pp t (b::l)
65 (fun x i -> pp x l ^ (match i with "" -> "" | _ -> " ") ^ i)
68 | C.Const (uri,_) -> UriManager.name_of_uri uri
69 | C.Abst uri -> UriManager.name_of_uri uri
70 | C.MutInd (uri,_,n) ->
71 (match CicCache.get_obj uri with
72 C.InductiveDefinition (dl,_,_) ->
73 let (name,_,_,_) = get_nth dl (n+1) in
75 | _ -> raise CicPpInternalError
77 | C.MutConstruct (uri,_,n1,n2) ->
78 (match CicCache.get_obj uri with
79 C.InductiveDefinition (dl,_,_) ->
80 let (_,_,_,cons) = get_nth dl (n1+1) in
81 let (id,_,_) = get_nth cons n2 in
83 | _ -> raise CicPpInternalError
85 | C.MutCase (uri,_,n1,ty,te,patterns) ->
87 (match CicCache.get_obj uri with
88 C.InductiveDefinition (dl,_,_) ->
89 let (_,_,_,cons) = get_nth dl (n1+1) in
90 List.map (fun (id,_,_) -> id) cons
91 | _ -> raise CicPpInternalError
94 "\n<" ^ pp ty l ^ ">Cases " ^ pp te l ^ " of " ^
95 List.fold_right (fun (x,y) i -> "\n " ^ x ^ " => " ^ pp y l ^ i)
96 (List.combine connames patterns) "" ^
99 let snames = List.map (fun (name,_,_,_) -> name) funs in
100 let names = List.rev (List.map (function name -> C.Name name) snames) in
101 "\nFix " ^ get_nth snames (no + 1) ^ " {" ^
103 (fun (name,ind,ty,bo) i -> "\n" ^ name ^ " / " ^ string_of_int ind ^
104 " : " ^ pp ty l ^ " := \n" ^
108 | C.CoFix (no,funs) ->
109 let snames = List.map (fun (name,_,_) -> name) funs in
110 let names = List.rev (List.map (function name -> C.Name name) snames) in
111 "\nCoFix " ^ get_nth snames (no + 1) ^ " {" ^
113 (fun (name,ty,bo) i -> "\n" ^ name ^
114 " : " ^ pp ty l ^ " := \n" ^
120 (* ppinductiveType (typename, inductive, arity, cons) names *)
121 (* pretty-prints a single inductive definition (typename, inductive, arity, *)
122 (* cons) where the cic terms in the inductive definition need to be *)
123 (* evaluated in the environment names that is the list of typenames of the *)
124 (* mutual inductive definitions defined in the block of mutual inductive *)
125 (* definitions to which this one belongs to *)
126 let ppinductiveType (typename, inductive, arity, cons) names =
127 (if inductive then "\nInductive " else "\nCoInductive ") ^ typename ^ ": " ^
128 (*CSC: bug found: was pp arity names ^ " =\n " ^*)
129 pp arity [] ^ " =\n " ^
131 (fun (id,ty,_) i -> id ^ " : " ^ pp ty names ^
132 (if i = "" then "\n" else "\n | ") ^ i)
136 (* ppobj obj returns a string with describing the cic object obj in a syntax *)
137 (* similar to the one used by Coq *)
139 let module C = Cic in
140 let module U = UriManager in
142 C.Definition (id, t1, t2, params) ->
143 "Definition of " ^ id ^
149 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
150 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
152 ":\n" ^ pp t1 [] ^ " : " ^ pp t2 []
153 | C.Axiom (id, ty, params) ->
154 "Axiom " ^ id ^ "(" ^
159 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
160 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
163 | C.Variable (name, ty) ->
164 "Variable " ^ name ^ ":\n" ^ pp ty []
165 | C.CurrentProof (name, conjectures, value, ty) ->
168 (fun (n, t) i -> "?" ^ (string_of_int n) ^ ": " ^ pp t [] ^ "\n" ^ i)
170 "\n" ^ pp value [] ^ " : " ^ pp ty []
171 | C.InductiveDefinition (l, params, nparams) ->
177 U.string_of_uri x ^ match i with "" -> "" | i' -> " " ^ i'
178 ) x "" ^ match i with "" -> "" | i' -> " " ^ i'
180 "NParams = " ^ string_of_int nparams ^ "\n" ^
181 let names = List.rev (List.map (fun (n,_,_,_) -> C.Name n) l) in
182 List.fold_right (fun x i -> ppinductiveType x names ^ i) l ""