1(* Title: Pure/proofterm.ML 2 Author: Stefan Berghofer, TU Muenchen 3 4LF style proof terms. 5*) 6 7infix 8 % %% %>; 8 9signature PROOFTERM = 10sig 11 type thm_header = 12 {serial: serial, pos: Position.T list, theory_name: string, name: string, 13 prop: term, types: typ list option} 14 type thm_body 15 type thm_node 16 datatype proof = 17 MinProof 18 | PBound of int 19 | Abst of string * typ option * proof 20 | AbsP of string * term option * proof 21 | % of proof * term option 22 | %% of proof * proof 23 | Hyp of term 24 | PAxm of string * term * typ list option 25 | OfClass of typ * class 26 | Oracle of string * term * typ list option 27 | PThm of thm_header * thm_body 28 and proof_body = PBody of 29 {oracles: ((string * Position.T) * term option) Ord_List.T, 30 thms: (serial * thm_node) Ord_List.T, 31 proof: proof} 32 type oracle = (string * Position.T) * term option 33 type thm = serial * thm_node 34 exception MIN_PROOF of unit 35 val proof_of: proof_body -> proof 36 val join_proof: proof_body future -> proof 37 val map_proof_of: (proof -> proof) -> proof_body -> proof_body 38 val thm_header: serial -> Position.T list -> string -> string -> term -> typ list option -> 39 thm_header 40 val thm_body: proof_body -> thm_body 41 val thm_body_proof_raw: thm_body -> proof 42 val thm_body_proof_open: thm_body -> proof 43 val thm_node_theory_name: thm_node -> string 44 val thm_node_name: thm_node -> string 45 val thm_node_prop: thm_node -> term 46 val thm_node_body: thm_node -> proof_body future 47 val thm_node_thms: thm_node -> thm list 48 val join_thms: thm list -> proof_body list 49 val make_thm: thm_header -> thm_body -> thm 50 val fold_proof_atoms: bool -> (proof -> 'a -> 'a) -> proof list -> 'a -> 'a 51 val fold_body_thms: 52 ({serial: serial, name: string, prop: term, body: proof_body} -> 'a -> 'a) -> 53 proof_body list -> 'a -> 'a 54 val oracle_ord: oracle ord 55 val thm_ord: thm ord 56 val unions_oracles: oracle Ord_List.T list -> oracle Ord_List.T 57 val unions_thms: thm Ord_List.T list -> thm Ord_List.T 58 val no_proof_body: proof -> proof_body 59 val no_thm_names: proof -> proof 60 val no_thm_proofs: proof -> proof 61 val no_body_proofs: proof -> proof 62 63 val encode: Consts.T -> proof XML.Encode.T 64 val encode_body: Consts.T -> proof_body XML.Encode.T 65 val encode_standard_term: Consts.T -> term XML.Encode.T 66 val encode_standard_proof: Consts.T -> proof XML.Encode.T 67 val decode: Consts.T -> proof XML.Decode.T 68 val decode_body: Consts.T -> proof_body XML.Decode.T 69 70 val %> : proof * term -> proof 71 72 (*primitive operations*) 73 val proofs: int Unsynchronized.ref 74 val proofs_enabled: unit -> bool 75 val atomic_proof: proof -> bool 76 val compact_proof: proof -> bool 77 val proof_combt: proof * term list -> proof 78 val proof_combt': proof * term option list -> proof 79 val proof_combP: proof * proof list -> proof 80 val strip_combt: proof -> proof * term option list 81 val strip_combP: proof -> proof * proof list 82 val strip_thm_body: proof_body -> proof_body 83 val map_proof_same: term Same.operation -> typ Same.operation 84 -> (typ * class -> proof) -> proof Same.operation 85 val map_proof_terms_same: term Same.operation -> typ Same.operation -> proof Same.operation 86 val map_proof_types_same: typ Same.operation -> proof Same.operation 87 val map_proof_terms: (term -> term) -> (typ -> typ) -> proof -> proof 88 val map_proof_types: (typ -> typ) -> proof -> proof 89 val fold_proof_terms: (term -> 'a -> 'a) -> proof -> 'a -> 'a 90 val fold_proof_terms_types: (term -> 'a -> 'a) -> (typ -> 'a -> 'a) -> proof -> 'a -> 'a 91 val maxidx_proof: proof -> int -> int 92 val size_of_proof: proof -> int 93 val change_types: typ list option -> proof -> proof 94 val prf_abstract_over: term -> proof -> proof 95 val prf_incr_bv: int -> int -> int -> int -> proof -> proof 96 val incr_pboundvars: int -> int -> proof -> proof 97 val prf_loose_bvar1: proof -> int -> bool 98 val prf_loose_Pbvar1: proof -> int -> bool 99 val prf_add_loose_bnos: int -> int -> proof -> int list * int list -> int list * int list 100 val norm_proof: Envir.env -> proof -> proof 101 val norm_proof': Envir.env -> proof -> proof 102 val prf_subst_bounds: term list -> proof -> proof 103 val prf_subst_pbounds: proof list -> proof -> proof 104 val freeze_thaw_prf: proof -> proof * (proof -> proof) 105 106 (*proof terms for specific inference rules*) 107 val trivial_proof: proof 108 val implies_intr_proof: term -> proof -> proof 109 val implies_intr_proof': term -> proof -> proof 110 val forall_intr_proof: string * term -> typ option -> proof -> proof 111 val forall_intr_proof': term -> proof -> proof 112 val varify_proof: term -> (string * sort) list -> proof -> proof 113 val legacy_freezeT: term -> proof -> proof 114 val rotate_proof: term list -> term -> (string * typ) list -> term list -> int -> proof -> proof 115 val permute_prems_proof: term list -> int -> int -> proof -> proof 116 val generalize_proof: string list * string list -> int -> term -> proof -> proof 117 val instantiate: ((indexname * sort) * typ) list * ((indexname * typ) * term) list 118 -> proof -> proof 119 val lift_proof: term -> int -> term -> proof -> proof 120 val incr_indexes: int -> proof -> proof 121 val assumption_proof: term list -> term -> int -> proof -> proof 122 val bicompose_proof: bool -> term list -> term list -> term option -> term list -> 123 int -> int -> proof -> proof -> proof 124 val equality_axms: (string * term) list 125 val reflexive_axm: proof 126 val symmetric_axm: proof 127 val transitive_axm: proof 128 val equal_intr_axm: proof 129 val equal_elim_axm: proof 130 val abstract_rule_axm: proof 131 val combination_axm: proof 132 val reflexive_proof: proof 133 val symmetric_proof: proof -> proof 134 val transitive_proof: typ -> term -> proof -> proof -> proof 135 val equal_intr_proof: term -> term -> proof -> proof -> proof 136 val equal_elim_proof: term -> term -> proof -> proof -> proof 137 val abstract_rule_proof: string * term -> proof -> proof 138 val combination_proof: term -> term -> term -> term -> proof -> proof -> proof 139 val strip_shyps_proof: Sorts.algebra -> (typ * sort) list -> (typ * sort) list -> 140 sort list -> proof -> proof 141 val of_sort_proof: Sorts.algebra -> 142 (class * class -> proof) -> 143 (string * class list list * class -> proof) -> 144 (typ * class -> proof) -> typ * sort -> proof list 145 val axm_proof: string -> term -> proof 146 val oracle_proof: string -> term -> proof 147 val shrink_proof: proof -> proof 148 149 (*rewriting on proof terms*) 150 val add_prf_rrule: proof * proof -> theory -> theory 151 val add_prf_rproc: (typ list -> term option list -> proof -> (proof * proof) option) -> theory -> theory 152 val set_preproc: (theory -> proof -> proof) -> theory -> theory 153 val apply_preproc: theory -> proof -> proof 154 val forall_intr_variables_term: term -> term 155 val forall_intr_variables: term -> proof -> proof 156 val no_skel: proof 157 val normal_skel: proof 158 val rewrite_proof: theory -> (proof * proof) list * 159 (typ list -> term option list -> proof -> (proof * proof) option) list -> proof -> proof 160 val rewrite_proof_notypes: (proof * proof) list * 161 (typ list -> term option list -> proof -> (proof * proof) option) list -> proof -> proof 162 val rew_proof: theory -> proof -> proof 163 164 val reconstruct_proof: theory -> term -> proof -> proof 165 val prop_of': term list -> proof -> term 166 val prop_of: proof -> term 167 val expand_name_empty: thm_header -> string option 168 val expand_proof: theory -> (thm_header -> string option) -> proof -> proof 169 170 val standard_vars: Name.context -> term * proof option -> term * proof option 171 val standard_vars_term: Name.context -> term -> term 172 val add_standard_vars: proof -> (string * typ) list -> (string * typ) list 173 val add_standard_vars_term: term -> (string * typ) list -> (string * typ) list 174 175 val export_enabled: unit -> bool 176 val export_standard_enabled: unit -> bool 177 val export_proof_boxes_required: theory -> bool 178 val export_proof_boxes: proof_body list -> unit 179 val fulfill_norm_proof: theory -> (serial * proof_body) list -> proof_body -> proof_body 180 val thm_proof: theory -> (class * class -> proof) -> 181 (string * class list list * class -> proof) -> string * Position.T -> sort list -> 182 term list -> term -> (serial * proof_body future) list -> proof_body -> thm * proof 183 val unconstrain_thm_proof: theory -> (class * class -> proof) -> 184 (string * class list list * class -> proof) -> sort list -> term -> 185 (serial * proof_body future) list -> proof_body -> thm * proof 186 val get_identity: sort list -> term list -> term -> proof -> 187 {serial: serial, theory_name: string, name: string} option 188 val get_approximative_name: sort list -> term list -> term -> proof -> string 189 type thm_id = {serial: serial, theory_name: string} 190 val make_thm_id: serial * string -> thm_id 191 val thm_header_id: thm_header -> thm_id 192 val thm_id: thm -> thm_id 193 val get_id: sort list -> term list -> term -> proof -> thm_id option 194 val this_id: thm_id option -> thm_id -> bool 195 val proof_boxes: {excluded: thm_id -> bool, included: thm_id -> bool} -> 196 proof list -> (thm_header * proof) list (*exception MIN_PROOF*) 197end 198 199structure Proofterm : PROOFTERM = 200struct 201 202(** datatype proof **) 203 204type thm_header = 205 {serial: serial, pos: Position.T list, theory_name: string, name: string, 206 prop: term, types: typ list option}; 207 208datatype proof = 209 MinProof 210 | PBound of int 211 | Abst of string * typ option * proof 212 | AbsP of string * term option * proof 213 | op % of proof * term option 214 | op %% of proof * proof 215 | Hyp of term 216 | PAxm of string * term * typ list option 217 | OfClass of typ * class 218 | Oracle of string * term * typ list option 219 | PThm of thm_header * thm_body 220and proof_body = PBody of 221 {oracles: ((string * Position.T) * term option) Ord_List.T, 222 thms: (serial * thm_node) Ord_List.T, 223 proof: proof} 224and thm_body = 225 Thm_Body of {open_proof: proof -> proof, body: proof_body future} 226and thm_node = 227 Thm_Node of {theory_name: string, name: string, prop: term, 228 body: proof_body future, export: unit lazy, consolidate: unit lazy}; 229 230type oracle = (string * Position.T) * term option; 231val oracle_ord: oracle ord = 232 prod_ord (prod_ord fast_string_ord Position.ord) (option_ord Term_Ord.fast_term_ord); 233 234type thm = serial * thm_node; 235val thm_ord: thm ord = fn ((i, _), (j, _)) => int_ord (j, i); 236 237 238exception MIN_PROOF of unit; 239 240fun proof_of (PBody {proof, ...}) = proof; 241val join_proof = Future.join #> proof_of; 242 243fun map_proof_of f (PBody {oracles, thms, proof}) = 244 PBody {oracles = oracles, thms = thms, proof = f proof}; 245 246fun thm_header serial pos theory_name name prop types : thm_header = 247 {serial = serial, pos = pos, theory_name = theory_name, name = name, prop = prop, types = types}; 248 249fun thm_body body = Thm_Body {open_proof = I, body = Future.value body}; 250fun thm_body_proof_raw (Thm_Body {body, ...}) = join_proof body; 251fun thm_body_proof_open (Thm_Body {open_proof, body, ...}) = open_proof (join_proof body); 252 253fun rep_thm_node (Thm_Node args) = args; 254val thm_node_theory_name = #theory_name o rep_thm_node; 255val thm_node_name = #name o rep_thm_node; 256val thm_node_prop = #prop o rep_thm_node; 257val thm_node_body = #body o rep_thm_node; 258val thm_node_thms = thm_node_body #> Future.join #> (fn PBody {thms, ...} => thms); 259val thm_node_export = #export o rep_thm_node; 260val thm_node_consolidate = #consolidate o rep_thm_node; 261 262fun join_thms (thms: thm list) = 263 Future.joins (map (thm_node_body o #2) thms); 264 265val consolidate_bodies = 266 maps (fn PBody {thms, ...} => map (thm_node_consolidate o #2) thms) 267 #> Lazy.consolidate #> map Lazy.force #> ignore; 268 269fun make_thm_node theory_name name prop body export = 270 let 271 val consolidate = 272 Lazy.lazy_name "Proofterm.make_thm_node" (fn () => 273 let val PBody {thms, ...} = Future.join body 274 in consolidate_bodies (join_thms thms) end); 275 in 276 Thm_Node {theory_name = theory_name, name = name, prop = prop, body = body, 277 export = export, consolidate = consolidate} 278 end; 279 280val no_export = Lazy.value (); 281 282fun make_thm ({serial, theory_name, name, prop, ...}: thm_header) (Thm_Body {body, ...}) = 283 (serial, make_thm_node theory_name name prop body no_export); 284 285 286(* proof atoms *) 287 288fun fold_proof_atoms all f = 289 let 290 fun app (Abst (_, _, prf)) = app prf 291 | app (AbsP (_, _, prf)) = app prf 292 | app (prf % _) = app prf 293 | app (prf1 %% prf2) = app prf1 #> app prf2 294 | app (prf as PThm ({serial = i, ...}, Thm_Body {body, ...})) = (fn (x, seen) => 295 if Inttab.defined seen i then (x, seen) 296 else 297 let val (x', seen') = 298 (if all then app (join_proof body) else I) (x, Inttab.update (i, ()) seen) 299 in (f prf x', seen') end) 300 | app prf = (fn (x, seen) => (f prf x, seen)); 301 in fn prfs => fn x => #1 (fold app prfs (x, Inttab.empty)) end; 302 303fun fold_body_thms f = 304 let 305 fun app (PBody {thms, ...}) = 306 tap join_thms thms |> fold (fn (i, thm_node) => fn (x, seen) => 307 if Inttab.defined seen i then (x, seen) 308 else 309 let 310 val name = thm_node_name thm_node; 311 val prop = thm_node_prop thm_node; 312 val body = Future.join (thm_node_body thm_node); 313 val (x', seen') = app body (x, Inttab.update (i, ()) seen); 314 in (f {serial = i, name = name, prop = prop, body = body} x', seen') end); 315 in fn bodies => fn x => #1 (fold app bodies (x, Inttab.empty)) end; 316 317 318(* proof body *) 319 320val unions_oracles = Ord_List.unions oracle_ord; 321val unions_thms = Ord_List.unions thm_ord; 322 323fun no_proof_body proof = PBody {oracles = [], thms = [], proof = proof}; 324val no_thm_body = thm_body (no_proof_body MinProof); 325 326fun no_thm_names (Abst (x, T, prf)) = Abst (x, T, no_thm_names prf) 327 | no_thm_names (AbsP (x, t, prf)) = AbsP (x, t, no_thm_names prf) 328 | no_thm_names (prf % t) = no_thm_names prf % t 329 | no_thm_names (prf1 %% prf2) = no_thm_names prf1 %% no_thm_names prf2 330 | no_thm_names (PThm ({serial, pos, theory_name, name = _, prop, types}, thm_body)) = 331 PThm (thm_header serial pos theory_name "" prop types, thm_body) 332 | no_thm_names a = a; 333 334fun no_thm_proofs (Abst (x, T, prf)) = Abst (x, T, no_thm_proofs prf) 335 | no_thm_proofs (AbsP (x, t, prf)) = AbsP (x, t, no_thm_proofs prf) 336 | no_thm_proofs (prf % t) = no_thm_proofs prf % t 337 | no_thm_proofs (prf1 %% prf2) = no_thm_proofs prf1 %% no_thm_proofs prf2 338 | no_thm_proofs (PThm (header, _)) = PThm (header, no_thm_body) 339 | no_thm_proofs a = a; 340 341fun no_body_proofs (Abst (x, T, prf)) = Abst (x, T, no_body_proofs prf) 342 | no_body_proofs (AbsP (x, t, prf)) = AbsP (x, t, no_body_proofs prf) 343 | no_body_proofs (prf % t) = no_body_proofs prf % t 344 | no_body_proofs (prf1 %% prf2) = no_body_proofs prf1 %% no_body_proofs prf2 345 | no_body_proofs (PThm (header, Thm_Body {open_proof, body})) = 346 let 347 val body' = Future.value (no_proof_body (join_proof body)); 348 val thm_body' = Thm_Body {open_proof = open_proof, body = body'}; 349 in PThm (header, thm_body') end 350 | no_body_proofs a = a; 351 352 353 354(** XML data representation **) 355 356(* encode *) 357 358local 359 360open XML.Encode Term_XML.Encode; 361 362fun proof consts prf = prf |> variant 363 [fn MinProof => ([], []), 364 fn PBound a => ([], int a), 365 fn Abst (a, b, c) => ([a], pair (option typ) (proof consts) (b, c)), 366 fn AbsP (a, b, c) => ([a], pair (option (term consts)) (proof consts) (b, c)), 367 fn a % b => ([], pair (proof consts) (option (term consts)) (a, b)), 368 fn a %% b => ([], pair (proof consts) (proof consts) (a, b)), 369 fn Hyp a => ([], term consts a), 370 fn PAxm (a, b, c) => ([a], pair (term consts) (option (list typ)) (b, c)), 371 fn OfClass (a, b) => ([b], typ a), 372 fn Oracle (a, b, c) => ([a], pair (term consts) (option (list typ)) (b, c)), 373 fn PThm ({serial, pos, theory_name, name, prop, types}, Thm_Body {open_proof, body, ...}) => 374 ([int_atom serial, theory_name, name], 375 pair (list properties) (pair (term consts) (pair (option (list typ)) (proof_body consts))) 376 (map Position.properties_of pos, (prop, (types, map_proof_of open_proof (Future.join body)))))] 377and proof_body consts (PBody {oracles, thms, proof = prf}) = 378 triple (list (pair (pair string (properties o Position.properties_of)) 379 (option (term consts)))) (list (thm consts)) (proof consts) (oracles, thms, prf) 380and thm consts (a, thm_node) = 381 pair int (pair string (pair string (pair (term consts) (proof_body consts)))) 382 (a, (thm_node_theory_name thm_node, (thm_node_name thm_node, (thm_node_prop thm_node, 383 (Future.join (thm_node_body thm_node)))))); 384 385fun standard_term consts t = t |> variant 386 [fn Const (a, b) => ([a], list typ (Consts.typargs consts (a, b))), 387 fn Free (a, _) => ([a], []), 388 fn Var (a, _) => (indexname a, []), 389 fn Bound a => ([], int a), 390 fn Abs (a, b, c) => ([a], pair typ (standard_term consts) (b, c)), 391 fn op $ a => ([], pair (standard_term consts) (standard_term consts) a)]; 392 393fun standard_proof consts prf = prf |> variant 394 [fn MinProof => ([], []), 395 fn PBound a => ([], int a), 396 fn Abst (a, SOME b, c) => ([a], pair typ (standard_proof consts) (b, c)), 397 fn AbsP (a, SOME b, c) => ([a], pair (standard_term consts) (standard_proof consts) (b, c)), 398 fn a % SOME b => ([], pair (standard_proof consts) (standard_term consts) (a, b)), 399 fn a %% b => ([], pair (standard_proof consts) (standard_proof consts) (a, b)), 400 fn Hyp a => ([], standard_term consts a), 401 fn PAxm (name, _, SOME Ts) => ([name], list typ Ts), 402 fn OfClass (T, c) => ([c], typ T), 403 fn Oracle (name, prop, SOME Ts) => ([name], pair (standard_term consts) (list typ) (prop, Ts)), 404 fn PThm ({serial, theory_name, name, types = SOME Ts, ...}, _) => 405 ([int_atom serial, theory_name, name], list typ Ts)]; 406 407in 408 409val encode = proof; 410val encode_body = proof_body; 411val encode_standard_term = standard_term; 412val encode_standard_proof = standard_proof; 413 414end; 415 416 417(* decode *) 418 419local 420 421open XML.Decode Term_XML.Decode; 422 423fun proof consts prf = prf |> variant 424 [fn ([], []) => MinProof, 425 fn ([], a) => PBound (int a), 426 fn ([a], b) => let val (c, d) = pair (option typ) (proof consts) b in Abst (a, c, d) end, 427 fn ([a], b) => let val (c, d) = pair (option (term consts)) (proof consts) b in AbsP (a, c, d) end, 428 fn ([], a) => op % (pair (proof consts) (option (term consts)) a), 429 fn ([], a) => op %% (pair (proof consts) (proof consts) a), 430 fn ([], a) => Hyp (term consts a), 431 fn ([a], b) => let val (c, d) = pair (term consts) (option (list typ)) b in PAxm (a, c, d) end, 432 fn ([b], a) => OfClass (typ a, b), 433 fn ([a], b) => let val (c, d) = pair (term consts) (option (list typ)) b in Oracle (a, c, d) end, 434 fn ([a, b, c], d) => 435 let 436 val ((e, (f, (g, h)))) = 437 pair (list properties) (pair (term consts) (pair (option (list typ)) (proof_body consts))) d; 438 val header = thm_header (int_atom a) (map Position.of_properties e) b c f g; 439 in PThm (header, thm_body h) end] 440and proof_body consts x = 441 let 442 val (a, b, c) = 443 triple (list (pair (pair string (Position.of_properties o properties)) 444 (option (term consts)))) (list (thm consts)) (proof consts) x; 445 in PBody {oracles = a, thms = b, proof = c} end 446and thm consts x = 447 let 448 val (a, (b, (c, (d, e)))) = 449 pair int (pair string (pair string (pair (term consts) (proof_body consts)))) x 450 in (a, make_thm_node b c d (Future.value e) no_export) end; 451 452in 453 454val decode = proof; 455val decode_body = proof_body; 456 457end; 458 459 460(** proof objects with different levels of detail **) 461 462val proofs = Unsynchronized.ref 2; 463fun proofs_enabled () = ! proofs >= 2; 464 465fun atomic_proof prf = 466 (case prf of 467 Abst _ => false 468 | AbsP _ => false 469 | op % _ => false 470 | op %% _ => false 471 | MinProof => false 472 | _ => true); 473 474fun compact_proof (prf % _) = compact_proof prf 475 | compact_proof (prf1 %% prf2) = atomic_proof prf2 andalso compact_proof prf1 476 | compact_proof prf = atomic_proof prf; 477 478fun (prf %> t) = prf % SOME t; 479 480val proof_combt = Library.foldl (op %>); 481val proof_combt' = Library.foldl (op %); 482val proof_combP = Library.foldl (op %%); 483 484fun strip_combt prf = 485 let fun stripc (prf % t, ts) = stripc (prf, t::ts) 486 | stripc x = x 487 in stripc (prf, []) end; 488 489fun strip_combP prf = 490 let fun stripc (prf %% prf', prfs) = stripc (prf, prf'::prfs) 491 | stripc x = x 492 in stripc (prf, []) end; 493 494fun strip_thm_body (body as PBody {proof, ...}) = 495 (case fst (strip_combt (fst (strip_combP proof))) of 496 PThm (_, Thm_Body {body = body', ...}) => Future.join body' 497 | _ => body); 498 499val mk_Abst = fold_rev (fn (x, _: typ) => fn prf => Abst (x, NONE, prf)); 500val mk_AbsP = fold_rev (fn _: term => fn prf => AbsP ("H", NONE, prf)); 501 502fun map_proof_same term typ ofclass = 503 let 504 val typs = Same.map typ; 505 506 fun proof (Abst (s, T, prf)) = 507 (Abst (s, Same.map_option typ T, Same.commit proof prf) 508 handle Same.SAME => Abst (s, T, proof prf)) 509 | proof (AbsP (s, t, prf)) = 510 (AbsP (s, Same.map_option term t, Same.commit proof prf) 511 handle Same.SAME => AbsP (s, t, proof prf)) 512 | proof (prf % t) = 513 (proof prf % Same.commit (Same.map_option term) t 514 handle Same.SAME => prf % Same.map_option term t) 515 | proof (prf1 %% prf2) = 516 (proof prf1 %% Same.commit proof prf2 517 handle Same.SAME => prf1 %% proof prf2) 518 | proof (PAxm (a, prop, SOME Ts)) = PAxm (a, prop, SOME (typs Ts)) 519 | proof (OfClass T_c) = ofclass T_c 520 | proof (Oracle (a, prop, SOME Ts)) = Oracle (a, prop, SOME (typs Ts)) 521 | proof (PThm ({serial, pos, theory_name, name, prop, types = SOME Ts}, thm_body)) = 522 PThm (thm_header serial pos theory_name name prop (SOME (typs Ts)), thm_body) 523 | proof _ = raise Same.SAME; 524 in proof end; 525 526fun map_proof_terms_same term typ = map_proof_same term typ (fn (T, c) => OfClass (typ T, c)); 527fun map_proof_types_same typ = map_proof_terms_same (Term_Subst.map_types_same typ) typ; 528 529fun same eq f x = 530 let val x' = f x 531 in if eq (x, x') then raise Same.SAME else x' end; 532 533fun map_proof_terms f g = Same.commit (map_proof_terms_same (same (op =) f) (same (op =) g)); 534fun map_proof_types f = Same.commit (map_proof_types_same (same (op =) f)); 535 536fun fold_proof_terms f (Abst (_, _, prf)) = fold_proof_terms f prf 537 | fold_proof_terms f (AbsP (_, SOME t, prf)) = f t #> fold_proof_terms f prf 538 | fold_proof_terms f (AbsP (_, NONE, prf)) = fold_proof_terms f prf 539 | fold_proof_terms f (prf % SOME t) = fold_proof_terms f prf #> f t 540 | fold_proof_terms f (prf % NONE) = fold_proof_terms f prf 541 | fold_proof_terms f (prf1 %% prf2) = fold_proof_terms f prf1 #> fold_proof_terms f prf2 542 | fold_proof_terms _ _ = I; 543 544fun fold_proof_terms_types f g (Abst (_, SOME T, prf)) = g T #> fold_proof_terms_types f g prf 545 | fold_proof_terms_types f g (Abst (_, NONE, prf)) = fold_proof_terms_types f g prf 546 | fold_proof_terms_types f g (AbsP (_, SOME t, prf)) = f t #> fold_proof_terms_types f g prf 547 | fold_proof_terms_types f g (AbsP (_, NONE, prf)) = fold_proof_terms_types f g prf 548 | fold_proof_terms_types f g (prf % SOME t) = fold_proof_terms_types f g prf #> f t 549 | fold_proof_terms_types f g (prf % NONE) = fold_proof_terms_types f g prf 550 | fold_proof_terms_types f g (prf1 %% prf2) = 551 fold_proof_terms_types f g prf1 #> fold_proof_terms_types f g prf2 552 | fold_proof_terms_types _ g (PAxm (_, _, SOME Ts)) = fold g Ts 553 | fold_proof_terms_types _ g (OfClass (T, _)) = g T 554 | fold_proof_terms_types _ g (Oracle (_, _, SOME Ts)) = fold g Ts 555 | fold_proof_terms_types _ g (PThm ({types = SOME Ts, ...}, _)) = fold g Ts 556 | fold_proof_terms_types _ _ _ = I; 557 558fun maxidx_proof prf = fold_proof_terms_types Term.maxidx_term Term.maxidx_typ prf; 559 560fun size_of_proof (Abst (_, _, prf)) = 1 + size_of_proof prf 561 | size_of_proof (AbsP (_, _, prf)) = 1 + size_of_proof prf 562 | size_of_proof (prf % _) = 1 + size_of_proof prf 563 | size_of_proof (prf1 %% prf2) = size_of_proof prf1 + size_of_proof prf2 564 | size_of_proof _ = 1; 565 566fun change_types types (PAxm (name, prop, _)) = PAxm (name, prop, types) 567 | change_types (SOME [T]) (OfClass (_, c)) = OfClass (T, c) 568 | change_types types (Oracle (name, prop, _)) = Oracle (name, prop, types) 569 | change_types types (PThm ({serial, pos, theory_name, name, prop, types = _}, thm_body)) = 570 PThm (thm_header serial pos theory_name name prop types, thm_body) 571 | change_types _ prf = prf; 572 573 574(* utilities *) 575 576fun strip_abs (_::Ts) (Abs (_, _, t)) = strip_abs Ts t 577 | strip_abs _ t = t; 578 579fun mk_abs Ts t = Library.foldl (fn (t', T) => Abs ("", T, t')) (t, Ts); 580 581 582(*Abstraction of a proof term over its occurrences of v, 583 which must contain no loose bound variables. 584 The resulting proof term is ready to become the body of an Abst.*) 585 586fun prf_abstract_over v = 587 let 588 fun abst' lev u = if v aconv u then Bound lev else 589 (case u of 590 Abs (a, T, t) => Abs (a, T, abst' (lev + 1) t) 591 | f $ t => (abst' lev f $ absth' lev t handle Same.SAME => f $ abst' lev t) 592 | _ => raise Same.SAME) 593 and absth' lev t = (abst' lev t handle Same.SAME => t); 594 595 fun abst lev (AbsP (a, t, prf)) = 596 (AbsP (a, Same.map_option (abst' lev) t, absth lev prf) 597 handle Same.SAME => AbsP (a, t, abst lev prf)) 598 | abst lev (Abst (a, T, prf)) = Abst (a, T, abst (lev + 1) prf) 599 | abst lev (prf1 %% prf2) = (abst lev prf1 %% absth lev prf2 600 handle Same.SAME => prf1 %% abst lev prf2) 601 | abst lev (prf % t) = (abst lev prf % Option.map (absth' lev) t 602 handle Same.SAME => prf % Same.map_option (abst' lev) t) 603 | abst _ _ = raise Same.SAME 604 and absth lev prf = (abst lev prf handle Same.SAME => prf); 605 606 in absth 0 end; 607 608 609(*increments a proof term's non-local bound variables 610 required when moving a proof term within abstractions 611 inc is increment for bound variables 612 lev is level at which a bound variable is considered 'loose'*) 613 614fun incr_bv' inct tlev t = incr_bv (inct, tlev, t); 615 616fun prf_incr_bv' incP _ Plev _ (PBound i) = 617 if i >= Plev then PBound (i+incP) else raise Same.SAME 618 | prf_incr_bv' incP inct Plev tlev (AbsP (a, t, body)) = 619 (AbsP (a, Same.map_option (same (op =) (incr_bv' inct tlev)) t, 620 prf_incr_bv incP inct (Plev+1) tlev body) handle Same.SAME => 621 AbsP (a, t, prf_incr_bv' incP inct (Plev+1) tlev body)) 622 | prf_incr_bv' incP inct Plev tlev (Abst (a, T, body)) = 623 Abst (a, T, prf_incr_bv' incP inct Plev (tlev+1) body) 624 | prf_incr_bv' incP inct Plev tlev (prf %% prf') = 625 (prf_incr_bv' incP inct Plev tlev prf %% prf_incr_bv incP inct Plev tlev prf' 626 handle Same.SAME => prf %% prf_incr_bv' incP inct Plev tlev prf') 627 | prf_incr_bv' incP inct Plev tlev (prf % t) = 628 (prf_incr_bv' incP inct Plev tlev prf % Option.map (incr_bv' inct tlev) t 629 handle Same.SAME => prf % Same.map_option (same (op =) (incr_bv' inct tlev)) t) 630 | prf_incr_bv' _ _ _ _ _ = raise Same.SAME 631and prf_incr_bv incP inct Plev tlev prf = 632 (prf_incr_bv' incP inct Plev tlev prf handle Same.SAME => prf); 633 634fun incr_pboundvars 0 0 prf = prf 635 | incr_pboundvars incP inct prf = prf_incr_bv incP inct 0 0 prf; 636 637 638fun prf_loose_bvar1 (prf1 %% prf2) k = prf_loose_bvar1 prf1 k orelse prf_loose_bvar1 prf2 k 639 | prf_loose_bvar1 (prf % SOME t) k = prf_loose_bvar1 prf k orelse loose_bvar1 (t, k) 640 | prf_loose_bvar1 (_ % NONE) _ = true 641 | prf_loose_bvar1 (AbsP (_, SOME t, prf)) k = loose_bvar1 (t, k) orelse prf_loose_bvar1 prf k 642 | prf_loose_bvar1 (AbsP (_, NONE, _)) _ = true 643 | prf_loose_bvar1 (Abst (_, _, prf)) k = prf_loose_bvar1 prf (k+1) 644 | prf_loose_bvar1 _ _ = false; 645 646fun prf_loose_Pbvar1 (PBound i) k = i = k 647 | prf_loose_Pbvar1 (prf1 %% prf2) k = prf_loose_Pbvar1 prf1 k orelse prf_loose_Pbvar1 prf2 k 648 | prf_loose_Pbvar1 (prf % _) k = prf_loose_Pbvar1 prf k 649 | prf_loose_Pbvar1 (AbsP (_, _, prf)) k = prf_loose_Pbvar1 prf (k+1) 650 | prf_loose_Pbvar1 (Abst (_, _, prf)) k = prf_loose_Pbvar1 prf k 651 | prf_loose_Pbvar1 _ _ = false; 652 653fun prf_add_loose_bnos plev _ (PBound i) (is, js) = 654 if i < plev then (is, js) else (insert (op =) (i-plev) is, js) 655 | prf_add_loose_bnos plev tlev (prf1 %% prf2) p = 656 prf_add_loose_bnos plev tlev prf2 657 (prf_add_loose_bnos plev tlev prf1 p) 658 | prf_add_loose_bnos plev tlev (prf % opt) (is, js) = 659 prf_add_loose_bnos plev tlev prf 660 (case opt of 661 NONE => (is, insert (op =) ~1 js) 662 | SOME t => (is, add_loose_bnos (t, tlev, js))) 663 | prf_add_loose_bnos plev tlev (AbsP (_, opt, prf)) (is, js) = 664 prf_add_loose_bnos (plev+1) tlev prf 665 (case opt of 666 NONE => (is, insert (op =) ~1 js) 667 | SOME t => (is, add_loose_bnos (t, tlev, js))) 668 | prf_add_loose_bnos plev tlev (Abst (_, _, prf)) p = 669 prf_add_loose_bnos plev (tlev+1) prf p 670 | prf_add_loose_bnos _ _ _ _ = ([], []); 671 672 673(* substitutions *) 674 675fun del_conflicting_tvars envT T = Term_Subst.instantiateT 676 (map_filter (fn ixnS as (_, S) => 677 (Type.lookup envT ixnS; NONE) handle TYPE _ => 678 SOME (ixnS, Logic.dummy_tfree S)) (Term.add_tvarsT T [])) T; 679 680fun del_conflicting_vars env t = Term_Subst.instantiate 681 (map_filter (fn ixnS as (_, S) => 682 (Type.lookup (Envir.type_env env) ixnS; NONE) handle TYPE _ => 683 SOME (ixnS, Logic.dummy_tfree S)) (Term.add_tvars t []), 684 map_filter (fn (ixnT as (_, T)) => 685 (Envir.lookup env ixnT; NONE) handle TYPE _ => 686 SOME (ixnT, Free ("dummy", T))) (Term.add_vars t [])) t; 687 688fun norm_proof env = 689 let 690 val envT = Envir.type_env env; 691 fun msg s = warning ("type conflict in norm_proof:\n" ^ s); 692 fun htype f t = f env t handle TYPE (s, _, _) => 693 (msg s; f env (del_conflicting_vars env t)); 694 fun htypeT f T = f envT T handle TYPE (s, _, _) => 695 (msg s; f envT (del_conflicting_tvars envT T)); 696 fun htypeTs f Ts = f envT Ts handle TYPE (s, _, _) => 697 (msg s; f envT (map (del_conflicting_tvars envT) Ts)); 698 699 fun norm (Abst (s, T, prf)) = 700 (Abst (s, Same.map_option (htypeT Envir.norm_type_same) T, Same.commit norm prf) 701 handle Same.SAME => Abst (s, T, norm prf)) 702 | norm (AbsP (s, t, prf)) = 703 (AbsP (s, Same.map_option (htype Envir.norm_term_same) t, Same.commit norm prf) 704 handle Same.SAME => AbsP (s, t, norm prf)) 705 | norm (prf % t) = 706 (norm prf % Option.map (htype Envir.norm_term) t 707 handle Same.SAME => prf % Same.map_option (htype Envir.norm_term_same) t) 708 | norm (prf1 %% prf2) = 709 (norm prf1 %% Same.commit norm prf2 710 handle Same.SAME => prf1 %% norm prf2) 711 | norm (PAxm (s, prop, Ts)) = 712 PAxm (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts) 713 | norm (OfClass (T, c)) = 714 OfClass (htypeT Envir.norm_type_same T, c) 715 | norm (Oracle (s, prop, Ts)) = 716 Oracle (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts) 717 | norm (PThm ({serial = i, pos = p, theory_name, name = a, prop = t, types = Ts}, thm_body)) = 718 PThm (thm_header i p theory_name a t 719 (Same.map_option (htypeTs Envir.norm_types_same) Ts), thm_body) 720 | norm _ = raise Same.SAME; 721 in Same.commit norm end; 722 723 724(* remove some types in proof term (to save space) *) 725 726fun remove_types (Abs (s, _, t)) = Abs (s, dummyT, remove_types t) 727 | remove_types (t $ u) = remove_types t $ remove_types u 728 | remove_types (Const (s, _)) = Const (s, dummyT) 729 | remove_types t = t; 730 731fun remove_types_env (Envir.Envir {maxidx, tenv, tyenv}) = 732 Envir.Envir {maxidx = maxidx, tenv = Vartab.map (K (apsnd remove_types)) tenv, tyenv = tyenv}; 733 734fun norm_proof' env prf = norm_proof (remove_types_env env) prf; 735 736 737(* substitution of bound variables *) 738 739fun prf_subst_bounds args prf = 740 let 741 val n = length args; 742 fun subst' lev (Bound i) = 743 (if i<lev then raise Same.SAME (*var is locally bound*) 744 else incr_boundvars lev (nth args (i-lev)) 745 handle General.Subscript => Bound (i-n)) (*loose: change it*) 746 | subst' lev (Abs (a, T, body)) = Abs (a, T, subst' (lev+1) body) 747 | subst' lev (f $ t) = (subst' lev f $ substh' lev t 748 handle Same.SAME => f $ subst' lev t) 749 | subst' _ _ = raise Same.SAME 750 and substh' lev t = (subst' lev t handle Same.SAME => t); 751 752 fun subst lev (AbsP (a, t, body)) = 753 (AbsP (a, Same.map_option (subst' lev) t, substh lev body) 754 handle Same.SAME => AbsP (a, t, subst lev body)) 755 | subst lev (Abst (a, T, body)) = Abst (a, T, subst (lev+1) body) 756 | subst lev (prf %% prf') = (subst lev prf %% substh lev prf' 757 handle Same.SAME => prf %% subst lev prf') 758 | subst lev (prf % t) = (subst lev prf % Option.map (substh' lev) t 759 handle Same.SAME => prf % Same.map_option (subst' lev) t) 760 | subst _ _ = raise Same.SAME 761 and substh lev prf = (subst lev prf handle Same.SAME => prf); 762 in (case args of [] => prf | _ => substh 0 prf) end; 763 764fun prf_subst_pbounds args prf = 765 let 766 val n = length args; 767 fun subst (PBound i) Plev tlev = 768 (if i < Plev then raise Same.SAME (*var is locally bound*) 769 else incr_pboundvars Plev tlev (nth args (i-Plev)) 770 handle General.Subscript => PBound (i-n) (*loose: change it*)) 771 | subst (AbsP (a, t, body)) Plev tlev = AbsP (a, t, subst body (Plev+1) tlev) 772 | subst (Abst (a, T, body)) Plev tlev = Abst (a, T, subst body Plev (tlev+1)) 773 | subst (prf %% prf') Plev tlev = (subst prf Plev tlev %% substh prf' Plev tlev 774 handle Same.SAME => prf %% subst prf' Plev tlev) 775 | subst (prf % t) Plev tlev = subst prf Plev tlev % t 776 | subst _ _ _ = raise Same.SAME 777 and substh prf Plev tlev = (subst prf Plev tlev handle Same.SAME => prf) 778 in (case args of [] => prf | _ => substh prf 0 0) end; 779 780 781(* freezing and thawing of variables in proof terms *) 782 783local 784 785fun frzT names = 786 map_type_tvar (fn (ixn, S) => TFree (the (AList.lookup (op =) names ixn), S)); 787 788fun thawT names = 789 map_type_tfree (fn (a, S) => 790 (case AList.lookup (op =) names a of 791 NONE => TFree (a, S) 792 | SOME ixn => TVar (ixn, S))); 793 794fun freeze names names' (t $ u) = 795 freeze names names' t $ freeze names names' u 796 | freeze names names' (Abs (s, T, t)) = 797 Abs (s, frzT names' T, freeze names names' t) 798 | freeze _ names' (Const (s, T)) = Const (s, frzT names' T) 799 | freeze _ names' (Free (s, T)) = Free (s, frzT names' T) 800 | freeze names names' (Var (ixn, T)) = 801 Free (the (AList.lookup (op =) names ixn), frzT names' T) 802 | freeze _ _ t = t; 803 804fun thaw names names' (t $ u) = 805 thaw names names' t $ thaw names names' u 806 | thaw names names' (Abs (s, T, t)) = 807 Abs (s, thawT names' T, thaw names names' t) 808 | thaw _ names' (Const (s, T)) = Const (s, thawT names' T) 809 | thaw names names' (Free (s, T)) = 810 let val T' = thawT names' T in 811 (case AList.lookup (op =) names s of 812 NONE => Free (s, T') 813 | SOME ixn => Var (ixn, T')) 814 end 815 | thaw _ names' (Var (ixn, T)) = Var (ixn, thawT names' T) 816 | thaw _ _ t = t; 817 818in 819 820fun freeze_thaw_prf prf = 821 let 822 val (fs, Tfs, vs, Tvs) = fold_proof_terms_types 823 (fn t => fn (fs, Tfs, vs, Tvs) => 824 (Term.add_free_names t fs, Term.add_tfree_names t Tfs, 825 Term.add_var_names t vs, Term.add_tvar_names t Tvs)) 826 (fn T => fn (fs, Tfs, vs, Tvs) => 827 (fs, Term.add_tfree_namesT T Tfs, 828 vs, Term.add_tvar_namesT T Tvs)) 829 prf ([], [], [], []); 830 val names = vs ~~ Name.variant_list fs (map fst vs); 831 val names' = Tvs ~~ Name.variant_list Tfs (map fst Tvs); 832 val rnames = map swap names; 833 val rnames' = map swap names'; 834 in 835 (map_proof_terms (freeze names names') (frzT names') prf, 836 map_proof_terms (thaw rnames rnames') (thawT rnames')) 837 end; 838 839end; 840 841 842 843(** inference rules **) 844 845(* trivial implication *) 846 847val trivial_proof = AbsP ("H", NONE, PBound 0); 848 849 850(* implication introduction *) 851 852fun gen_implies_intr_proof f h prf = 853 let 854 fun abshyp i (Hyp t) = if h aconv t then PBound i else raise Same.SAME 855 | abshyp i (Abst (s, T, prf)) = Abst (s, T, abshyp i prf) 856 | abshyp i (AbsP (s, t, prf)) = AbsP (s, t, abshyp (i + 1) prf) 857 | abshyp i (prf % t) = abshyp i prf % t 858 | abshyp i (prf1 %% prf2) = 859 (abshyp i prf1 %% abshyph i prf2 860 handle Same.SAME => prf1 %% abshyp i prf2) 861 | abshyp _ _ = raise Same.SAME 862 and abshyph i prf = (abshyp i prf handle Same.SAME => prf); 863 in 864 AbsP ("H", f h, abshyph 0 prf) 865 end; 866 867val implies_intr_proof = gen_implies_intr_proof (K NONE); 868val implies_intr_proof' = gen_implies_intr_proof SOME; 869 870 871(* forall introduction *) 872 873fun forall_intr_proof (a, v) opt_T prf = Abst (a, opt_T, prf_abstract_over v prf); 874 875fun forall_intr_proof' v prf = 876 let val (a, T) = (case v of Var ((a, _), T) => (a, T) | Free (a, T) => (a, T)) 877 in forall_intr_proof (a, v) (SOME T) prf end; 878 879 880(* varify *) 881 882fun varify_proof t fixed prf = 883 let 884 val fs = Term.fold_types (Term.fold_atyps 885 (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t []; 886 val used = Name.context 887 |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t; 888 val fmap = fs ~~ #1 (fold_map Name.variant (map fst fs) used); 889 fun thaw (a, S) = 890 (case AList.lookup (op =) fmap (a, S) of 891 NONE => TFree (a, S) 892 | SOME b => TVar ((b, 0), S)); 893 in map_proof_terms (map_types (map_type_tfree thaw)) (map_type_tfree thaw) prf end; 894 895 896local 897 898fun new_name ix (pairs, used) = 899 let val v = singleton (Name.variant_list used) (string_of_indexname ix) 900 in ((ix, v) :: pairs, v :: used) end; 901 902fun freeze_one alist (ix, sort) = 903 (case AList.lookup (op =) alist ix of 904 NONE => TVar (ix, sort) 905 | SOME name => TFree (name, sort)); 906 907in 908 909fun legacy_freezeT t prf = 910 let 911 val used = Term.add_tfree_names t []; 912 val (alist, _) = fold_rev new_name (map #1 (Term.add_tvars t [])) ([], used); 913 in 914 (case alist of 915 [] => prf (*nothing to do!*) 916 | _ => 917 let val frzT = map_type_tvar (freeze_one alist) 918 in map_proof_terms (map_types frzT) frzT prf end) 919 end; 920 921end; 922 923 924(* rotate assumptions *) 925 926fun rotate_proof Bs Bi' params asms m prf = 927 let 928 val i = length asms; 929 val j = length Bs; 930 in 931 mk_AbsP (Bs @ [Bi']) (proof_combP (prf, map PBound 932 (j downto 1) @ [mk_Abst params (mk_AbsP asms 933 (proof_combP (proof_combt (PBound i, map Bound ((length params - 1) downto 0)), 934 map PBound (((i-m-1) downto 0) @ ((i-1) downto (i-m))))))])) 935 end; 936 937 938(* permute premises *) 939 940fun permute_prems_proof prems' j k prf = 941 let val n = length prems' in 942 mk_AbsP prems' 943 (proof_combP (prf, map PBound ((n-1 downto n-j) @ (k-1 downto 0) @ (n-j-1 downto k)))) 944 end; 945 946 947(* generalization *) 948 949fun generalize_proof (tfrees, frees) idx prop prf = 950 let 951 val gen = 952 if null frees then [] 953 else 954 fold_aterms (fn Free (x, T) => member (op =) frees x ? insert (op =) (x, T) | _ => I) 955 (Term_Subst.generalize (tfrees, []) idx prop) []; 956 in 957 prf 958 |> Same.commit (map_proof_terms_same 959 (Term_Subst.generalize_same (tfrees, []) idx) 960 (Term_Subst.generalizeT_same tfrees idx)) 961 |> fold (fn (x, T) => forall_intr_proof (x, Free (x, T)) NONE) gen 962 |> fold_rev (fn (x, T) => fn prf' => prf' %> Var (Name.clean_index (x, idx), T)) gen 963 end; 964 965 966(* instantiation *) 967 968fun instantiate (instT, inst) = 969 Same.commit (map_proof_terms_same 970 (Term_Subst.instantiate_same (instT, map (apsnd remove_types) inst)) 971 (Term_Subst.instantiateT_same instT)); 972 973 974(* lifting *) 975 976fun lift_proof Bi inc prop prf = 977 let 978 fun lift'' Us Ts t = 979 strip_abs Ts (Logic.incr_indexes ([], Us, inc) (mk_abs Ts t)); 980 981 fun lift' Us Ts (Abst (s, T, prf)) = 982 (Abst (s, Same.map_option (Logic.incr_tvar_same inc) T, lifth' Us (dummyT::Ts) prf) 983 handle Same.SAME => Abst (s, T, lift' Us (dummyT::Ts) prf)) 984 | lift' Us Ts (AbsP (s, t, prf)) = 985 (AbsP (s, Same.map_option (same (op =) (lift'' Us Ts)) t, lifth' Us Ts prf) 986 handle Same.SAME => AbsP (s, t, lift' Us Ts prf)) 987 | lift' Us Ts (prf % t) = (lift' Us Ts prf % Option.map (lift'' Us Ts) t 988 handle Same.SAME => prf % Same.map_option (same (op =) (lift'' Us Ts)) t) 989 | lift' Us Ts (prf1 %% prf2) = (lift' Us Ts prf1 %% lifth' Us Ts prf2 990 handle Same.SAME => prf1 %% lift' Us Ts prf2) 991 | lift' _ _ (PAxm (s, prop, Ts)) = 992 PAxm (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts) 993 | lift' _ _ (OfClass (T, c)) = 994 OfClass (Logic.incr_tvar_same inc T, c) 995 | lift' _ _ (Oracle (s, prop, Ts)) = 996 Oracle (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts) 997 | lift' _ _ (PThm ({serial = i, pos = p, theory_name, name = s, prop, types = Ts}, thm_body)) = 998 PThm (thm_header i p theory_name s prop 999 ((Same.map_option o Same.map) (Logic.incr_tvar inc) Ts), thm_body) 1000 | lift' _ _ _ = raise Same.SAME 1001 and lifth' Us Ts prf = (lift' Us Ts prf handle Same.SAME => prf); 1002 1003 val ps = map (Logic.lift_all inc Bi) (Logic.strip_imp_prems prop); 1004 val k = length ps; 1005 1006 fun mk_app b (i, j, prf) = 1007 if b then (i-1, j, prf %% PBound i) else (i, j-1, prf %> Bound j); 1008 1009 fun lift Us bs i j (Const ("Pure.imp", _) $ A $ B) = 1010 AbsP ("H", NONE (*A*), lift Us (true::bs) (i+1) j B) 1011 | lift Us bs i j (Const ("Pure.all", _) $ Abs (a, T, t)) = 1012 Abst (a, NONE (*T*), lift (T::Us) (false::bs) i (j+1) t) 1013 | lift Us bs i j _ = proof_combP (lifth' (rev Us) [] prf, 1014 map (fn k => (#3 (fold_rev mk_app bs (i-1, j-1, PBound k)))) 1015 (i + k - 1 downto i)); 1016 in 1017 mk_AbsP ps (lift [] [] 0 0 Bi) 1018 end; 1019 1020fun incr_indexes i = 1021 Same.commit (map_proof_terms_same 1022 (Logic.incr_indexes_same ([], [], i)) (Logic.incr_tvar_same i)); 1023 1024 1025(* proof by assumption *) 1026 1027fun mk_asm_prf t i m = 1028 let 1029 fun imp_prf _ i 0 = PBound i 1030 | imp_prf (Const ("Pure.imp", _) $ A $ B) i m = AbsP ("H", NONE (*A*), imp_prf B (i+1) (m-1)) 1031 | imp_prf _ i _ = PBound i; 1032 fun all_prf (Const ("Pure.all", _) $ Abs (a, T, t)) = Abst (a, NONE (*T*), all_prf t) 1033 | all_prf t = imp_prf t (~i) m 1034 in all_prf t end; 1035 1036fun assumption_proof Bs Bi n prf = 1037 mk_AbsP Bs (proof_combP (prf, map PBound (length Bs - 1 downto 0) @ [mk_asm_prf Bi n ~1])); 1038 1039 1040(* composition of object rule with proof state *) 1041 1042fun flatten_params_proof i j n (Const ("Pure.imp", _) $ A $ B, k) = 1043 AbsP ("H", NONE (*A*), flatten_params_proof (i+1) j n (B, k)) 1044 | flatten_params_proof i j n (Const ("Pure.all", _) $ Abs (a, T, t), k) = 1045 Abst (a, NONE (*T*), flatten_params_proof i (j+1) n (t, k)) 1046 | flatten_params_proof i j n (_, k) = proof_combP (proof_combt (PBound (k+i), 1047 map Bound (j-1 downto 0)), map PBound (remove (op =) (i-n) (i-1 downto 0))); 1048 1049fun bicompose_proof flatten Bs As A oldAs n m rprf sprf = 1050 let 1051 val lb = length Bs; 1052 val la = length As; 1053 in 1054 mk_AbsP (Bs @ As) (proof_combP (sprf, 1055 map PBound (lb + la - 1 downto la)) %% 1056 proof_combP (rprf, (if n>0 then [mk_asm_prf (the A) n m] else []) @ 1057 map (if flatten then flatten_params_proof 0 0 n else PBound o snd) 1058 (oldAs ~~ (la - 1 downto 0)))) 1059 end; 1060 1061 1062 1063(** type classes **) 1064 1065fun strip_shyps_proof algebra present witnessed extra prf = 1066 let 1067 val replacements = present @ witnessed @ map (`Logic.dummy_tfree) extra; 1068 fun get_replacement S = 1069 replacements |> get_first (fn (T', S') => 1070 if Sorts.sort_le algebra (S', S) then SOME T' else NONE); 1071 fun replace T = 1072 if exists (fn (T', _) => T' = T) present then raise Same.SAME 1073 else 1074 (case get_replacement (Type.sort_of_atyp T) of 1075 SOME T' => T' 1076 | NONE => raise Fail "strip_shyps_proof: bad type variable in proof term"); 1077 in Same.commit (map_proof_types_same (Term_Subst.map_atypsT_same replace)) prf end; 1078 1079fun of_sort_proof algebra classrel_proof arity_proof hyps = 1080 Sorts.of_sort_derivation algebra 1081 {class_relation = fn _ => fn _ => fn (prf, c1) => fn c2 => 1082 if c1 = c2 then prf else classrel_proof (c1, c2) %% prf, 1083 type_constructor = fn (a, _) => fn dom => fn c => 1084 let val Ss = map (map snd) dom and prfs = maps (map fst) dom 1085 in proof_combP (arity_proof (a, Ss, c), prfs) end, 1086 type_variable = fn typ => map (fn c => (hyps (typ, c), c)) (Type.sort_of_atyp typ)}; 1087 1088 1089 1090(** axioms and theorems **) 1091 1092val add_type_variables = (fold_types o fold_atyps) (insert (op =)); 1093fun type_variables_of t = rev (add_type_variables t []); 1094 1095val add_variables = fold_aterms (fn a => (is_Var a orelse is_Free a) ? insert (op =) a); 1096fun variables_of t = rev (add_variables t []); 1097 1098fun test_args _ [] = true 1099 | test_args is (Bound i :: ts) = 1100 not (member (op =) is i) andalso test_args (i :: is) ts 1101 | test_args _ _ = false; 1102 1103fun is_fun (Type ("fun", _)) = true 1104 | is_fun (TVar _) = true 1105 | is_fun _ = false; 1106 1107fun vars_of t = map Var (rev (Term.add_vars t [])); 1108 1109fun add_funvars Ts (vs, t) = 1110 if is_fun (fastype_of1 (Ts, t)) then 1111 union (op =) vs (map_filter (fn Var (ixn, T) => 1112 if is_fun T then SOME ixn else NONE | _ => NONE) (vars_of t)) 1113 else vs; 1114 1115fun add_npvars q p Ts (vs, Const ("Pure.imp", _) $ t $ u) = 1116 add_npvars q p Ts (add_npvars q (not p) Ts (vs, t), u) 1117 | add_npvars q p Ts (vs, Const ("Pure.all", Type (_, [Type (_, [T, _]), _])) $ t) = 1118 add_npvars q p Ts (vs, if p andalso q then betapply (t, Var (("",0), T)) else t) 1119 | add_npvars q p Ts (vs, Abs (_, T, t)) = add_npvars q p (T::Ts) (vs, t) 1120 | add_npvars _ _ Ts (vs, t) = add_npvars' Ts (vs, t) 1121and add_npvars' Ts (vs, t) = 1122 (case strip_comb t of 1123 (Var (ixn, _), ts) => if test_args [] ts then vs 1124 else Library.foldl (add_npvars' Ts) 1125 (AList.update (op =) (ixn, 1126 Library.foldl (add_funvars Ts) ((these ooo AList.lookup) (op =) vs ixn, ts)) vs, ts) 1127 | (Abs (_, T, u), ts) => Library.foldl (add_npvars' (T::Ts)) (vs, u :: ts) 1128 | (_, ts) => Library.foldl (add_npvars' Ts) (vs, ts)); 1129 1130fun prop_vars (Const ("Pure.imp", _) $ P $ Q) = union (op =) (prop_vars P) (prop_vars Q) 1131 | prop_vars (Const ("Pure.all", _) $ Abs (_, _, t)) = prop_vars t 1132 | prop_vars t = (case strip_comb t of (Var (ixn, _), _) => [ixn] | _ => []); 1133 1134fun is_proj t = 1135 let 1136 fun is_p i t = 1137 (case strip_comb t of 1138 (Bound _, []) => false 1139 | (Bound j, ts) => j >= i orelse exists (is_p i) ts 1140 | (Abs (_, _, u), _) => is_p (i+1) u 1141 | (_, ts) => exists (is_p i) ts) 1142 in 1143 (case strip_abs_body t of 1144 Bound _ => true 1145 | t' => is_p 0 t') 1146 end; 1147 1148fun prop_args prop = 1149 let 1150 val needed_vars = 1151 union (op =) (Library.foldl (uncurry (union (op =))) 1152 ([], map (uncurry (insert (op =))) (add_npvars true true [] ([], prop)))) 1153 (prop_vars prop); 1154 in 1155 variables_of prop |> map 1156 (fn var as Var (ixn, _) => if member (op =) needed_vars ixn then SOME var else NONE 1157 | free => SOME free) 1158 end; 1159 1160fun const_proof mk name prop = 1161 let 1162 val args = prop_args prop; 1163 val ({outer_constraints, ...}, prop1) = Logic.unconstrainT [] prop; 1164 val head = mk (name, prop1, NONE); 1165 in proof_combP (proof_combt' (head, args), map OfClass outer_constraints) end; 1166 1167val axm_proof = const_proof PAxm; 1168val oracle_proof = const_proof Oracle; 1169 1170val shrink_proof = 1171 let 1172 fun shrink ls lev (prf as Abst (a, T, body)) = 1173 let val (b, is, ch, body') = shrink ls (lev+1) body 1174 in (b, is, ch, if ch then Abst (a, T, body') else prf) end 1175 | shrink ls lev (prf as AbsP (a, t, body)) = 1176 let val (b, is, ch, body') = shrink (lev::ls) lev body 1177 in (b orelse member (op =) is 0, map_filter (fn 0 => NONE | i => SOME (i-1)) is, 1178 ch, if ch then AbsP (a, t, body') else prf) 1179 end 1180 | shrink ls lev prf = 1181 let val (is, ch, _, prf') = shrink' ls lev [] [] prf 1182 in (false, is, ch, prf') end 1183 and shrink' ls lev ts prfs (prf as prf1 %% prf2) = 1184 let 1185 val p as (_, is', ch', prf') = shrink ls lev prf2; 1186 val (is, ch, ts', prf'') = shrink' ls lev ts (p::prfs) prf1 1187 in (union (op =) is is', ch orelse ch', ts', 1188 if ch orelse ch' then prf'' %% prf' else prf) 1189 end 1190 | shrink' ls lev ts prfs (prf as prf1 % t) = 1191 let val (is, ch, (ch', t')::ts', prf') = shrink' ls lev (t::ts) prfs prf1 1192 in (is, ch orelse ch', ts', 1193 if ch orelse ch' then prf' % t' else prf) end 1194 | shrink' ls lev ts prfs (prf as PBound i) = 1195 (if exists (fn SOME (Bound j) => lev-j <= nth ls i | _ => true) ts 1196 orelse has_duplicates (op =) 1197 (Library.foldl (fn (js, SOME (Bound j)) => j :: js | (js, _) => js) ([], ts)) 1198 orelse exists #1 prfs then [i] else [], false, map (pair false) ts, prf) 1199 | shrink' _ _ ts _ (Hyp t) = ([], false, map (pair false) ts, Hyp t) 1200 | shrink' _ _ ts _ (prf as MinProof) = ([], false, map (pair false) ts, prf) 1201 | shrink' _ _ ts _ (prf as OfClass _) = ([], false, map (pair false) ts, prf) 1202 | shrink' _ _ ts prfs prf = 1203 let 1204 val prop = 1205 (case prf of 1206 PAxm (_, prop, _) => prop 1207 | Oracle (_, prop, _) => prop 1208 | PThm ({prop, ...}, _) => prop 1209 | _ => raise Fail "shrink: proof not in normal form"); 1210 val vs = vars_of prop; 1211 val (ts', ts'') = chop (length vs) ts; 1212 val insts = take (length ts') (map (fst o dest_Var) vs) ~~ ts'; 1213 val nvs = Library.foldl (fn (ixns', (ixn, ixns)) => 1214 insert (op =) ixn 1215 (case AList.lookup (op =) insts ixn of 1216 SOME (SOME t) => if is_proj t then union (op =) ixns ixns' else ixns' 1217 | _ => union (op =) ixns ixns')) 1218 (needed prop ts'' prfs, add_npvars false true [] ([], prop)); 1219 val insts' = map 1220 (fn (ixn, x as SOME _) => if member (op =) nvs ixn then (false, x) else (true, NONE) 1221 | (_, x) => (false, x)) insts 1222 in ([], false, insts' @ map (pair false) ts'', prf) end 1223 and needed (Const ("Pure.imp", _) $ t $ u) ts ((b, _, _, _)::prfs) = 1224 union (op =) (if b then map (fst o dest_Var) (vars_of t) else []) (needed u ts prfs) 1225 | needed (Var (ixn, _)) (_::_) _ = [ixn] 1226 | needed _ _ _ = []; 1227 in fn prf => #4 (shrink [] 0 prf) end; 1228 1229 1230 1231(** axioms for equality **) 1232 1233val aT = TFree ("'a", []); 1234val bT = TFree ("'b", []); 1235val x = Free ("x", aT); 1236val y = Free ("y", aT); 1237val z = Free ("z", aT); 1238val A = Free ("A", propT); 1239val B = Free ("B", propT); 1240val f = Free ("f", aT --> bT); 1241val g = Free ("g", aT --> bT); 1242 1243val equality_axms = 1244 [("reflexive", Logic.mk_equals (x, x)), 1245 ("symmetric", Logic.mk_implies (Logic.mk_equals (x, y), Logic.mk_equals (y, x))), 1246 ("transitive", 1247 Logic.list_implies ([Logic.mk_equals (x, y), Logic.mk_equals (y, z)], Logic.mk_equals (x, z))), 1248 ("equal_intr", 1249 Logic.list_implies ([Logic.mk_implies (A, B), Logic.mk_implies (B, A)], Logic.mk_equals (A, B))), 1250 ("equal_elim", Logic.list_implies ([Logic.mk_equals (A, B), A], B)), 1251 ("abstract_rule", 1252 Logic.mk_implies 1253 (Logic.all x (Logic.mk_equals (f $ x, g $ x)), 1254 Logic.mk_equals (lambda x (f $ x), lambda x (g $ x)))), 1255 ("combination", Logic.list_implies 1256 ([Logic.mk_equals (f, g), Logic.mk_equals (x, y)], Logic.mk_equals (f $ x, g $ y)))]; 1257 1258val [reflexive_axm, symmetric_axm, transitive_axm, equal_intr_axm, 1259 equal_elim_axm, abstract_rule_axm, combination_axm] = 1260 map (fn (s, t) => PAxm ("Pure." ^ s, Logic.varify_global t, NONE)) equality_axms; 1261 1262val reflexive_proof = reflexive_axm % NONE; 1263 1264val is_reflexive_proof = fn PAxm ("Pure.reflexive", _, _) % _ => true | _ => false; 1265 1266fun symmetric_proof prf = 1267 if is_reflexive_proof prf then prf 1268 else symmetric_axm % NONE % NONE %% prf; 1269 1270fun transitive_proof U u prf1 prf2 = 1271 if is_reflexive_proof prf1 then prf2 1272 else if is_reflexive_proof prf2 then prf1 1273 else if U = propT then transitive_axm % NONE % SOME (remove_types u) % NONE %% prf1 %% prf2 1274 else transitive_axm % NONE % NONE % NONE %% prf1 %% prf2; 1275 1276fun equal_intr_proof A B prf1 prf2 = 1277 equal_intr_axm %> remove_types A %> remove_types B %% prf1 %% prf2; 1278 1279fun equal_elim_proof A B prf1 prf2 = 1280 equal_elim_axm %> remove_types A %> remove_types B %% prf1 %% prf2; 1281 1282fun abstract_rule_proof (a, x) prf = 1283 abstract_rule_axm % NONE % NONE %% forall_intr_proof (a, x) NONE prf; 1284 1285fun check_comb (PAxm ("Pure.combination", _, _) % f % _ % _ % _ %% prf %% _) = 1286 is_some f orelse check_comb prf 1287 | check_comb (PAxm ("Pure.transitive", _, _) % _ % _ % _ %% prf1 %% prf2) = 1288 check_comb prf1 andalso check_comb prf2 1289 | check_comb (PAxm ("Pure.symmetric", _, _) % _ % _ %% prf) = check_comb prf 1290 | check_comb _ = false; 1291 1292fun combination_proof f g t u prf1 prf2 = 1293 let 1294 val f = Envir.beta_norm f; 1295 val g = Envir.beta_norm g; 1296 val prf = 1297 if check_comb prf1 then 1298 combination_axm % NONE % NONE 1299 else 1300 (case prf1 of 1301 PAxm ("Pure.reflexive", _, _) % _ => 1302 combination_axm %> remove_types f % NONE 1303 | _ => combination_axm %> remove_types f %> remove_types g) 1304 in 1305 prf % 1306 (case head_of f of 1307 Abs _ => SOME (remove_types t) 1308 | Var _ => SOME (remove_types t) 1309 | _ => NONE) % 1310 (case head_of g of 1311 Abs _ => SOME (remove_types u) 1312 | Var _ => SOME (remove_types u) 1313 | _ => NONE) %% prf1 %% prf2 1314 end; 1315 1316 1317 1318(** rewriting on proof terms **) 1319 1320(* simple first order matching functions for terms and proofs (see pattern.ML) *) 1321 1322exception PMatch; 1323 1324fun flt (i: int) = filter (fn n => n < i); 1325 1326fun fomatch Ts tymatch j instsp p = 1327 let 1328 fun mtch (instsp as (tyinsts, insts)) = fn 1329 (Var (ixn, T), t) => 1330 if j>0 andalso not (null (flt j (loose_bnos t))) 1331 then raise PMatch 1332 else (tymatch (tyinsts, fn () => (T, fastype_of1 (Ts, t))), 1333 (ixn, t) :: insts) 1334 | (Free (a, T), Free (b, U)) => 1335 if a=b then (tymatch (tyinsts, K (T, U)), insts) else raise PMatch 1336 | (Const (a, T), Const (b, U)) => 1337 if a=b then (tymatch (tyinsts, K (T, U)), insts) else raise PMatch 1338 | (f $ t, g $ u) => mtch (mtch instsp (f, g)) (t, u) 1339 | (Bound i, Bound j) => if i=j then instsp else raise PMatch 1340 | _ => raise PMatch 1341 in mtch instsp (apply2 Envir.beta_eta_contract p) end; 1342 1343fun match_proof Ts tymatch = 1344 let 1345 fun optmatch _ inst (NONE, _) = inst 1346 | optmatch _ _ (SOME _, NONE) = raise PMatch 1347 | optmatch mtch inst (SOME x, SOME y) = mtch inst (x, y) 1348 1349 fun matcht Ts j (pinst, tinst) (t, u) = 1350 (pinst, fomatch Ts tymatch j tinst (t, Envir.beta_norm u)); 1351 fun matchT (pinst, (tyinsts, insts)) p = 1352 (pinst, (tymatch (tyinsts, K p), insts)); 1353 fun matchTs inst (Ts, Us) = Library.foldl (uncurry matchT) (inst, Ts ~~ Us); 1354 1355 fun mtch Ts i j (pinst, tinst) (Hyp (Var (ixn, _)), prf) = 1356 if i = 0 andalso j = 0 then ((ixn, prf) :: pinst, tinst) 1357 else 1358 (case apfst (flt i) (apsnd (flt j) (prf_add_loose_bnos 0 0 prf ([], []))) of 1359 ([], []) => ((ixn, incr_pboundvars (~i) (~j) prf) :: pinst, tinst) 1360 | ([], _) => 1361 if j = 0 then ((ixn, incr_pboundvars (~i) (~j) prf) :: pinst, tinst) 1362 else raise PMatch 1363 | _ => raise PMatch) 1364 | mtch Ts i j inst (prf1 % opt1, prf2 % opt2) = 1365 optmatch (matcht Ts j) (mtch Ts i j inst (prf1, prf2)) (opt1, opt2) 1366 | mtch Ts i j inst (prf1 %% prf2, prf1' %% prf2') = 1367 mtch Ts i j (mtch Ts i j inst (prf1, prf1')) (prf2, prf2') 1368 | mtch Ts i j inst (Abst (_, opT, prf1), Abst (_, opU, prf2)) = 1369 mtch (the_default dummyT opU :: Ts) i (j+1) 1370 (optmatch matchT inst (opT, opU)) (prf1, prf2) 1371 | mtch Ts i j inst (prf1, Abst (_, opU, prf2)) = 1372 mtch (the_default dummyT opU :: Ts) i (j+1) inst 1373 (incr_pboundvars 0 1 prf1 %> Bound 0, prf2) 1374 | mtch Ts i j inst (AbsP (_, opt, prf1), AbsP (_, opu, prf2)) = 1375 mtch Ts (i+1) j (optmatch (matcht Ts j) inst (opt, opu)) (prf1, prf2) 1376 | mtch Ts i j inst (prf1, AbsP (_, _, prf2)) = 1377 mtch Ts (i+1) j inst (incr_pboundvars 1 0 prf1 %% PBound 0, prf2) 1378 | mtch Ts i j inst (PAxm (s1, _, opTs), PAxm (s2, _, opUs)) = 1379 if s1 = s2 then optmatch matchTs inst (opTs, opUs) 1380 else raise PMatch 1381 | mtch Ts i j inst (OfClass (T1, c1), OfClass (T2, c2)) = 1382 if c1 = c2 then matchT inst (T1, T2) 1383 else raise PMatch 1384 | mtch Ts i j inst 1385 (PThm ({name = name1, prop = prop1, types = types1, ...}, _), 1386 PThm ({name = name2, prop = prop2, types = types2, ...}, _)) = 1387 if name1 = name2 andalso prop1 = prop2 1388 then optmatch matchTs inst (types1, types2) 1389 else raise PMatch 1390 | mtch _ _ _ inst (PBound i, PBound j) = if i = j then inst else raise PMatch 1391 | mtch _ _ _ _ _ = raise PMatch 1392 in mtch Ts 0 0 end; 1393 1394fun prf_subst (pinst, (tyinsts, insts)) = 1395 let 1396 val substT = Envir.subst_type_same tyinsts; 1397 val substTs = Same.map substT; 1398 1399 fun subst' lev (Var (xi, _)) = 1400 (case AList.lookup (op =) insts xi of 1401 NONE => raise Same.SAME 1402 | SOME u => incr_boundvars lev u) 1403 | subst' _ (Const (s, T)) = Const (s, substT T) 1404 | subst' _ (Free (s, T)) = Free (s, substT T) 1405 | subst' lev (Abs (a, T, body)) = 1406 (Abs (a, substT T, Same.commit (subst' (lev + 1)) body) 1407 handle Same.SAME => Abs (a, T, subst' (lev + 1) body)) 1408 | subst' lev (f $ t) = 1409 (subst' lev f $ Same.commit (subst' lev) t 1410 handle Same.SAME => f $ subst' lev t) 1411 | subst' _ _ = raise Same.SAME; 1412 1413 fun subst plev tlev (AbsP (a, t, body)) = 1414 (AbsP (a, Same.map_option (subst' tlev) t, Same.commit (subst (plev + 1) tlev) body) 1415 handle Same.SAME => AbsP (a, t, subst (plev + 1) tlev body)) 1416 | subst plev tlev (Abst (a, T, body)) = 1417 (Abst (a, Same.map_option substT T, Same.commit (subst plev (tlev + 1)) body) 1418 handle Same.SAME => Abst (a, T, subst plev (tlev + 1) body)) 1419 | subst plev tlev (prf %% prf') = 1420 (subst plev tlev prf %% Same.commit (subst plev tlev) prf' 1421 handle Same.SAME => prf %% subst plev tlev prf') 1422 | subst plev tlev (prf % t) = 1423 (subst plev tlev prf % Same.commit (Same.map_option (subst' tlev)) t 1424 handle Same.SAME => prf % Same.map_option (subst' tlev) t) 1425 | subst plev tlev (Hyp (Var (xi, _))) = 1426 (case AList.lookup (op =) pinst xi of 1427 NONE => raise Same.SAME 1428 | SOME prf' => incr_pboundvars plev tlev prf') 1429 | subst _ _ (PAxm (id, prop, Ts)) = PAxm (id, prop, Same.map_option substTs Ts) 1430 | subst _ _ (OfClass (T, c)) = OfClass (substT T, c) 1431 | subst _ _ (Oracle (id, prop, Ts)) = Oracle (id, prop, Same.map_option substTs Ts) 1432 | subst _ _ (PThm ({serial = i, pos = p, theory_name, name = id, prop, types}, thm_body)) = 1433 PThm (thm_header i p theory_name id prop (Same.map_option substTs types), thm_body) 1434 | subst _ _ _ = raise Same.SAME; 1435 in fn t => subst 0 0 t handle Same.SAME => t end; 1436 1437(*A fast unification filter: true unless the two terms cannot be unified. 1438 Terms must be NORMAL. Treats all Vars as distinct. *) 1439fun could_unify prf1 prf2 = 1440 let 1441 fun matchrands (prf1 %% prf2) (prf1' %% prf2') = 1442 could_unify prf2 prf2' andalso matchrands prf1 prf1' 1443 | matchrands (prf % SOME t) (prf' % SOME t') = 1444 Term.could_unify (t, t') andalso matchrands prf prf' 1445 | matchrands (prf % _) (prf' % _) = matchrands prf prf' 1446 | matchrands _ _ = true 1447 1448 fun head_of (prf %% _) = head_of prf 1449 | head_of (prf % _) = head_of prf 1450 | head_of prf = prf 1451 1452 in case (head_of prf1, head_of prf2) of 1453 (_, Hyp (Var _)) => true 1454 | (Hyp (Var _), _) => true 1455 | (PAxm (a, _, _), PAxm (b, _, _)) => a = b andalso matchrands prf1 prf2 1456 | (OfClass (_, c), OfClass (_, d)) => c = d andalso matchrands prf1 prf2 1457 | (PThm ({name = a, prop = propa, ...}, _), PThm ({name = b, prop = propb, ...}, _)) => 1458 a = b andalso propa = propb andalso matchrands prf1 prf2 1459 | (PBound i, PBound j) => i = j andalso matchrands prf1 prf2 1460 | (AbsP _, _) => true (*because of possible eta equality*) 1461 | (Abst _, _) => true 1462 | (_, AbsP _) => true 1463 | (_, Abst _) => true 1464 | _ => false 1465 end; 1466 1467 1468(* rewrite proof *) 1469 1470val no_skel = PBound 0; 1471val normal_skel = Hyp (Var ((Name.uu, 0), propT)); 1472 1473fun rewrite_prf tymatch (rules, procs) prf = 1474 let 1475 fun rew _ _ (Abst (_, _, body) % SOME t) = SOME (prf_subst_bounds [t] body, no_skel) 1476 | rew _ _ (AbsP (_, _, body) %% prf) = SOME (prf_subst_pbounds [prf] body, no_skel) 1477 | rew Ts hs prf = 1478 (case get_first (fn r => r Ts hs prf) procs of 1479 NONE => get_first (fn (prf1, prf2) => SOME (prf_subst 1480 (match_proof Ts tymatch ([], (Vartab.empty, [])) (prf1, prf)) prf2, prf2) 1481 handle PMatch => NONE) (filter (could_unify prf o fst) rules) 1482 | some => some); 1483 1484 fun rew0 Ts hs (prf as AbsP (_, _, prf' %% PBound 0)) = 1485 if prf_loose_Pbvar1 prf' 0 then rew Ts hs prf 1486 else 1487 let val prf'' = incr_pboundvars (~1) 0 prf' 1488 in SOME (the_default (prf'', no_skel) (rew Ts hs prf'')) end 1489 | rew0 Ts hs (prf as Abst (_, _, prf' % SOME (Bound 0))) = 1490 if prf_loose_bvar1 prf' 0 then rew Ts hs prf 1491 else 1492 let val prf'' = incr_pboundvars 0 (~1) prf' 1493 in SOME (the_default (prf'', no_skel) (rew Ts hs prf'')) end 1494 | rew0 Ts hs prf = rew Ts hs prf; 1495 1496 fun rew1 _ _ (Hyp (Var _)) _ = NONE 1497 | rew1 Ts hs skel prf = 1498 (case rew2 Ts hs skel prf of 1499 SOME prf1 => 1500 (case rew0 Ts hs prf1 of 1501 SOME (prf2, skel') => SOME (the_default prf2 (rew1 Ts hs skel' prf2)) 1502 | NONE => SOME prf1) 1503 | NONE => 1504 (case rew0 Ts hs prf of 1505 SOME (prf1, skel') => SOME (the_default prf1 (rew1 Ts hs skel' prf1)) 1506 | NONE => NONE)) 1507 1508 and rew2 Ts hs skel (prf % SOME t) = 1509 (case prf of 1510 Abst (_, _, body) => 1511 let val prf' = prf_subst_bounds [t] body 1512 in SOME (the_default prf' (rew2 Ts hs no_skel prf')) end 1513 | _ => 1514 (case rew1 Ts hs (case skel of skel' % _ => skel' | _ => no_skel) prf of 1515 SOME prf' => SOME (prf' % SOME t) 1516 | NONE => NONE)) 1517 | rew2 Ts hs skel (prf % NONE) = Option.map (fn prf' => prf' % NONE) 1518 (rew1 Ts hs (case skel of skel' % _ => skel' | _ => no_skel) prf) 1519 | rew2 Ts hs skel (prf1 %% prf2) = 1520 (case prf1 of 1521 AbsP (_, _, body) => 1522 let val prf' = prf_subst_pbounds [prf2] body 1523 in SOME (the_default prf' (rew2 Ts hs no_skel prf')) end 1524 | _ => 1525 let 1526 val (skel1, skel2) = 1527 (case skel of 1528 skel1 %% skel2 => (skel1, skel2) 1529 | _ => (no_skel, no_skel)) 1530 in 1531 (case rew1 Ts hs skel1 prf1 of 1532 SOME prf1' => 1533 (case rew1 Ts hs skel2 prf2 of 1534 SOME prf2' => SOME (prf1' %% prf2') 1535 | NONE => SOME (prf1' %% prf2)) 1536 | NONE => 1537 (case rew1 Ts hs skel2 prf2 of 1538 SOME prf2' => SOME (prf1 %% prf2') 1539 | NONE => NONE)) 1540 end) 1541 | rew2 Ts hs skel (Abst (s, T, prf)) = 1542 (case rew1 (the_default dummyT T :: Ts) hs 1543 (case skel of Abst (_, _, skel') => skel' | _ => no_skel) prf of 1544 SOME prf' => SOME (Abst (s, T, prf')) 1545 | NONE => NONE) 1546 | rew2 Ts hs skel (AbsP (s, t, prf)) = 1547 (case rew1 Ts (t :: hs) (case skel of AbsP (_, _, skel') => skel' | _ => no_skel) prf of 1548 SOME prf' => SOME (AbsP (s, t, prf')) 1549 | NONE => NONE) 1550 | rew2 _ _ _ _ = NONE; 1551 1552 in the_default prf (rew1 [] [] no_skel prf) end; 1553 1554fun rewrite_proof thy = rewrite_prf (fn (tyenv, f) => 1555 Sign.typ_match thy (f ()) tyenv handle Type.TYPE_MATCH => raise PMatch); 1556 1557fun rewrite_proof_notypes rews = rewrite_prf fst rews; 1558 1559 1560(* theory data *) 1561 1562structure Data = Theory_Data 1563( 1564 type T = 1565 ((stamp * (proof * proof)) list * 1566 (stamp * (typ list -> term option list -> proof -> (proof * proof) option)) list) * 1567 (theory -> proof -> proof) option; 1568 1569 val empty = (([], []), NONE); 1570 val extend = I; 1571 fun merge (((rules1, procs1), preproc1), ((rules2, procs2), preproc2)) : T = 1572 ((AList.merge (op =) (K true) (rules1, rules2), 1573 AList.merge (op =) (K true) (procs1, procs2)), 1574 merge_options (preproc1, preproc2)); 1575); 1576 1577fun get_rew_data thy = 1578 let val (rules, procs) = #1 (Data.get thy) 1579 in (map #2 rules, map #2 procs) end; 1580 1581fun rew_proof thy = rewrite_prf fst (get_rew_data thy); 1582 1583fun add_prf_rrule r = (Data.map o apfst o apfst) (cons (stamp (), r)); 1584fun add_prf_rproc p = (Data.map o apfst o apsnd) (cons (stamp (), p)); 1585 1586fun set_preproc f = (Data.map o apsnd) (K (SOME f)); 1587fun apply_preproc thy = (case #2 (Data.get thy) of NONE => I | SOME f => f thy); 1588 1589 1590 1591(** reconstruction of partial proof terms **) 1592 1593fun forall_intr_variables_term prop = fold_rev Logic.all (variables_of prop) prop; 1594fun forall_intr_variables prop prf = fold_rev forall_intr_proof' (variables_of prop) prf; 1595 1596local 1597 1598fun app_types shift prop Ts prf = 1599 let 1600 val inst = type_variables_of prop ~~ Ts; 1601 fun subst_same A = 1602 (case AList.lookup (op =) inst A of SOME T => T | NONE => raise Same.SAME); 1603 val subst_type_same = 1604 Term_Subst.map_atypsT_same 1605 (fn TVar ((a, i), S) => subst_same (TVar ((a, i - shift), S)) | A => subst_same A); 1606 in Same.commit (map_proof_types_same subst_type_same) prf end; 1607 1608fun guess_name (PThm ({name, ...}, _)) = name 1609 | guess_name (prf %% Hyp _) = guess_name prf 1610 | guess_name (prf %% OfClass _) = guess_name prf 1611 | guess_name (prf % NONE) = guess_name prf 1612 | guess_name (prf % SOME (Var _)) = guess_name prf 1613 | guess_name _ = ""; 1614 1615 1616(* generate constraints for proof term *) 1617 1618fun mk_var env Ts T = 1619 let val (env', v) = Envir.genvar "a" (env, rev Ts ---> T) 1620 in (list_comb (v, map Bound (length Ts - 1 downto 0)), env') end; 1621 1622fun mk_tvar S (Envir.Envir {maxidx, tenv, tyenv}) = 1623 (TVar (("'t", maxidx + 1), S), 1624 Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv}); 1625 1626val mk_abs = fold (fn T => fn u => Abs ("", T, u)); 1627 1628fun unifyT thy env T U = 1629 let 1630 val Envir.Envir {maxidx, tenv, tyenv} = env; 1631 val (tyenv', maxidx') = Sign.typ_unify thy (T, U) (tyenv, maxidx); 1632 in Envir.Envir {maxidx = maxidx', tenv = tenv, tyenv = tyenv'} end; 1633 1634fun chaseT env (T as TVar v) = 1635 (case Type.lookup (Envir.type_env env) v of 1636 NONE => T 1637 | SOME T' => chaseT env T') 1638 | chaseT _ T = T; 1639 1640fun infer_type thy (env as Envir.Envir {maxidx, tenv, tyenv}) _ vTs (t as Const (s, T)) = 1641 if T = dummyT then 1642 (case Sign.const_type thy s of 1643 NONE => error ("reconstruct_proof: No such constant: " ^ quote s) 1644 | SOME T => 1645 let val T' = Type.strip_sorts (Logic.incr_tvar (maxidx + 1) T) in 1646 (Const (s, T'), T', vTs, 1647 Envir.Envir {maxidx = maxidx + 1, tenv = tenv, tyenv = tyenv}) 1648 end) 1649 else (t, T, vTs, env) 1650 | infer_type _ env _ vTs (t as Free (s, T)) = 1651 if T = dummyT then 1652 (case Symtab.lookup vTs s of 1653 NONE => 1654 let val (T, env') = mk_tvar [] env 1655 in (Free (s, T), T, Symtab.update_new (s, T) vTs, env') end 1656 | SOME T => (Free (s, T), T, vTs, env)) 1657 else (t, T, vTs, env) 1658 | infer_type _ _ _ _ (Var _) = error "reconstruct_proof: internal error" 1659 | infer_type thy env Ts vTs (Abs (s, T, t)) = 1660 let 1661 val (T', env') = if T = dummyT then mk_tvar [] env else (T, env); 1662 val (t', U, vTs', env'') = infer_type thy env' (T' :: Ts) vTs t 1663 in (Abs (s, T', t'), T' --> U, vTs', env'') end 1664 | infer_type thy env Ts vTs (t $ u) = 1665 let 1666 val (t', T, vTs1, env1) = infer_type thy env Ts vTs t; 1667 val (u', U, vTs2, env2) = infer_type thy env1 Ts vTs1 u; 1668 in 1669 (case chaseT env2 T of 1670 Type ("fun", [U', V]) => (t' $ u', V, vTs2, unifyT thy env2 U U') 1671 | _ => 1672 let val (V, env3) = mk_tvar [] env2 1673 in (t' $ u', V, vTs2, unifyT thy env3 T (U --> V)) end) 1674 end 1675 | infer_type _ env Ts vTs (t as Bound i) = ((t, nth Ts i, vTs, env) 1676 handle General.Subscript => error ("infer_type: bad variable index " ^ string_of_int i)); 1677 1678fun cantunify thy (t, u) = 1679 error ("Non-unifiable terms:\n" ^ 1680 Syntax.string_of_term_global thy t ^ "\n\n" ^ Syntax.string_of_term_global thy u); 1681 1682fun decompose thy Ts (p as (t, u)) env = 1683 let 1684 fun rigrig (a, T) (b, U) uT ts us = 1685 if a <> b then cantunify thy p 1686 else apfst flat (fold_map (decompose thy Ts) (ts ~~ us) (uT env T U)) 1687 in 1688 case apply2 (strip_comb o Envir.head_norm env) p of 1689 ((Const c, ts), (Const d, us)) => rigrig c d (unifyT thy) ts us 1690 | ((Free c, ts), (Free d, us)) => rigrig c d (unifyT thy) ts us 1691 | ((Bound i, ts), (Bound j, us)) => 1692 rigrig (i, dummyT) (j, dummyT) (K o K) ts us 1693 | ((Abs (_, T, t), []), (Abs (_, U, u), [])) => 1694 decompose thy (T::Ts) (t, u) (unifyT thy env T U) 1695 | ((Abs (_, T, t), []), _) => 1696 decompose thy (T::Ts) (t, incr_boundvars 1 u $ Bound 0) env 1697 | (_, (Abs (_, T, u), [])) => 1698 decompose thy (T::Ts) (incr_boundvars 1 t $ Bound 0, u) env 1699 | _ => ([(mk_abs Ts t, mk_abs Ts u)], env) 1700 end; 1701 1702fun make_constraints_cprf thy env cprf = 1703 let 1704 fun add_cnstrt Ts prop prf cs env vTs (t, u) = 1705 let 1706 val t' = mk_abs Ts t; 1707 val u' = mk_abs Ts u 1708 in 1709 (prop, prf, cs, Pattern.unify (Context.Theory thy) (t', u') env, vTs) 1710 handle Pattern.Pattern => 1711 let val (cs', env') = decompose thy [] (t', u') env 1712 in (prop, prf, cs @ cs', env', vTs) end 1713 | Pattern.Unif => cantunify thy (Envir.norm_term env t', Envir.norm_term env u') 1714 end; 1715 1716 fun mk_cnstrts_atom env vTs prop opTs prf = 1717 let 1718 val prop_types = type_variables_of prop; 1719 val (Ts, env') = 1720 (case opTs of 1721 NONE => fold_map (mk_tvar o Type.sort_of_atyp) prop_types env 1722 | SOME Ts => (Ts, env)); 1723 val prop' = subst_atomic_types (prop_types ~~ Ts) 1724 (forall_intr_variables_term prop) handle ListPair.UnequalLengths => 1725 error ("Wrong number of type arguments for " ^ quote (guess_name prf)) 1726 in (prop', change_types (SOME Ts) prf, [], env', vTs) end; 1727 1728 fun head_norm (prop, prf, cnstrts, env, vTs) = 1729 (Envir.head_norm env prop, prf, cnstrts, env, vTs); 1730 1731 fun mk_cnstrts env _ Hs vTs (PBound i) = ((nth Hs i, PBound i, [], env, vTs) 1732 handle General.Subscript => error ("mk_cnstrts: bad variable index " ^ string_of_int i)) 1733 | mk_cnstrts env Ts Hs vTs (Abst (s, opT, cprf)) = 1734 let 1735 val (T, env') = 1736 (case opT of 1737 NONE => mk_tvar [] env 1738 | SOME T => (T, env)); 1739 val (t, prf, cnstrts, env'', vTs') = 1740 mk_cnstrts env' (T::Ts) (map (incr_boundvars 1) Hs) vTs cprf; 1741 in 1742 (Const ("Pure.all", (T --> propT) --> propT) $ Abs (s, T, t), Abst (s, SOME T, prf), 1743 cnstrts, env'', vTs') 1744 end 1745 | mk_cnstrts env Ts Hs vTs (AbsP (s, SOME t, cprf)) = 1746 let 1747 val (t', _, vTs', env') = infer_type thy env Ts vTs t; 1748 val (u, prf, cnstrts, env'', vTs'') = mk_cnstrts env' Ts (t'::Hs) vTs' cprf; 1749 in (Logic.mk_implies (t', u), AbsP (s, SOME t', prf), cnstrts, env'', vTs'') 1750 end 1751 | mk_cnstrts env Ts Hs vTs (AbsP (s, NONE, cprf)) = 1752 let 1753 val (t, env') = mk_var env Ts propT; 1754 val (u, prf, cnstrts, env'', vTs') = mk_cnstrts env' Ts (t::Hs) vTs cprf; 1755 in (Logic.mk_implies (t, u), AbsP (s, SOME t, prf), cnstrts, env'', vTs') 1756 end 1757 | mk_cnstrts env Ts Hs vTs (cprf1 %% cprf2) = 1758 let val (u, prf2, cnstrts, env', vTs') = mk_cnstrts env Ts Hs vTs cprf2 1759 in (case head_norm (mk_cnstrts env' Ts Hs vTs' cprf1) of 1760 (Const ("Pure.imp", _) $ u' $ t', prf1, cnstrts', env'', vTs'') => 1761 add_cnstrt Ts t' (prf1 %% prf2) (cnstrts' @ cnstrts) 1762 env'' vTs'' (u, u') 1763 | (t, prf1, cnstrts', env'', vTs'') => 1764 let val (v, env''') = mk_var env'' Ts propT 1765 in add_cnstrt Ts v (prf1 %% prf2) (cnstrts' @ cnstrts) 1766 env''' vTs'' (t, Logic.mk_implies (u, v)) 1767 end) 1768 end 1769 | mk_cnstrts env Ts Hs vTs (cprf % SOME t) = 1770 let val (t', U, vTs1, env1) = infer_type thy env Ts vTs t 1771 in (case head_norm (mk_cnstrts env1 Ts Hs vTs1 cprf) of 1772 (Const ("Pure.all", Type ("fun", [Type ("fun", [T, _]), _])) $ f, 1773 prf, cnstrts, env2, vTs2) => 1774 let val env3 = unifyT thy env2 T U 1775 in (betapply (f, t'), prf % SOME t', cnstrts, env3, vTs2) 1776 end 1777 | (u, prf, cnstrts, env2, vTs2) => 1778 let val (v, env3) = mk_var env2 Ts (U --> propT); 1779 in 1780 add_cnstrt Ts (v $ t') (prf % SOME t') cnstrts env3 vTs2 1781 (u, Const ("Pure.all", (U --> propT) --> propT) $ v) 1782 end) 1783 end 1784 | mk_cnstrts env Ts Hs vTs (cprf % NONE) = 1785 (case head_norm (mk_cnstrts env Ts Hs vTs cprf) of 1786 (Const ("Pure.all", Type ("fun", [Type ("fun", [T, _]), _])) $ f, 1787 prf, cnstrts, env', vTs') => 1788 let val (t, env'') = mk_var env' Ts T 1789 in (betapply (f, t), prf % SOME t, cnstrts, env'', vTs') 1790 end 1791 | (u, prf, cnstrts, env', vTs') => 1792 let 1793 val (T, env1) = mk_tvar [] env'; 1794 val (v, env2) = mk_var env1 Ts (T --> propT); 1795 val (t, env3) = mk_var env2 Ts T 1796 in 1797 add_cnstrt Ts (v $ t) (prf % SOME t) cnstrts env3 vTs' 1798 (u, Const ("Pure.all", (T --> propT) --> propT) $ v) 1799 end) 1800 | mk_cnstrts env _ _ vTs (prf as PThm ({prop, types = opTs, ...}, _)) = 1801 mk_cnstrts_atom env vTs prop opTs prf 1802 | mk_cnstrts env _ _ vTs (prf as PAxm (_, prop, opTs)) = 1803 mk_cnstrts_atom env vTs prop opTs prf 1804 | mk_cnstrts env _ _ vTs (prf as OfClass (T, c)) = 1805 mk_cnstrts_atom env vTs (Logic.mk_of_class (T, c)) NONE prf 1806 | mk_cnstrts env _ _ vTs (prf as Oracle (_, prop, opTs)) = 1807 mk_cnstrts_atom env vTs prop opTs prf 1808 | mk_cnstrts env _ _ vTs (Hyp t) = (t, Hyp t, [], env, vTs) 1809 | mk_cnstrts _ _ _ _ MinProof = raise MIN_PROOF () 1810 in mk_cnstrts env [] [] Symtab.empty cprf end; 1811 1812 1813(* update list of free variables of constraints *) 1814 1815fun upd_constrs env cs = 1816 let 1817 val tenv = Envir.term_env env; 1818 val tyenv = Envir.type_env env; 1819 val dom = [] 1820 |> Vartab.fold (cons o #1) tenv 1821 |> Vartab.fold (cons o #1) tyenv; 1822 val vran = [] 1823 |> Vartab.fold (Term.add_var_names o #2 o #2) tenv 1824 |> Vartab.fold (Term.add_tvar_namesT o #2 o #2) tyenv; 1825 fun check_cs [] = [] 1826 | check_cs ((u, p, vs) :: ps) = 1827 let val vs' = subtract (op =) dom vs in 1828 if vs = vs' then (u, p, vs) :: check_cs ps 1829 else (true, p, fold (insert op =) vs' vran) :: check_cs ps 1830 end; 1831 in check_cs cs end; 1832 1833 1834(* solution of constraints *) 1835 1836fun solve _ [] bigenv = bigenv 1837 | solve thy cs bigenv = 1838 let 1839 fun search _ [] = 1840 error ("Unsolvable constraints:\n" ^ 1841 Pretty.string_of (Pretty.chunks (map (fn (_, p, _) => 1842 Syntax.pretty_flexpair (Syntax.init_pretty_global thy) 1843 (apply2 (Envir.norm_term bigenv) p)) cs))) 1844 | search env ((u, p as (t1, t2), vs)::ps) = 1845 if u then 1846 let 1847 val tn1 = Envir.norm_term bigenv t1; 1848 val tn2 = Envir.norm_term bigenv t2 1849 in 1850 if Pattern.pattern tn1 andalso Pattern.pattern tn2 then 1851 (Pattern.unify (Context.Theory thy) (tn1, tn2) env, ps) 1852 handle Pattern.Unif => cantunify thy (tn1, tn2) 1853 else 1854 let val (cs', env') = decompose thy [] (tn1, tn2) env 1855 in if cs' = [(tn1, tn2)] then 1856 apsnd (cons (false, (tn1, tn2), vs)) (search env ps) 1857 else search env' (map (fn q => (true, q, vs)) cs' @ ps) 1858 end 1859 end 1860 else apsnd (cons (false, p, vs)) (search env ps); 1861 val Envir.Envir {maxidx, ...} = bigenv; 1862 val (env, cs') = search (Envir.empty maxidx) cs; 1863 in 1864 solve thy (upd_constrs env cs') (Envir.merge (bigenv, env)) 1865 end; 1866 1867in 1868 1869 1870(* reconstruction of proofs *) 1871 1872fun reconstruct_proof thy prop cprf = 1873 let 1874 val (cprf' % SOME prop', thawf) = freeze_thaw_prf (cprf % SOME prop); 1875 val (t, prf, cs, env, _) = make_constraints_cprf thy 1876 (Envir.empty (maxidx_proof cprf ~1)) cprf'; 1877 val cs' = 1878 map (apply2 (Envir.norm_term env)) ((t, prop') :: cs) 1879 |> map (fn p => (true, p, Term.add_var_names (#1 p) (Term.add_var_names (#2 p) []))); 1880 val env' = solve thy cs' env; 1881 in thawf (norm_proof env' prf) end handle MIN_PROOF () => MinProof; 1882 1883fun prop_of_atom prop Ts = 1884 subst_atomic_types (type_variables_of prop ~~ Ts) (forall_intr_variables_term prop); 1885 1886val head_norm = Envir.head_norm Envir.init; 1887 1888fun prop_of0 Hs (PBound i) = nth Hs i 1889 | prop_of0 Hs (Abst (s, SOME T, prf)) = 1890 Logic.all_const T $ (Abs (s, T, prop_of0 Hs prf)) 1891 | prop_of0 Hs (AbsP (_, SOME t, prf)) = 1892 Logic.mk_implies (t, prop_of0 (t :: Hs) prf) 1893 | prop_of0 Hs (prf % SOME t) = (case head_norm (prop_of0 Hs prf) of 1894 Const ("Pure.all", _) $ f => f $ t 1895 | _ => error "prop_of: all expected") 1896 | prop_of0 Hs (prf1 %% _) = (case head_norm (prop_of0 Hs prf1) of 1897 Const ("Pure.imp", _) $ _ $ Q => Q 1898 | _ => error "prop_of: ==> expected") 1899 | prop_of0 _ (Hyp t) = t 1900 | prop_of0 _ (PThm ({prop, types = SOME Ts, ...}, _)) = prop_of_atom prop Ts 1901 | prop_of0 _ (PAxm (_, prop, SOME Ts)) = prop_of_atom prop Ts 1902 | prop_of0 _ (OfClass (T, c)) = Logic.mk_of_class (T, c) 1903 | prop_of0 _ (Oracle (_, prop, SOME Ts)) = prop_of_atom prop Ts 1904 | prop_of0 _ _ = error "prop_of: partial proof object"; 1905 1906val prop_of' = Envir.beta_eta_contract oo prop_of0; 1907val prop_of = prop_of' []; 1908 1909 1910(* expand and reconstruct subproofs *) 1911 1912fun expand_name_empty (header: thm_header) = if #name header = "" then SOME "" else NONE; 1913 1914fun expand_proof thy expand_name prf = 1915 let 1916 fun expand seen maxidx (AbsP (s, t, prf)) = 1917 let val (seen', maxidx', prf') = expand seen maxidx prf 1918 in (seen', maxidx', AbsP (s, t, prf')) end 1919 | expand seen maxidx (Abst (s, T, prf)) = 1920 let val (seen', maxidx', prf') = expand seen maxidx prf 1921 in (seen', maxidx', Abst (s, T, prf')) end 1922 | expand seen maxidx (prf1 %% prf2) = 1923 let 1924 val (seen', maxidx', prf1') = expand seen maxidx prf1; 1925 val (seen'', maxidx'', prf2') = expand seen' maxidx' prf2; 1926 in (seen'', maxidx'', prf1' %% prf2') end 1927 | expand seen maxidx (prf % t) = 1928 let val (seen', maxidx', prf') = expand seen maxidx prf 1929 in (seen', maxidx', prf' % t) end 1930 | expand seen maxidx (prf as PThm (header, thm_body)) = 1931 let val {serial, pos, theory_name, name, prop, types} = header in 1932 (case expand_name header of 1933 SOME name' => 1934 if name' = "" andalso is_some types then 1935 let 1936 val (seen', maxidx', prf') = 1937 (case Inttab.lookup seen serial of 1938 NONE => 1939 let 1940 val prf1 = 1941 thm_body_proof_open thm_body 1942 |> reconstruct_proof thy prop 1943 |> forall_intr_variables prop; 1944 val (seen1, maxidx1, prf2) = expand_init seen prf1 1945 val seen2 = seen1 |> Inttab.update (serial, (maxidx1, prf2)); 1946 in (seen2, maxidx1, prf2) end 1947 | SOME (maxidx1, prf1) => (seen, maxidx1, prf1)); 1948 val prf'' = prf' 1949 |> incr_indexes (maxidx + 1) |> app_types (maxidx + 1) prop (the types); 1950 in (seen', maxidx' + maxidx + 1, prf'') end 1951 else if name' <> name then 1952 (seen, maxidx, PThm (thm_header serial pos theory_name name' prop types, thm_body)) 1953 else (seen, maxidx, prf) 1954 | NONE => (seen, maxidx, prf)) 1955 end 1956 | expand seen maxidx prf = (seen, maxidx, prf) 1957 and expand_init seen prf = expand seen (maxidx_proof prf ~1) prf; 1958 1959 in #3 (expand_init Inttab.empty prf) end; 1960 1961end; 1962 1963 1964 1965(** promises **) 1966 1967fun fulfill_norm_proof thy ps body0 = 1968 let 1969 val _ = consolidate_bodies (map #2 ps @ [body0]); 1970 val PBody {oracles = oracles0, thms = thms0, proof = proof0} = body0; 1971 val oracles = 1972 unions_oracles 1973 (fold (fn (_, PBody {oracles, ...}) => not (null oracles) ? cons oracles) ps [oracles0]); 1974 val thms = 1975 unions_thms (fold (fn (_, PBody {thms, ...}) => not (null thms) ? cons thms) ps [thms0]); 1976 val proof = rew_proof thy proof0; 1977 in PBody {oracles = oracles, thms = thms, proof = proof} end; 1978 1979fun fulfill_proof_future thy promises (postproc: proof_body -> proof_body) body = 1980 let 1981 fun fulfill () = 1982 postproc (fulfill_norm_proof thy (map (apsnd Future.join) promises) (Future.join body)); 1983 in 1984 if null promises then Future.map postproc body 1985 else if Future.is_finished body andalso length promises = 1 then 1986 Future.map (fn _ => fulfill ()) (snd (hd promises)) 1987 else 1988 (singleton o Future.forks) 1989 {name = "Proofterm.fulfill_proof_future", group = NONE, 1990 deps = Future.task_of body :: map (Future.task_of o snd) promises, pri = 1, 1991 interrupts = true} 1992 fulfill 1993 end; 1994 1995 1996 1997(** theorems **) 1998 1999(* standardization of variables for export: only frees and named bounds *) 2000 2001local 2002 2003val declare_names_term = Term.declare_term_frees; 2004val declare_names_term' = fn SOME t => declare_names_term t | NONE => I; 2005val declare_names_proof = fold_proof_terms declare_names_term; 2006 2007fun variant names bs x = 2008 #1 (Name.variant x (fold Name.declare bs names)); 2009 2010fun variant_term bs (Abs (x, T, t)) = 2011 let 2012 val x' = variant (declare_names_term t Name.context) bs x; 2013 val t' = variant_term (x' :: bs) t; 2014 in Abs (x', T, t') end 2015 | variant_term bs (t $ u) = variant_term bs t $ variant_term bs u 2016 | variant_term _ t = t; 2017 2018fun variant_proof bs (Abst (x, T, prf)) = 2019 let 2020 val x' = variant (declare_names_proof prf Name.context) bs x; 2021 val prf' = variant_proof (x' :: bs) prf; 2022 in Abst (x', T, prf') end 2023 | variant_proof bs (AbsP (x, t, prf)) = 2024 let 2025 val x' = variant (declare_names_term' t (declare_names_proof prf Name.context)) bs x; 2026 val t' = Option.map (variant_term bs) t; 2027 val prf' = variant_proof (x' :: bs) prf; 2028 in AbsP (x', t', prf') end 2029 | variant_proof bs (prf % t) = variant_proof bs prf % Option.map (variant_term bs) t 2030 | variant_proof bs (prf1 %% prf2) = variant_proof bs prf1 %% variant_proof bs prf2 2031 | variant_proof bs (Hyp t) = Hyp (variant_term bs t) 2032 | variant_proof _ prf = prf; 2033 2034val used_frees_type = fold_atyps (fn TFree (a, _) => Name.declare a | _ => I); 2035fun used_frees_term t = fold_types used_frees_type t #> Term.declare_term_frees t; 2036val used_frees_proof = fold_proof_terms_types used_frees_term used_frees_type; 2037 2038val unvarifyT = Term.map_atyps (fn TVar ((a, _), S) => TFree (a, S) | T => T); 2039val unvarify = Term.map_aterms (fn Var ((x, _), T) => Free (x, T) | t => t) #> map_types unvarifyT; 2040val unvarify_proof = map_proof_terms unvarify unvarifyT; 2041 2042fun hidden_types prop proof = 2043 let 2044 val visible = (fold_types o fold_atyps) (insert (op =)) prop []; 2045 val add_hiddenT = fold_atyps (fn T => not (member (op =) visible T) ? insert (op =) T); 2046 in rev (fold_proof_terms_types (fold_types add_hiddenT) add_hiddenT proof []) end; 2047 2048fun standard_hidden_types term proof = 2049 let 2050 val hidden = hidden_types term proof; 2051 val idx = Term.maxidx_term term (maxidx_proof proof ~1) + 1; 2052 fun smash T = 2053 if member (op =) hidden T then 2054 (case Type.sort_of_atyp T of 2055 [] => dummyT 2056 | S => TVar (("'", idx), S)) 2057 else T; 2058 val smashT = map_atyps smash; 2059 in map_proof_terms (map_types smashT) smashT proof end; 2060 2061fun standard_hidden_terms term proof = 2062 let 2063 fun add_unless excluded x = 2064 ((is_Free x orelse is_Var x) andalso not (member (op =) excluded x)) ? insert (op =) x; 2065 val visible = fold_aterms (add_unless []) term []; 2066 val hidden = fold_proof_terms (fold_aterms (add_unless visible)) proof []; 2067 val dummy_term = Term.map_aterms (fn x => 2068 if member (op =) hidden x then Term.dummy_pattern (Term.fastype_of x) else x); 2069 in proof |> not (null hidden) ? map_proof_terms dummy_term I end; 2070 2071in 2072 2073fun standard_vars used (term, opt_proof) = 2074 let 2075 val proofs = opt_proof 2076 |> Option.map (standard_hidden_types term #> standard_hidden_terms term) |> the_list; 2077 val proof_terms = rev (fold (fold_proof_terms_types cons (cons o Logic.mk_type)) proofs []); 2078 val used_frees = used 2079 |> used_frees_term term 2080 |> fold used_frees_proof proofs; 2081 val inst = Term_Subst.zero_var_indexes_inst used_frees (term :: proof_terms); 2082 val term' = term |> Term_Subst.instantiate inst |> unvarify |> variant_term []; 2083 val proofs' = proofs |> map (instantiate inst #> unvarify_proof #> variant_proof []); 2084 in (term', try hd proofs') end; 2085 2086fun standard_vars_term used t = #1 (standard_vars used (t, NONE)); 2087 2088val add_standard_vars_term = fold_aterms 2089 (fn Free (x, T) => 2090 (fn env => 2091 (case AList.lookup (op =) env x of 2092 NONE => (x, T) :: env 2093 | SOME T' => 2094 if T = T' then env 2095 else raise TYPE ("standard_vars_env: type conflict for variable " ^ quote x, [T, T'], []))) 2096 | _ => I); 2097 2098val add_standard_vars = fold_proof_terms add_standard_vars_term; 2099 2100end; 2101 2102 2103(* PThm nodes *) 2104 2105fun prune_body body = 2106 if Options.default_bool "prune_proofs" 2107 then (Future.map o map_proof_of) (K MinProof) body 2108 else body; 2109 2110fun export_enabled () = Options.default_bool "export_proofs"; 2111fun export_standard_enabled () = Options.default_bool "export_standard_proofs"; 2112 2113fun export_proof_boxes_required thy = 2114 Context.theory_name thy = Context.PureN orelse 2115 (export_enabled () andalso not (export_standard_enabled ())); 2116 2117fun export_proof_boxes bodies = 2118 let 2119 fun export_thm (i, thm_node) boxes = 2120 if Inttab.defined boxes i then boxes 2121 else 2122 boxes 2123 |> Inttab.update (i, thm_node_export thm_node) 2124 |> fold export_thm (thm_node_thms thm_node); 2125 2126 fun export_body (PBody {thms, ...}) = fold export_thm thms; 2127 2128 val exports = (bodies, Inttab.empty) |-> fold export_body |> Inttab.dest; 2129 in List.app (Lazy.force o #2) exports end; 2130 2131local 2132 2133fun unconstrainT_proof algebra classrel_proof arity_proof (ucontext: Logic.unconstrain_context) = 2134 let 2135 fun hyp_map hyp = 2136 (case AList.lookup (op =) (#constraints ucontext) hyp of 2137 SOME t => Hyp t 2138 | NONE => raise Fail "unconstrainT_proof: missing constraint"); 2139 2140 val typ = Term_Subst.map_atypsT_same (Type.strip_sorts o #atyp_map ucontext); 2141 fun ofclass (ty, c) = 2142 let val ty' = Term.map_atyps (#atyp_map ucontext) ty; 2143 in the_single (of_sort_proof algebra classrel_proof arity_proof hyp_map (ty', [c])) end; 2144 in 2145 Same.commit (map_proof_same (Term_Subst.map_types_same typ) typ ofclass) 2146 #> fold_rev (implies_intr_proof o snd) (#constraints ucontext) 2147 end; 2148 2149fun export_proof thy i prop prf0 = 2150 let 2151 val prf = prf0 2152 |> reconstruct_proof thy prop 2153 |> apply_preproc thy; 2154 val (prop', SOME prf') = (prop, SOME prf) |> standard_vars Name.context; 2155 val args = [] |> add_standard_vars_term prop' |> add_standard_vars prf' |> rev; 2156 val typargs = [] |> Term.add_tfrees prop' |> fold_proof_terms Term.add_tfrees prf' |> rev; 2157 2158 val consts = Sign.consts_of thy; 2159 val xml = (typargs, (args, (prop', no_thm_names prf'))) |> 2160 let 2161 open XML.Encode Term_XML.Encode; 2162 val encode_vars = list (pair string typ); 2163 val encode_term = encode_standard_term consts; 2164 val encode_proof = encode_standard_proof consts; 2165 in pair (list (pair string sort)) (pair encode_vars (pair encode_term encode_proof)) end; 2166 in 2167 Export.export_params 2168 {theory = thy, 2169 binding = Path.binding0 (Path.make ["proofs", string_of_int i]), 2170 executable = false, 2171 compress = true, 2172 strict = false} xml 2173 end; 2174 2175fun prepare_thm_proof unconstrain thy classrel_proof arity_proof 2176 (name, pos) shyps hyps concl promises body = 2177 let 2178 val named = name <> ""; 2179 2180 val prop = Logic.list_implies (hyps, concl); 2181 val args = prop_args prop; 2182 2183 val (ucontext, prop1) = Logic.unconstrainT shyps prop; 2184 2185 val PBody {oracles = oracles0, thms = thms0, proof = prf} = body; 2186 val body0 = 2187 Future.value 2188 (PBody {oracles = oracles0, thms = thms0, 2189 proof = if proofs_enabled () then fold_rev implies_intr_proof hyps prf else MinProof}); 2190 2191 fun new_prf () = 2192 let 2193 val i = serial (); 2194 val unconstrainT = 2195 unconstrainT_proof (Sign.classes_of thy) classrel_proof arity_proof ucontext; 2196 val postproc = map_proof_of (unconstrainT #> named ? rew_proof thy); 2197 in (i, fulfill_proof_future thy promises postproc body0) end; 2198 2199 val (i, body') = 2200 (*somewhat non-deterministic proof boxes!*) 2201 if export_enabled () then new_prf () 2202 else 2203 (case strip_combt (fst (strip_combP prf)) of 2204 (PThm ({serial = ser, name = a, prop = prop', types = NONE, ...}, thm_body'), args') => 2205 if (a = "" orelse a = name) andalso prop' = prop1 andalso args' = args then 2206 let 2207 val Thm_Body {body = body', ...} = thm_body'; 2208 val i = if a = "" andalso named then serial () else ser; 2209 in (i, body' |> ser <> i ? Future.map (map_proof_of (rew_proof thy))) end 2210 else new_prf () 2211 | _ => new_prf ()); 2212 2213 val open_proof = not named ? rew_proof thy; 2214 2215 val export = 2216 if export_enabled () then 2217 Lazy.lazy (fn () => 2218 join_proof body' |> open_proof |> export_proof thy i prop1 handle exn => 2219 if Exn.is_interrupt exn then 2220 raise Fail ("Interrupt: potential resource problems while exporting proof " ^ 2221 string_of_int i) 2222 else Exn.reraise exn) 2223 else no_export; 2224 2225 val thm_body = prune_body body'; 2226 val theory_name = Context.theory_long_name thy; 2227 val thm = (i, make_thm_node theory_name name prop1 thm_body export); 2228 2229 val header = thm_header i ([pos, Position.thread_data ()]) theory_name name prop1 NONE; 2230 val head = PThm (header, Thm_Body {open_proof = open_proof, body = thm_body}); 2231 val proof = 2232 if unconstrain then 2233 proof_combt' (head, (map o Option.map o Term.map_types) (#map_atyps ucontext) args) 2234 else 2235 proof_combP (proof_combt' (head, args), 2236 map OfClass (#outer_constraints ucontext) @ map Hyp hyps); 2237 in (thm, proof) end; 2238 2239in 2240 2241fun thm_proof thy = prepare_thm_proof false thy; 2242 2243fun unconstrain_thm_proof thy classrel_proof arity_proof shyps concl promises body = 2244 prepare_thm_proof true thy classrel_proof arity_proof ("", Position.none) 2245 shyps [] concl promises body; 2246 2247end; 2248 2249 2250(* PThm identity *) 2251 2252fun get_identity shyps hyps prop prf = 2253 let val (_, prop) = Logic.unconstrainT shyps (Logic.list_implies (hyps, prop)) in 2254 (case fst (strip_combt (fst (strip_combP prf))) of 2255 PThm ({serial, theory_name, name, prop = prop', ...}, _) => 2256 if prop = prop' 2257 then SOME {serial = serial, theory_name = theory_name, name = name} else NONE 2258 | _ => NONE) 2259 end; 2260 2261fun get_approximative_name shyps hyps prop prf = 2262 Option.map #name (get_identity shyps hyps prop prf) |> the_default ""; 2263 2264 2265(* thm_id *) 2266 2267type thm_id = {serial: serial, theory_name: string}; 2268 2269fun make_thm_id (serial, theory_name) : thm_id = 2270 {serial = serial, theory_name = theory_name}; 2271 2272fun thm_header_id ({serial, theory_name, ...}: thm_header) = 2273 make_thm_id (serial, theory_name); 2274 2275fun thm_id (serial, thm_node) : thm_id = 2276 make_thm_id (serial, thm_node_theory_name thm_node); 2277 2278fun get_id shyps hyps prop prf : thm_id option = 2279 (case get_identity shyps hyps prop prf of 2280 NONE => NONE 2281 | SOME {name = "", ...} => NONE 2282 | SOME {serial, theory_name, ...} => SOME (make_thm_id (serial, theory_name))); 2283 2284fun this_id NONE _ = false 2285 | this_id (SOME (thm_id: thm_id)) (thm_id': thm_id) = #serial thm_id = #serial thm_id'; 2286 2287 2288(* proof boxes: intermediate PThm nodes *) 2289 2290fun proof_boxes {included, excluded} proofs = 2291 let 2292 fun boxes_of (Abst (_, _, prf)) = boxes_of prf 2293 | boxes_of (AbsP (_, _, prf)) = boxes_of prf 2294 | boxes_of (prf % _) = boxes_of prf 2295 | boxes_of (prf1 %% prf2) = boxes_of prf1 #> boxes_of prf2 2296 | boxes_of (PThm (header as {serial = i, ...}, thm_body)) = 2297 (fn boxes => 2298 let val thm_id = thm_header_id header in 2299 if Inttab.defined boxes i orelse (excluded thm_id andalso not (included thm_id)) 2300 then boxes 2301 else 2302 let 2303 val prf' = thm_body_proof_open thm_body; 2304 val boxes' = Inttab.update (i, (header, prf')) boxes; 2305 in boxes_of prf' boxes' end 2306 end) 2307 | boxes_of MinProof = raise MIN_PROOF () 2308 | boxes_of _ = I; 2309 in Inttab.fold_rev (cons o #2) (fold boxes_of proofs Inttab.empty) [] end; 2310 2311end; 2312 2313structure Basic_Proofterm = 2314struct 2315 datatype proof = datatype Proofterm.proof 2316 datatype proof_body = datatype Proofterm.proof_body 2317 val op %> = Proofterm.%> 2318end; 2319 2320open Basic_Proofterm; 2321