| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Util |
| 12 |
open Pp |
| 13 |
open Names |
| 14 |
open Tac2expr |
| 15 |
open Tac2env |
| 16 |
open Pputils |
| 17 |
|
| 18 |
let pr_tacref avoid kn = |
| 19 |
try Libnames.pr_qualid (Tac2env.shortest_qualid_of_ltac avoid (TacConstant kn)) |
| 20 |
with Not_found when !Flags.in_debugger || KerName.Map.mem kn (Tac2env.globals()) -> |
| 21 |
str (ModPath.to_string (KerName.modpath kn)) |
| 22 |
++ str"." ++ Id.print (KerName.label kn) |
| 23 |
++ if !Flags.in_debugger then mt() else str " (* local *)" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
let change_kn_label kn id = |
| 28 |
let mp = KerName.modpath kn in |
| 29 |
KerName.make mp id |
| 30 |
|
| 31 |
let paren p = hov 2 (str "(" ++ p ++ str ")") |
| 32 |
|
| 33 |
let t_list = |
| 34 |
KerName.make Tac2env.rocq_prefix (Id.of_string "list") |
| 35 |
|
| 36 |
let c_nil = change_kn_label t_list (Id.of_string_soft "[]") |
| 37 |
let c_cons = change_kn_label t_list (Id.of_string_soft "::") |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
type typ_level = |
| 42 |
| T5_l |
| 43 |
| T5_r |
| 44 |
| T2 |
| 45 |
| T1 |
| 46 |
| T0 |
| 47 |
|
| 48 |
let t_unit = |
| 49 |
KerName.make Tac2env.rocq_prefix (Id.of_string "unit") |
| 50 |
|
| 51 |
let pr_typref kn = |
| 52 |
Libnames.pr_qualid (Tac2env.shortest_qualid_of_type kn) |
| 53 |
|
| 54 |
let pr_glbtype_gen pr lvl c = |
| 55 |
let rec pr_glbtype lvl = function |
| 56 |
| GTypVar n -> str "'" ++ str (pr n) |
| 57 |
| GTypRef (Other kn, []) -> pr_typref kn |
| 58 |
| GTypRef (Other kn, [t]) -> |
| 59 |
let paren = match lvl with |
| 60 |
| T5_r | T5_l | T2 | T1 -> fun x -> x |
| 61 |
| T0 -> paren |
| 62 |
in |
| 63 |
paren (pr_glbtype T1 t ++ spc () ++ pr_typref kn) |
| 64 |
| GTypRef (Other kn, tl) -> |
| 65 |
let paren = match lvl with |
| 66 |
| T5_r | T5_l | T2 | T1 -> fun x -> x |
| 67 |
| T0 -> paren |
| 68 |
in |
| 69 |
paren (str "(" ++ prlist_with_sep pr_comma (pr_glbtype lvl) tl ++ str ")" ++ spc () ++ pr_typref kn) |
| 70 |
| GTypArrow (t1, t2) -> |
| 71 |
let paren = match lvl with |
| 72 |
| T5_r -> fun x -> x |
| 73 |
| T5_l | T2 | T1 | T0 -> paren |
| 74 |
in |
| 75 |
paren (pr_glbtype T5_l t1 ++ spc () ++ str "->" ++ spc () ++ pr_glbtype T5_r t2) |
| 76 |
| GTypRef (Tuple 0, []) -> |
| 77 |
Libnames.pr_qualid (Tac2env.shortest_qualid_of_type t_unit) |
| 78 |
| GTypRef (Tuple _, tl) -> |
| 79 |
let paren = match lvl with |
| 80 |
| T5_r | T5_l -> fun x -> x |
| 81 |
| T2 | T1 | T0 -> paren |
| 82 |
in |
| 83 |
paren (prlist_with_sep (fun () -> str " * ") (pr_glbtype T2) tl) |
| 84 |
in |
| 85 |
hov 0 (pr_glbtype lvl c) |
| 86 |
|
| 87 |
let pr_glbtype pr c = pr_glbtype_gen pr T5_r c |
| 88 |
|
| 89 |
let int_name () = |
| 90 |
let vars = ref Int.Map.empty in |
| 91 |
fun n -> |
| 92 |
if Int.Map.mem n !vars then Int.Map.find n !vars |
| 93 |
else |
| 94 |
let num = Int.Map.cardinal !vars in |
| 95 |
let base = num mod 26 in |
| 96 |
let rem = num / 26 in |
| 97 |
let name = String.make 1 (Char.chr (97 + base)) in |
| 98 |
let suff = if Int.equal rem 0 then "" else string_of_int rem in |
| 99 |
let name = name ^ suff in |
| 100 |
let () = vars := Int.Map.add n name !vars in |
| 101 |
name |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
let pr_constructor kn = |
| 106 |
Libnames.pr_qualid (Tac2env.shortest_qualid_of_constructor kn) |
| 107 |
|
| 108 |
let pr_projection kn = |
| 109 |
Libnames.pr_qualid (Tac2env.shortest_qualid_of_projection kn) |
| 110 |
|
| 111 |
type exp_level = Tac2expr.exp_level = |
| 112 |
| E5 |
| 113 |
| E4 |
| 114 |
| E3 |
| 115 |
| E2 |
| 116 |
| E1 |
| 117 |
| E0 |
| 118 |
|
| 119 |
let pr_atom = function |
| 120 |
| AtmInt n -> Pp.int n |
| 121 |
| AtmStr s -> qstring s |
| 122 |
|
| 123 |
let pr_name = function |
| 124 |
| Name id -> Id.print id |
| 125 |
| Anonymous -> str "_" |
| 126 |
|
| 127 |
let find_constructor n empty def = |
| 128 |
let rec find n = function |
| 129 |
| [] -> assert false |
| 130 |
| (_, id, []) as ans :: rem -> |
| 131 |
if empty then |
| 132 |
if Int.equal n 0 then ans |
| 133 |
else find (pred n) rem |
| 134 |
else find n rem |
| 135 |
| (_, id, _ :: _) as ans :: rem -> |
| 136 |
if not empty then |
| 137 |
if Int.equal n 0 then ans |
| 138 |
else find (pred n) rem |
| 139 |
else find n rem |
| 140 |
in |
| 141 |
find n def |
| 142 |
|
| 143 |
let pr_internal_constructor tpe n is_const = |
| 144 |
let data = match Tac2env.interp_type tpe with |
| 145 |
| (_, GTydAlg data) -> data |
| 146 |
| _ -> assert false |
| 147 |
in |
| 148 |
let (_, id, _) = find_constructor n is_const data.galg_constructors in |
| 149 |
let kn = change_kn_label tpe id in |
| 150 |
pr_constructor kn |
| 151 |
|
| 152 |
let order_branches cbr nbr def = |
| 153 |
let rec order cidx nidx def = match def with |
| 154 |
| [] -> [] |
| 155 |
| (_, id, []) :: rem -> |
| 156 |
let ans = order (succ cidx) nidx rem in |
| 157 |
(id, [], cbr.(cidx)) :: ans |
| 158 |
| (_, id, _ :: _) :: rem -> |
| 159 |
let ans = order cidx (succ nidx) rem in |
| 160 |
let (vars, e) = nbr.(nidx) in |
| 161 |
(id, Array.to_list vars, e) :: ans |
| 162 |
in |
| 163 |
order 0 0 def |
| 164 |
|
| 165 |
type ('a,'b) factorization = |
| 166 |
| FullList of 'a list |
| 167 |
| ListPrefix of 'a list * 'a |
| 168 |
| Other of 'b |
| 169 |
|
| 170 |
let pr_factorized_constructor pr_rec lvl tpe = function |
| 171 |
| FullList l -> |
| 172 |
let pr e = pr_rec E4 e in |
| 173 |
hov 2 (str "[" ++ prlist_with_sep pr_semicolon pr (List.rev l) ++ str "]") |
| 174 |
| ListPrefix (l,e) -> |
| 175 |
let paren = match lvl with |
| 176 |
| E0 | E1 | E2 -> paren |
| 177 |
| E3 | E4 | E5 -> fun x -> x |
| 178 |
in |
| 179 |
let pr e = pr_rec E1 e in |
| 180 |
let pr_cons () = spc () ++ str "::" ++ spc () in |
| 181 |
paren (hov 2 (prlist_with_sep pr_cons pr (List.rev (e :: l)))) |
| 182 |
| Other (n,cl) -> |
| 183 |
let _, data = Tac2env.interp_type tpe in |
| 184 |
match data with |
| 185 |
| GTydDef None -> str "<abstr>" |
| 186 |
| GTydAlg def -> |
| 187 |
let paren = match lvl with |
| 188 |
| E0 -> |
| 189 |
if List.is_empty cl then fun x -> x else paren |
| 190 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 191 |
in |
| 192 |
let cstr = pr_internal_constructor tpe n (List.is_empty cl) in |
| 193 |
let cl = match cl with |
| 194 |
| [] -> mt () |
| 195 |
| _ -> spc () ++ pr_sequence (pr_rec E0) cl |
| 196 |
in |
| 197 |
paren (hov 2 (cstr ++ cl)) |
| 198 |
| GTydRec def -> |
| 199 |
let args = List.combine def cl in |
| 200 |
let pr_arg ((id, _, _), arg) = |
| 201 |
let kn = change_kn_label tpe id in |
| 202 |
pr_projection kn ++ spc () ++ str ":=" ++ spc () ++ pr_rec E1 arg |
| 203 |
in |
| 204 |
let args = prlist_with_sep pr_semicolon pr_arg args in |
| 205 |
hv 0 (str "{" ++ spc () ++ args ++ spc () ++ str "}") |
| 206 |
| (GTydDef _ | GTydOpn) -> assert false |
| 207 |
|
| 208 |
let pr_partial_pat_gen = |
| 209 |
let open PartialPat in |
| 210 |
let rec pr_pat lvl pat = match pat.CAst.v with |
| 211 |
| Var x -> pr_name x |
| 212 |
| Atom a -> pr_atom a |
| 213 |
| As (p, x) -> |
| 214 |
let paren = match lvl with |
| 215 |
| E0 -> paren |
| 216 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 217 |
in |
| 218 |
paren (hv 0 (pr_pat E1 p ++ spc() ++ str "as" ++ spc () ++ Id.print x)) |
| 219 |
| Ref ({ctyp=None; cnargs=0}, _) -> str "()" |
| 220 |
| Ref ({ctyp=None}, args) -> |
| 221 |
let paren = match lvl with |
| 222 |
| E0 | E1 -> paren |
| 223 |
| E2 | E3 | E4 | E5 -> fun x -> x |
| 224 |
in |
| 225 |
paren (prlist_with_sep pr_comma (pr_pat E1) args) |
| 226 |
| Ref ({cindx=Open kn}, args) -> |
| 227 |
let paren = match lvl with |
| 228 |
| E0 -> paren |
| 229 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 230 |
in |
| 231 |
let c = pr_constructor kn in |
| 232 |
paren (hov 0 (c ++ spc () ++ (pr_sequence (pr_pat E0) args))) |
| 233 |
| Ref ({ctyp=Some ctyp; cindx=Closed i}, args) -> |
| 234 |
|
| 235 |
let factorized = |
| 236 |
if KerName.equal ctyp t_list then |
| 237 |
let rec factorize accu pat = match pat.CAst.v with |
| 238 |
| Ref (_, []) -> accu, None |
| 239 |
| Ref (_, [e; l]) -> factorize (e :: accu) l |
| 240 |
| _ -> accu, Some pat |
| 241 |
in |
| 242 |
let l, e = factorize [] pat in |
| 243 |
match e with |
| 244 |
| None -> FullList l |
| 245 |
| Some e -> ListPrefix (l,e) |
| 246 |
else Other (i,args) |
| 247 |
in |
| 248 |
pr_factorized_constructor pr_pat lvl ctyp factorized |
| 249 |
| Extension {example=None} -> str "*extension*" |
| 250 |
| Extension {example=Some a} -> pr_atom a |
| 251 |
| Or pats -> |
| 252 |
let paren = match lvl with |
| 253 |
| E0 -> paren |
| 254 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 255 |
in |
| 256 |
paren (hv 0 (prlist_with_sep (fun () -> spc() ++ str "|" ++ spc()) (pr_pat E1) pats)) |
| 257 |
in |
| 258 |
fun lvl pat -> hov 0 (pr_pat lvl pat) |
| 259 |
|
| 260 |
let pr_partial_pat pat = pr_partial_pat_gen E5 pat |
| 261 |
|
| 262 |
|
| 263 |
let rec partial_pat_of_glb_pat pat = |
| 264 |
let open PartialPat in |
| 265 |
let pat = match pat with |
| 266 |
| GPatVar x -> Var x |
| 267 |
| GPatAtm x -> Atom x |
| 268 |
| GPatRef (ctor,pats) -> Ref (ctor, List.map partial_pat_of_glb_pat pats) |
| 269 |
| GPatOr pats -> Or (List.map partial_pat_of_glb_pat pats) |
| 270 |
| GPatAs (p,x) -> As (partial_pat_of_glb_pat p, x) |
| 271 |
in |
| 272 |
CAst.make pat |
| 273 |
|
| 274 |
let pr_glb_pat pat = pr_partial_pat (partial_pat_of_glb_pat pat) |
| 275 |
|
| 276 |
let rec avoid_glb_pat avoid = function |
| 277 |
| GPatVar x -> Termops.add_vname avoid x |
| 278 |
| GPatAtm _ -> avoid |
| 279 |
| GPatRef (_, pats) -> List.fold_left avoid_glb_pat avoid pats |
| 280 |
| GPatOr [] -> assert false |
| 281 |
| GPatOr (p::_) -> avoid_glb_pat avoid p |
| 282 |
| GPatAs (p,x) -> avoid_glb_pat (Id.Set.add x avoid) p |
| 283 |
|
| 284 |
let pr_glbexpr_gen lvl ~avoid c = |
| 285 |
let rec pr_glbexpr lvl avoid = function |
| 286 |
| GTacAtm atm -> pr_atom atm |
| 287 |
| GTacVar id -> Id.print id |
| 288 |
| GTacRef gr -> pr_tacref avoid gr |
| 289 |
| GTacFun (nas, c) -> |
| 290 |
let avoid = List.fold_left Termops.add_vname avoid nas in |
| 291 |
let nas = pr_sequence pr_name nas in |
| 292 |
let paren = match lvl with |
| 293 |
| E0 | E1 | E2 | E3 | E4 -> paren |
| 294 |
| E5 -> fun x -> x |
| 295 |
in |
| 296 |
paren (hov 0 (hov 2 (str "fun" ++ spc () ++ nas) ++ spc () ++ str "=>" ++ spc () ++ |
| 297 |
pr_glbexpr E5 avoid c)) |
| 298 |
| GTacApp (c, cl) -> |
| 299 |
let paren = match lvl with |
| 300 |
| E0 -> paren |
| 301 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 302 |
in |
| 303 |
paren (hov 2 (pr_glbexpr E1 avoid c ++ spc () ++ (pr_sequence (pr_glbexpr E0 avoid) cl))) |
| 304 |
| GTacLet (isrec, bnd, e) -> |
| 305 |
let paren = match lvl with |
| 306 |
| E0 | E1 | E2 | E3 | E4 -> paren |
| 307 |
| E5 -> fun x -> x |
| 308 |
in |
| 309 |
let pprec = if isrec then str "rec " else mt () in |
| 310 |
let avoidbnd = List.fold_left (fun avoid (na,_) -> Termops.add_vname avoid na) avoid bnd in |
| 311 |
let pr_bnd (na, e) = |
| 312 |
let avoid = if isrec then avoidbnd else avoid in |
| 313 |
|
| 314 |
|
| 315 |
hov (-2 - if isrec then 4 else 0) |
| 316 |
(pr_name na ++ str " :=" ++ spc () ++ pr_glbexpr E5 avoid e) |
| 317 |
in |
| 318 |
let bnd = prlist_with_sep (fun () -> spc() ++ str "with ") pr_bnd bnd in |
| 319 |
paren (v 0 (hov 0 (v 0 (str "let " ++ pprec ++ bnd) ++ spc() ++ str "in") ++ spc ()) ++ pr_glbexpr E5 avoidbnd e) |
| 320 |
| GTacCst (Tuple 0, _, _) -> str "()" |
| 321 |
| GTacCst (Tuple _, _, cl) -> |
| 322 |
paren (prlist_with_sep (fun () -> str "," ++ spc ()) (pr_glbexpr E1 avoid) cl) |
| 323 |
| GTacCst (Other tpe, n, cl) -> |
| 324 |
pr_applied_constructor lvl avoid tpe n cl |
| 325 |
| GTacCse (e, info, cst_br, ncst_br) -> |
| 326 |
let e = pr_glbexpr E5 avoid e in |
| 327 |
let br = match info with |
| 328 |
| Other kn -> |
| 329 |
begin match Tac2env.interp_type kn with |
| 330 |
| _, GTydDef None -> str "<abstr>" |
| 331 |
| _, GTydDef _ | _, GTydRec _ | _, GTydOpn -> assert false |
| 332 |
| _, GTydAlg { galg_constructors = def } -> |
| 333 |
let br = order_branches cst_br ncst_br def in |
| 334 |
let pr_branch (cstr, vars, p) = |
| 335 |
let cstr = change_kn_label kn cstr in |
| 336 |
let cstr = pr_constructor cstr in |
| 337 |
let avoid = List.fold_left Termops.add_vname avoid vars in |
| 338 |
let vars = match vars with |
| 339 |
| [] -> mt () |
| 340 |
| _ -> spc () ++ pr_sequence pr_name vars |
| 341 |
in |
| 342 |
hov 4 (str "|" ++ spc () ++ hov 0 (cstr ++ vars ++ spc () ++ str "=>") ++ spc () ++ |
| 343 |
hov 2 (pr_glbexpr E5 avoid p)) |
| 344 |
in |
| 345 |
prlist_with_sep spc pr_branch br |
| 346 |
end |
| 347 |
| Tuple n -> |
| 348 |
let (vars, p) = if Int.equal n 0 then ([||], cst_br.(0)) else ncst_br.(0) in |
| 349 |
let avoid = Array.fold_left Termops.add_vname avoid vars in |
| 350 |
let p = pr_glbexpr E5 avoid p in |
| 351 |
let vars = prvect_with_sep (fun () -> str "," ++ spc ()) pr_name vars in |
| 352 |
hov 4 (str "|" ++ spc () ++ hov 0 (paren vars ++ spc () ++ str "=>") ++ spc () ++ p) |
| 353 |
in |
| 354 |
v 0 (hv 0 (str "match" ++ spc () ++ e ++ spc () ++ str "with") ++ spc () ++ br ++ spc () ++ str "end") |
| 355 |
| GTacWth wth -> |
| 356 |
let e = pr_glbexpr E5 avoid wth.opn_match in |
| 357 |
let pr_pattern c self vars avoid p = |
| 358 |
let avoid = Termops.add_vname avoid self in |
| 359 |
let self = match self with |
| 360 |
| Anonymous -> mt () |
| 361 |
| Name id -> spc () ++ str "as" ++ spc () ++ Id.print id |
| 362 |
in |
| 363 |
hov 4 (str "|" ++ spc () ++ hov 0 (c ++ vars ++ self ++ spc () ++ str "=>") ++ spc () ++ |
| 364 |
hov 2 (pr_glbexpr E5 avoid p)) ++ spc () |
| 365 |
in |
| 366 |
let pr_branch (cstr, (self, vars, p)) = |
| 367 |
let cstr = pr_constructor cstr in |
| 368 |
let avoid = Array.fold_left Termops.add_vname avoid vars in |
| 369 |
let vars = match Array.to_list vars with |
| 370 |
| [] -> mt () |
| 371 |
| vars -> spc () ++ pr_sequence pr_name vars |
| 372 |
in |
| 373 |
pr_pattern cstr self vars avoid p |
| 374 |
in |
| 375 |
let br = prlist pr_branch (KerName.Map.bindings wth.opn_branch) in |
| 376 |
let (def_as, def_p) = wth.opn_default in |
| 377 |
let def = pr_pattern (str "_") def_as (mt ()) avoid def_p in |
| 378 |
let br = br ++ def in |
| 379 |
v 0 (hv 0 (str "match" ++ spc () ++ e ++ spc () ++ str "with") ++ spc () ++ br ++ str "end") |
| 380 |
| GTacFullMatch (e, brs) -> |
| 381 |
let e = pr_glbexpr E5 avoid e in |
| 382 |
let pr_one_branch (pat,br) = |
| 383 |
let avoid = avoid_glb_pat avoid pat in |
| 384 |
hov 4 (str "|" ++ spc() ++ hov 0 (pr_glb_pat pat ++ spc() ++ str "=>") ++ spc() ++ |
| 385 |
hov 2 (pr_glbexpr E5 avoid br)) |
| 386 |
in |
| 387 |
let brs = prlist_with_sep spc pr_one_branch brs in |
| 388 |
v 0 (hv 0 (str "match" ++ spc () ++ e ++ spc () ++ str "with") ++ spc () ++ brs ++ spc() ++ str "end") |
| 389 |
| GTacPrj (kn, e, n) -> |
| 390 |
begin match Tac2env.interp_type kn with |
| 391 |
| _, GTydDef None -> str "<abstr>" |
| 392 |
| _, GTydDef _ | _, GTydAlg _ | _, GTydOpn -> assert false |
| 393 |
| _, GTydRec def -> |
| 394 |
let (proj, _, _) = List.nth def n in |
| 395 |
let proj = change_kn_label kn proj in |
| 396 |
let proj = pr_projection proj in |
| 397 |
let e = pr_glbexpr E0 avoid e in |
| 398 |
hov 0 (e ++ str "." ++ paren proj) |
| 399 |
end |
| 400 |
| GTacSet (kn, e, n, r) -> |
| 401 |
begin match Tac2env.interp_type kn with |
| 402 |
| _, GTydDef None -> str "<abstr>" |
| 403 |
| _, GTydDef _ | _, GTydAlg _ | _, GTydOpn -> assert false |
| 404 |
| _, GTydRec def -> |
| 405 |
let (proj, _, _) = List.nth def n in |
| 406 |
let proj = change_kn_label kn proj in |
| 407 |
let proj = pr_projection proj in |
| 408 |
let e = pr_glbexpr E0 avoid e in |
| 409 |
let r = pr_glbexpr E1 avoid r in |
| 410 |
hov 0 (e ++ str "." ++ paren proj ++ spc () ++ str ":=" ++ spc () ++ r) |
| 411 |
end |
| 412 |
| GTacOpn (kn, []) -> pr_constructor kn |
| 413 |
| GTacOpn (kn, cl) -> |
| 414 |
let paren = match lvl with |
| 415 |
| E0 -> paren |
| 416 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 417 |
in |
| 418 |
let c = pr_constructor kn in |
| 419 |
paren (hov 0 (c ++ spc () ++ (pr_sequence (pr_glbexpr E0 avoid) cl))) |
| 420 |
| GTacExt (tag, arg) -> |
| 421 |
let tpe = interp_ml_object tag in |
| 422 |
let env = Global.env() in |
| 423 |
let sigma = Evd.from_env env in |
| 424 |
hov 0 (tpe.ml_print env sigma arg) |
| 425 |
| GTacPrm prm -> |
| 426 |
hov 0 (str "@external" ++ spc () ++ qstring prm.mltac_plugin ++ spc () ++ |
| 427 |
qstring prm.mltac_tactic) |
| 428 |
and pr_applied_constructor lvl avoid tpe n cl = |
| 429 |
let factorized = |
| 430 |
if KerName.equal tpe t_list then |
| 431 |
let rec factorize accu = function |
| 432 |
| GTacCst (_, 0, []) -> accu, None |
| 433 |
| GTacCst (_, 0, [e; l]) -> factorize (e :: accu) l |
| 434 |
| e -> accu, Some e |
| 435 |
in |
| 436 |
let l, e = factorize [] (GTacCst (Other tpe, n, cl)) in |
| 437 |
match e with |
| 438 |
| None -> FullList l |
| 439 |
| Some e -> ListPrefix (l,e) |
| 440 |
else Other (n,cl) |
| 441 |
in |
| 442 |
pr_factorized_constructor (fun lvl -> pr_glbexpr lvl avoid) lvl tpe factorized |
| 443 |
in |
| 444 |
hov 0 (pr_glbexpr lvl avoid c) |
| 445 |
|
| 446 |
let pr_glbexpr ~avoid c = |
| 447 |
pr_glbexpr_gen E5 ~avoid c |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
let pr_relid pr = function |
| 453 |
| AbsKn x -> pr x |
| 454 |
| RelId qid -> Libnames.pr_qualid qid |
| 455 |
|
| 456 |
let swap_tuple_relid : _ or_tuple or_relid -> _ or_relid or_tuple = function |
| 457 |
| RelId _ as x -> Other x |
| 458 |
| AbsKn (Tuple _ as x) -> x |
| 459 |
| AbsKn (Other x) -> Other (AbsKn x) |
| 460 |
|
| 461 |
let rec pr_rawtype_gen lvl t = match t.CAst.v with |
| 462 |
| CTypVar Anonymous -> str "_" |
| 463 |
| CTypVar (Name id) -> str "'" ++ Id.print id |
| 464 |
| CTypArrow (t1, t2) -> |
| 465 |
let paren = match lvl with |
| 466 |
| T5_r -> fun x -> x |
| 467 |
| T5_l | T2 | T1 | T0 -> paren |
| 468 |
in |
| 469 |
paren (pr_rawtype_gen T5_l t1 ++ spc () ++ str "->" ++ spc () ++ pr_rawtype_gen T5_r t2) |
| 470 |
| CTypRef (kn, args) -> match swap_tuple_relid kn with |
| 471 |
| Tuple n -> |
| 472 |
let () = if not (Int.equal n (List.length args)) |
| 473 |
then CErrors.anomaly ?loc:t.loc Pp.(str "Incorrect tuple.") |
| 474 |
in |
| 475 |
if Int.equal n 0 then Libnames.pr_qualid (Tac2env.shortest_qualid_of_type t_unit) |
| 476 |
else |
| 477 |
let paren = match lvl with |
| 478 |
| T5_r | T5_l -> fun x -> x |
| 479 |
| T2 | T1 | T0 -> paren |
| 480 |
in |
| 481 |
paren (prlist_with_sep (fun () -> str " * ") (pr_rawtype_gen T2) args) |
| 482 |
| Other kn -> |
| 483 |
let ppkn = pr_relid pr_typref kn in |
| 484 |
match args with |
| 485 |
| [] -> ppkn |
| 486 |
| [arg] -> |
| 487 |
let paren = match lvl with |
| 488 |
| T5_r | T5_l | T2 | T1 -> fun x -> x |
| 489 |
| T0 -> paren |
| 490 |
in |
| 491 |
paren (pr_rawtype_gen T1 arg ++ spc () ++ ppkn) |
| 492 |
| _ -> |
| 493 |
let paren = match lvl with |
| 494 |
| T5_r | T5_l | T2 | T1 -> fun x -> x |
| 495 |
| T0 -> paren |
| 496 |
in |
| 497 |
paren (surround (prlist_with_sep pr_comma (pr_rawtype_gen lvl) args) |
| 498 |
++ spc () ++ ppkn) |
| 499 |
|
| 500 |
|
| 501 |
let rec ids_of_pattern accu {CAst.v=pat} = match pat with |
| 502 |
| CPatVar Anonymous | CPatAtm _ -> accu |
| 503 |
| CPatVar (Name id) -> Id.Set.add id accu |
| 504 |
| CPatAs (p,id) -> ids_of_pattern (Id.Set.add id.v accu) p |
| 505 |
| CPatRef (_, pl) | CPatOr pl -> |
| 506 |
List.fold_left ids_of_pattern accu pl |
| 507 |
| CPatCnv (pat, _) -> ids_of_pattern accu pat |
| 508 |
| CPatRecord pats -> List.fold_left (fun accu (_,pat) -> ids_of_pattern accu pat) accu pats |
| 509 |
|
| 510 |
let rec pr_rawpat_gen lvl p = match p.CAst.v with |
| 511 |
| CPatVar x -> pr_name x |
| 512 |
| CPatAtm a -> pr_atom a |
| 513 |
| CPatRef (AbsKn (Tuple n), pats) -> |
| 514 |
let () = if not (Int.equal n (List.length pats)) |
| 515 |
then CErrors.anomaly ?loc:p.loc Pp.(str "Incorrect tuple.") |
| 516 |
in |
| 517 |
str "(" ++ prlist_with_sep pr_comma (pr_rawpat_gen E1) pats ++ str ")" |
| 518 |
| CPatRef (RelId r,pats) -> |
| 519 |
let paren = match lvl with |
| 520 |
| E0 -> paren |
| 521 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 522 |
in |
| 523 |
paren (hov 0 (Libnames.pr_qualid r ++ spc() ++ pr_sequence (pr_rawpat_gen E0) pats)) |
| 524 |
| CPatRef (AbsKn (Other kn), pats) -> |
| 525 |
let rec factorize accu pat = match pat.CAst.v with |
| 526 |
| CPatRef (AbsKn (Other kn), pats) when KerName.equal kn c_nil -> |
| 527 |
let () = if not (CList.is_empty pats) |
| 528 |
then CErrors.anomaly ?loc:p.loc (str "Incorrect list pattern.") |
| 529 |
in |
| 530 |
accu, None |
| 531 |
| CPatRef (AbsKn (Other kn), pats) when KerName.equal kn c_cons -> |
| 532 |
let hd, tl = match pats with |
| 533 |
| [hd;tl] -> hd, tl |
| 534 |
| _ -> CErrors.anomaly ?loc:p.loc (str "Incorrect list pattern.") |
| 535 |
in |
| 536 |
factorize (hd::accu) tl |
| 537 |
| _ -> accu, Some pat |
| 538 |
in |
| 539 |
begin match factorize [] p with |
| 540 |
| pats, None -> |
| 541 |
hov 2 (str "[" ++ prlist_with_sep pr_semicolon (pr_rawpat_gen E4) (List.rev pats) ++ str "]") |
| 542 |
| (_ :: _) as pats, Some rest -> |
| 543 |
let paren = match lvl with |
| 544 |
| E0 | E1 | E2 -> paren |
| 545 |
| E3 | E4 | E5 -> fun x -> x |
| 546 |
in |
| 547 |
let pr_cons () = spc () ++ str "::" ++ spc () in |
| 548 |
paren (hov 2 (prlist_with_sep pr_cons (pr_rawpat_gen E1) (List.rev (rest :: pats)))) |
| 549 |
| [], Some p' -> |
| 550 |
assert (p == p'); |
| 551 |
if CList.is_empty pats then pr_constructor kn |
| 552 |
else |
| 553 |
let paren = match lvl with |
| 554 |
| E0 -> paren |
| 555 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 556 |
in |
| 557 |
paren (hov 0 (pr_constructor kn ++ spc() ++ pr_sequence (pr_rawpat_gen E0) pats)) |
| 558 |
end |
| 559 |
| CPatRecord pats -> |
| 560 |
let pr_field (proj, p) = |
| 561 |
pr_relid pr_projection proj ++ spc() ++ str ":=" ++ spc() ++ pr_rawpat_gen E1 p |
| 562 |
in |
| 563 |
hov 0 (str "{" ++ spc() ++ prlist_with_sep pr_semicolon pr_field pats ++ spc() ++ str "}") |
| 564 |
| CPatCnv (pat,t) -> |
| 565 |
let paren = match lvl with |
| 566 |
| E0 -> paren |
| 567 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 568 |
in |
| 569 |
paren (pr_rawpat_gen E5 pat ++ spc() ++ str ":" ++ spc() ++ pr_rawtype_gen T5_l t) |
| 570 |
| CPatOr pats -> |
| 571 |
let paren = match lvl with |
| 572 |
| E0 -> paren |
| 573 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 574 |
in |
| 575 |
paren (hv 0 (prlist_with_sep (fun () -> spc() ++ str "|" ++ spc()) (pr_rawpat_gen E1) pats)) |
| 576 |
| CPatAs (p,x) -> |
| 577 |
let paren = match lvl with |
| 578 |
| E0 -> paren |
| 579 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 580 |
in |
| 581 |
paren (hv 0 (pr_rawpat_gen E1 p ++ spc() ++ str "as" ++ spc () ++ Id.print x.v)) |
| 582 |
|
| 583 |
|
| 584 |
let base_internal_ty_ident = Id.of_string "__α" |
| 585 |
|
| 586 |
let pr_rawexpr_gen lvl ~avoid c = |
| 587 |
let rec pr_rawexpr lvl avoid c = |
| 588 |
let loc = c.CAst.loc in |
| 589 |
match c.CAst.v with |
| 590 |
| CTacAtm a -> pr_atom a |
| 591 |
| CTacRef (RelId qid) -> Libnames.pr_qualid qid |
| 592 |
| CTacRef (AbsKn (TacConstant r)) -> pr_tacref avoid r |
| 593 |
| CTacRef (AbsKn (TacAlias _ as r)) -> Libnames.pr_qualid (Tac2env.shortest_qualid_of_ltac avoid r) |
| 594 |
| CTacCst (RelId qid) -> Libnames.pr_qualid qid |
| 595 |
| CTacCst (AbsKn (Tuple 0)) -> str "()" |
| 596 |
| CTacCst (AbsKn (Tuple n)) -> CErrors.anomaly ?loc Pp.(str "Incorrect tuple.") |
| 597 |
| CTacCst (AbsKn (Other kn)) when KerName.equal c_nil kn -> str "[]" |
| 598 |
| CTacCst (AbsKn (Other kn)) -> pr_constructor kn |
| 599 |
| CTacFun (pats, c) -> |
| 600 |
let avoid = List.fold_left ids_of_pattern avoid pats in |
| 601 |
let pats = pr_sequence (pr_rawpat_gen E0) pats in |
| 602 |
let paren = match lvl with |
| 603 |
| E0 | E1 | E2 | E3 | E4 -> paren |
| 604 |
| E5 -> fun x -> x |
| 605 |
in |
| 606 |
paren (hov 0 (hov 2 (str "fun" ++ spc() ++ pats) ++ spc() ++ str "=>" ++ spc() ++ |
| 607 |
pr_rawexpr E5 avoid c)) |
| 608 |
| CTacApp ({v=CTacCst (AbsKn (Tuple 0))}, args) -> |
| 609 |
let paren = match lvl with |
| 610 |
| E0 -> paren |
| 611 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 612 |
in |
| 613 |
paren (hov 0 (str "()" ++ spc() ++ pr_sequence (pr_rawexpr E0 avoid) args)) |
| 614 |
| CTacApp ({v=CTacCst (AbsKn (Tuple n))}, args) -> |
| 615 |
let () = if not (Int.equal n (List.length args)) |
| 616 |
then CErrors.anomaly ?loc Pp.(str "Incorrect tuple.") |
| 617 |
in |
| 618 |
surround (prlist_with_sep pr_comma (pr_rawexpr E1 avoid) args) |
| 619 |
| CTacApp ({v=CTacCst (AbsKn (Other kn))}, _) when KerName.equal kn c_cons -> |
| 620 |
let rec factorize accu c = match c.CAst.v with |
| 621 |
| CTacCst (AbsKn (Other kn)) when KerName.equal c_nil kn -> accu, None |
| 622 |
| CTacApp ({v=CTacCst (AbsKn (Other kn))}, args) when KerName.equal kn c_cons -> |
| 623 |
let hd, tl = match args with |
| 624 |
| [hd;tl] -> hd, tl |
| 625 |
| _ -> CErrors.anomaly ?loc (str "Incorrect list.") |
| 626 |
in |
| 627 |
factorize (hd::accu) tl |
| 628 |
| _ -> accu, Some c |
| 629 |
in |
| 630 |
begin match factorize [] c with |
| 631 |
| l, None -> |
| 632 |
hov 2 (str "[" ++ prlist_with_sep pr_semicolon (pr_rawexpr E4 avoid) (List.rev l) ++ str "]") |
| 633 |
| l, Some rest -> |
| 634 |
let paren = match lvl with |
| 635 |
| E0 | E1 | E2 -> paren |
| 636 |
| E3 | E4 | E5 -> fun x -> x |
| 637 |
in |
| 638 |
let pr_cons () = spc () ++ str "::" ++ spc () in |
| 639 |
paren (hov 2 (prlist_with_sep pr_cons (pr_rawexpr E1 avoid) (List.rev (rest :: l)))) |
| 640 |
end |
| 641 |
| CTacApp (hd,args) -> |
| 642 |
let paren = match lvl with |
| 643 |
| E0 -> paren |
| 644 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 645 |
in |
| 646 |
paren (hov 0 (pr_rawexpr E0 avoid hd ++ spc() ++ pr_sequence (pr_rawexpr E0 avoid) args)) |
| 647 |
| CTacSyn (_,kn) -> fmt "<notation %t>" (fun () -> KerName.print kn) |
| 648 |
| CTacLet (isrec, bnd, e) -> |
| 649 |
let paren = match lvl with |
| 650 |
| E0 | E1 | E2 | E3 | E4 -> paren |
| 651 |
| E5 -> fun x -> x |
| 652 |
in |
| 653 |
let pprec = if isrec then str "rec" ++ spc () else mt () in |
| 654 |
let avoidbnd = List.fold_left (fun avoid (pat,_) -> ids_of_pattern avoid pat) avoid bnd in |
| 655 |
let pr_bnd (pat, e) = |
| 656 |
let avoid = if isrec then avoidbnd else avoid in |
| 657 |
hov (-2 - if isrec then 4 else 0) |
| 658 |
(pr_rawpat_gen E0 pat ++ str " :=" ++ spc () ++ hov 2 (pr_rawexpr E5 avoid e)) |
| 659 |
in |
| 660 |
let bnd = prlist_with_sep (fun () -> spc() ++ str "with ") pr_bnd bnd in |
| 661 |
paren (v 0 (hov 0 (v 0 (str "let " ++ pprec ++ bnd) ++ spc() ++ str "in") ++ spc ()) ++ pr_rawexpr E5 avoidbnd e) |
| 662 |
| CTacCnv (e,t) -> |
| 663 |
paren (pr_rawexpr E5 avoid e ++ spc() ++ str ":" ++ spc() ++ pr_rawtype_gen T5_l t) |
| 664 |
| CTacSeq (e1,e2) -> |
| 665 |
let paren = match lvl with |
| 666 |
| E0 | E1 | E2 | E3 | E4 -> paren |
| 667 |
| E5 -> fun x -> x |
| 668 |
in |
| 669 |
paren (pr_rawexpr E4 avoid e1 ++ str ";" ++ spc() ++ pr_rawexpr E4 avoid e2) |
| 670 |
| CTacIft (b,e1,e2) -> |
| 671 |
let paren = match lvl with |
| 672 |
| E0 | E1 | E2 | E3 | E4 -> paren |
| 673 |
| E5 -> fun x -> x |
| 674 |
in |
| 675 |
paren (str "if" ++ spc() ++ pr_rawexpr E5 avoid b ++ spc() |
| 676 |
++ str "then" ++ spc() ++ pr_rawexpr E5 avoid e1 ++ spc() |
| 677 |
++ str "else" ++ spc() ++ pr_rawexpr E5 avoid e2) |
| 678 |
| CTacCse (e,brs) -> |
| 679 |
let pr_one_branch (pat,br) = |
| 680 |
let avoid = ids_of_pattern avoid pat in |
| 681 |
hov 4 (str "|" ++ spc() ++ hov 0 (pr_rawpat_gen E5 pat ++ spc() ++ str "=>") ++ spc() ++ |
| 682 |
hov 2 (pr_rawexpr E5 avoid br)) |
| 683 |
in |
| 684 |
let brs = prlist_with_sep spc pr_one_branch brs in |
| 685 |
v 0 (hv 0 (str "match" ++ spc() ++ pr_rawexpr E5 avoid e ++ spc() ++ str "with") |
| 686 |
++ spc() ++ brs ++ spc() ++ str "end") |
| 687 |
| CTacRec (def,fields) -> |
| 688 |
let def = match def with |
| 689 |
| None -> mt() |
| 690 |
| Some def -> pr_rawexpr E0 avoid def ++ spc() ++ str "with" ++ spc() |
| 691 |
in |
| 692 |
let pr_field (proj,e) = |
| 693 |
pr_relid pr_projection proj ++ spc() ++ str ":=" ++ spc() ++ pr_rawexpr E1 avoid e |
| 694 |
in |
| 695 |
hov 2 (str "{" ++ spc() ++ def ++ prlist_with_sep pr_semicolon pr_field fields ++ str "}") |
| 696 |
| CTacPrj (e,p) -> |
| 697 |
hov 0 (pr_rawexpr E0 avoid e ++ str "." ++ paren (pr_relid pr_projection p)) |
| 698 |
| CTacSet (e1,p,e2) -> |
| 699 |
hov 0 (pr_rawexpr E0 avoid e1 ++ str "." ++ paren (pr_relid pr_projection p) |
| 700 |
++ spc() ++ str ":=" ++ spc() ++ pr_rawexpr E1 avoid e2) |
| 701 |
| CTacExt (tag,arg) -> |
| 702 |
let obj = interp_ml_object tag in |
| 703 |
let env = Global.env() in |
| 704 |
let sigma = Evd.from_env env in |
| 705 |
obj.ml_raw_print env sigma arg |
| 706 |
| CTacGlb (prms, args, body, ty) -> |
| 707 |
let avoid, tynames = |
| 708 |
Array.fold_left_map (fun avoid () -> |
| 709 |
let na = Namegen.next_ident_away base_internal_ty_ident avoid in |
| 710 |
let avoid = Id.Set.add na avoid in |
| 711 |
avoid, Id.to_string na) |
| 712 |
avoid |
| 713 |
(Array.make prms ()) |
| 714 |
in |
| 715 |
let tynames i = tynames.(i) in |
| 716 |
let pr_arg (pat, arg, ty) = |
| 717 |
let bnd = match ty with |
| 718 |
| Some ty -> |
| 719 |
paren (pr_name pat.CAst.v ++ spc() ++ str ":" ++ spc() ++ pr_glbtype_gen tynames T5_l ty) |
| 720 |
| None -> pr_name pat.CAst.v |
| 721 |
in |
| 722 |
hov (-2) (bnd ++ str " :=" ++ spc() ++ hov 2 (pr_rawexpr E5 avoid arg)) |
| 723 |
in |
| 724 |
let paren = match lvl with |
| 725 |
| E0 | E1 | E2 | E3 | E4 -> paren |
| 726 |
| E5 -> fun x -> x |
| 727 |
in |
| 728 |
let bnd = prlist_with_sep (fun () -> spc() ++ str "with ") pr_arg args in |
| 729 |
paren (v 0 (hov 0 (v 0 (str "let " ++ bnd) ++ spc() ++ str "in") ++ spc()) |
| 730 |
++ pr_glbexpr_gen ~avoid E5 body ++ spc() |
| 731 |
++ str ":" ++ pr_glbtype_gen tynames T5_l ty) |
| 732 |
in |
| 733 |
hov 0 (pr_rawexpr lvl avoid c) |
| 734 |
|
| 735 |
let pr_mutflag ismut = if ismut then str "mutable " else mt() |
| 736 |
|
| 737 |
let pr_isrec isrec = if isrec then str "rec " else mt() |
| 738 |
|
| 739 |
let pr_one_strval ~isrec (na, e) = |
| 740 |
let avoid = match na.CAst.v with |
| 741 |
| Anonymous -> Id.Set.empty |
| 742 |
| Name id -> if isrec then Id.Set.singleton id else Id.Set.empty |
| 743 |
in |
| 744 |
pr_lname na ++ str " :=" ++ spc() ++ pr_rawexpr_gen E5 ~avoid e |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
let pr_one_strtyp (qid, redef, (params, v)) = |
| 749 |
let ppparams = match params with |
| 750 |
| [] -> mt() |
| 751 |
| [x] -> pr_lident x ++ spc() |
| 752 |
| _ -> surround (prlist_with_sep pr_comma pr_lident params) ++ spc() |
| 753 |
in |
| 754 |
let ppredef = if redef then str " ::=" ++ spc() else str " :=" ++ spc() in |
| 755 |
let ppv = match v with |
| 756 |
| CTydDef None -> mt() |
| 757 |
| CTydDef (Some typ) -> ppredef ++ pr_rawtype_gen T5_r typ |
| 758 |
| CTydAlg [] -> ppredef ++ str "[ ]" |
| 759 |
| CTydAlg ctors -> |
| 760 |
let pr_one_ctor (atts, na, typs) = |
| 761 |
let pptyps = |
| 762 |
if CList.is_empty typs then mt() |
| 763 |
else |
| 764 |
spc() ++ surround (prlist_with_sep pr_comma (pr_rawtype_gen T5_r) typs) |
| 765 |
in |
| 766 |
hov 2 (Ppvernac.pr_vernac_attributes atts ++ Id.print na.CAst.v ++ pptyps) |
| 767 |
in |
| 768 |
let ppctors = match ctors with |
| 769 |
| [] -> str "[ ]" |
| 770 |
| [c] -> str "[" ++ spc() ++ pr_one_ctor c ++ spc() ++ str "]" |
| 771 |
| _ -> |
| 772 |
hv 0 |
| 773 |
(str "[" ++ spc() ++ |
| 774 |
prlist_with_sep spc (fun c -> str "| " ++ pr_one_ctor c) ctors ++ |
| 775 |
spc() ++ str "]") |
| 776 |
in |
| 777 |
ppredef ++ ppctors |
| 778 |
| CTydRec fields -> |
| 779 |
ppredef ++ |
| 780 |
let pr_one_field (id, ismut, typ) = |
| 781 |
hov 2 (pr_mutflag ismut ++ Id.print id ++ str " :" ++ spc() ++ pr_rawtype_gen T5_r typ ++ str ";") ++ spc() |
| 782 |
in |
| 783 |
hv 2 |
| 784 |
(str "{" ++ spc() ++ |
| 785 |
prlist_with_sep mt pr_one_field fields ++ |
| 786 |
str "}") |
| 787 |
| CTydOpn -> ppredef ++ str "[ .. ]" |
| 788 |
in |
| 789 |
hov 0 (ppparams ++ Libnames.pr_qualid qid ++ ppv) |
| 790 |
|
| 791 |
let pr_strexpr = function |
| 792 |
| StrVal (ismut, isrec, vals) -> |
| 793 |
pr_mutflag ismut ++ pr_isrec isrec ++ |
| 794 |
prlist_with_sep (fun () -> spc() ++ str "with ") (pr_one_strval ~isrec) vals |
| 795 |
| StrTyp (isrec, typs) -> |
| 796 |
str "Type " ++ pr_isrec isrec ++ |
| 797 |
hv 0 (prlist_with_sep (fun () -> spc() ++ str "with ") pr_one_strtyp typs) |
| 798 |
| StrPrm (id, typ, v) -> |
| 799 |
str "@external " ++ pr_lident id ++ spc() ++ str ": " ++ hov 0 (pr_rawtype_gen T5_r typ) |
| 800 |
++ spc() ++ hov 2 (str ":= " ++ qstring v.mltac_plugin ++ spc() ++ qstring v.mltac_tactic) |
| 801 |
| StrMut (qid, asna, e) -> |
| 802 |
let avoid = match asna with |
| 803 |
| None -> Id.Set.empty |
| 804 |
| Some asid -> Id.Set.singleton asid.v |
| 805 |
in |
| 806 |
str "Set " ++ Libnames.pr_qualid qid ++ |
| 807 |
pr_opt (fun asid -> str "as " ++ pr_lident asid) asna ++ |
| 808 |
spc() ++ hov 2 (str ":= " ++ pr_rawexpr_gen E5 ~avoid e) |
| 809 |
|
| 810 |
let rec pr_syntax_class = let open CAst in function |
| 811 |
| SexprStr {v=s} -> qstring s |
| 812 |
| SexprInt {v=n} -> Pp.int n |
| 813 |
| SexprRec (_, {v=na}, args) -> |
| 814 |
let na = match na with |
| 815 |
| None -> str "_" |
| 816 |
| Some id -> Libnames.pr_qualid id |
| 817 |
in |
| 818 |
let ppargs = if CList.is_empty args then mt() |
| 819 |
else str "(" ++ prlist_with_sep (fun () -> str ", ") pr_syntax_class args ++ str ")" |
| 820 |
in |
| 821 |
na ++ ppargs |
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
let rec subst_type subst (t : 'a glb_typexpr) = match t with |
| 826 |
| GTypVar id -> subst.(id) |
| 827 |
| GTypArrow (t1, t2) -> GTypArrow (subst_type subst t1, subst_type subst t2) |
| 828 |
| GTypRef (qid, args) -> |
| 829 |
GTypRef (qid, List.map (fun t -> subst_type subst t) args) |
| 830 |
|
| 831 |
let unfold kn args = |
| 832 |
let (nparams, def) = Tac2env.interp_type kn in |
| 833 |
match def with |
| 834 |
| GTydDef (Some def) -> |
| 835 |
let args = Array.of_list args in |
| 836 |
Some (subst_type args def) |
| 837 |
| _ -> None |
| 838 |
|
| 839 |
let rec kind t = match t with |
| 840 |
| GTypVar id -> GTypVar id |
| 841 |
| GTypRef (Other kn, tl) -> |
| 842 |
begin match unfold kn tl with |
| 843 |
| None -> t |
| 844 |
| Some t -> kind t |
| 845 |
end |
| 846 |
| GTypArrow _ | GTypRef (Tuple _, _) -> t |
| 847 |
|
| 848 |
type val_printer = |
| 849 |
{ val_printer : 'a. Environ.env -> Evd.evar_map -> Tac2val.valexpr -> 'a glb_typexpr list -> Pp.t } |
| 850 |
|
| 851 |
let printers = ref KerName.Map.empty |
| 852 |
|
| 853 |
let register_val_printer kn pr = |
| 854 |
printers := KerName.Map.add kn pr !printers |
| 855 |
|
| 856 |
open Tac2ffi |
| 857 |
|
| 858 |
let rec pr_valexpr_gen env sigma lvl v t = match kind t with |
| 859 |
| GTypVar _ -> str "<poly>" |
| 860 |
| GTypRef (Other kn, params) -> |
| 861 |
let pr = try Some (KerName.Map.find kn !printers) with Not_found -> None in |
| 862 |
begin match pr with |
| 863 |
| Some pr -> |
| 864 |
|
| 865 |
pr.val_printer env sigma v params |
| 866 |
| None -> |
| 867 |
let n, repr = Tac2env.interp_type kn in |
| 868 |
if KerName.equal kn t_list then |
| 869 |
pr_val_list env sigma (to_list (fun v -> repr_to valexpr v) v) (List.hd params) |
| 870 |
else match repr with |
| 871 |
| GTydDef None -> str "<abstr>" |
| 872 |
| GTydDef (Some _) -> |
| 873 |
|
| 874 |
assert false |
| 875 |
| GTydAlg alg -> |
| 876 |
if Tac2val.Valexpr.is_int v then |
| 877 |
pr_internal_constructor kn (Tac2ffi.to_int v) true |
| 878 |
else |
| 879 |
let paren = match lvl with |
| 880 |
| E0 -> paren |
| 881 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 882 |
in |
| 883 |
let (n, args) = Tac2ffi.to_block v in |
| 884 |
let (_, id, tpe) = find_constructor n false alg.galg_constructors in |
| 885 |
let knc = change_kn_label kn id in |
| 886 |
let args = pr_constrargs env sigma params args tpe in |
| 887 |
paren (pr_constructor knc ++ spc () ++ args) |
| 888 |
| GTydRec rcd -> |
| 889 |
let (_, args) = Tac2ffi.to_block v in |
| 890 |
pr_record env sigma params args rcd |
| 891 |
| GTydOpn -> |
| 892 |
begin match Tac2ffi.to_open v with |
| 893 |
| (knc, [||]) -> pr_constructor knc |
| 894 |
| (knc, args) -> |
| 895 |
let paren = match lvl with |
| 896 |
| E0 -> paren |
| 897 |
| E1 | E2 | E3 | E4 | E5 -> fun x -> x |
| 898 |
in |
| 899 |
let data = Tac2env.interp_constructor knc in |
| 900 |
let args = pr_constrargs env sigma params args data.Tac2env.cdata_args in |
| 901 |
paren (pr_constructor knc ++ spc () ++ args) |
| 902 |
end |
| 903 |
end |
| 904 |
| GTypArrow _ -> str "<fun>" |
| 905 |
| GTypRef (Tuple 0, []) -> str "()" |
| 906 |
| GTypRef (Tuple _, tl) -> |
| 907 |
let blk = Array.to_list (snd (to_block v)) in |
| 908 |
if List.length blk == List.length tl then |
| 909 |
let prs = List.map2 (fun v t -> pr_valexpr_gen env sigma E1 v t) blk tl in |
| 910 |
hv 2 (str "(" ++ prlist_with_sep pr_comma (fun p -> p) prs ++ str ")") |
| 911 |
else |
| 912 |
str "<unknown>" |
| 913 |
|
| 914 |
and pr_constrargs env sigma params args tpe = |
| 915 |
let subst = Array.of_list params in |
| 916 |
let tpe = List.map (fun t -> subst_type subst t) tpe in |
| 917 |
let args = Array.to_list args in |
| 918 |
let args = List.combine args tpe in |
| 919 |
pr_sequence (fun (v, t) -> pr_valexpr_gen env sigma E0 v t) args |
| 920 |
|
| 921 |
and pr_record env sigma params args rcd = |
| 922 |
let subst = Array.of_list params in |
| 923 |
let map (id, _, tpe) = (id, subst_type subst tpe) in |
| 924 |
let rcd = List.map map rcd in |
| 925 |
let args = Array.to_list args in |
| 926 |
let fields = List.combine rcd args in |
| 927 |
let pr_field ((id, t), arg) = |
| 928 |
Id.print id ++ spc () ++ str ":=" ++ spc () ++ pr_valexpr_gen env sigma E1 arg t |
| 929 |
in |
| 930 |
str "{" ++ spc () ++ prlist_with_sep pr_semicolon pr_field fields ++ spc () ++ str "}" |
| 931 |
|
| 932 |
and pr_val_list env sigma args tpe = |
| 933 |
let pr v = pr_valexpr_gen env sigma E4 v tpe in |
| 934 |
hov 1 (str "[" ++ prlist_with_sep pr_semicolon pr args ++ str "]") |
| 935 |
|
| 936 |
let pr_valexpr env sigma v t = pr_valexpr_gen env sigma E5 v t |
| 937 |
|
| 938 |
let register_init n f = |
| 939 |
let kn = KerName.make Tac2env.rocq_prefix (Id.of_string n) in |
| 940 |
register_val_printer kn { val_printer = fun env sigma v _ -> f env sigma v } |
| 941 |
|
| 942 |
let () = register_init "int" begin fun _ _ n -> |
| 943 |
let n = to_int n in |
| 944 |
Pp.int n |
| 945 |
end |
| 946 |
|
| 947 |
let () = register_init "string" begin fun _ _ s -> |
| 948 |
let s = to_string s in |
| 949 |
Pp.quote (str s) |
| 950 |
end |
| 951 |
|
| 952 |
let () = register_init "ident" begin fun _ _ id -> |
| 953 |
let id = to_ident id in |
| 954 |
str "@" ++ Id.print id |
| 955 |
end |
| 956 |
|
| 957 |
let () = register_init "constr" begin fun env sigma c -> |
| 958 |
let c = to_constr c in |
| 959 |
let c = try Printer.pr_leconstr_env env sigma c with _ -> str "..." in |
| 960 |
hov 2 (str "constr:(" ++ c ++ str ")") |
| 961 |
end |
| 962 |
|
| 963 |
let () = register_init "preterm" begin fun env sigma c -> |
| 964 |
let c = to_preterm c in |
| 965 |
|
| 966 |
let c = try Printer.pr_closed_lglob_env env sigma c with _ -> str "..." in |
| 967 |
hov 2 (str "preterm:(" ++ c ++ str ")") |
| 968 |
end |
| 969 |
|
| 970 |
let () = register_init "pattern" begin fun env sigma c -> |
| 971 |
let c = to_pattern c in |
| 972 |
let c = try Printer.pr_lconstr_pattern_env env sigma c with _ -> str "..." in |
| 973 |
hov 2 (str "pat:(" ++ c ++ str ")") |
| 974 |
end |
| 975 |
|
| 976 |
let () = register_init "message" begin fun _ _ pp -> |
| 977 |
hov 2 (str "message:(" ++ to_pp pp ++ str ")") |
| 978 |
end |
| 979 |
|
| 980 |
let () = register_init "err" begin fun _ _ e -> |
| 981 |
let e = to_err e in |
| 982 |
hov 2 (str "err:(" ++ CErrors.iprint_no_report e ++ str ")") |
| 983 |
end |
| 984 |
|
| 985 |
let () = |
| 986 |
let kn = KerName.make Tac2env.rocq_prefix (Id.of_string "array") in |
| 987 |
let val_printer env sigma v arg = match arg with |
| 988 |
| [arg] -> |
| 989 |
let (_, v) = to_block v in |
| 990 |
str "[|" ++ spc () ++ |
| 991 |
prvect_with_sep pr_semicolon (fun a -> pr_valexpr env sigma a arg) v ++ |
| 992 |
spc () ++ str "|]" |
| 993 |
| _ -> assert false |
| 994 |
in |
| 995 |
register_val_printer kn { val_printer } |
| 996 |
|
| 997 |
let () = |
| 998 |
let kn = KerName.make (MPfile (Libnames.dirpath_of_string "Ltac2.Module")) (Id.of_string "t") in |
| 999 |
let val_printer _env _sigma v _arg = |
| 1000 |
let v = to_modpath v in |
| 1001 |
|
| 1002 |
str "module:(" ++ ModPath.print v ++ str ")" |
| 1003 |
in |
| 1004 |
register_val_printer kn { val_printer } |
| 1005 |
|
| 1006 |
let pr_profile_frame = let open Pp in function |
| 1007 |
| FrAnon e -> str "<fun " ++ pr_glbexpr ~avoid:Id.Set.empty e ++ str ">" |
| 1008 |
| FrLtac kn -> pr_tacref Id.Set.empty kn |
| 1009 |
| FrPrim ml -> str "<" ++ str ml.mltac_plugin ++ str ":" ++ str ml.mltac_tactic ++ str ">" |
| 1010 |
| FrExtn (tag,_) -> str "<extn:" ++ str (Tac2dyn.Arg.repr tag) ++ str ">" |
| 1011 |
|
| 1012 |
let () = Hook.set Tac2bt.pr_frame_hook pr_profile_frame |