src/Pure/thm.ML
changeset 1597 54ece585bf62
parent 1580 e3fd931e6095
child 1634 9b9cdef70669
equal deleted inserted replaced
1596:4a09f4698813 1597:54ece585bf62
    36     string list -> bool -> string * typ -> cterm * (indexname * typ) list
    36     string list -> bool -> string * typ -> cterm * (indexname * typ) list
    37 
    37 
    38   (*theories*)
    38   (*theories*)
    39 
    39 
    40   (*proof terms [must duplicate declaration as a specification]*)
    40   (*proof terms [must duplicate declaration as a specification]*)
    41   val full_deriv	: bool ref
    41   datatype deriv_kind = MinDeriv | ThmDeriv | FullDeriv;
       
    42   val keep_derivs	: deriv_kind ref
    42   datatype rule = 
    43   datatype rule = 
    43       MinProof				
    44       MinProof				
       
    45     | Oracle of theory * Sign.sg * exn
    44     | Axiom		of theory * string
    46     | Axiom		of theory * string
    45     | Theorem		of theory * string	
    47     | Theorem		of string	
    46     | Assume		of cterm
    48     | Assume		of cterm
    47     | Implies_intr	of cterm
    49     | Implies_intr	of cterm
    48     | Implies_intr_shyps
    50     | Implies_intr_shyps
    49     | Implies_intr_hyps
    51     | Implies_intr_hyps
    50     | Implies_elim 
    52     | Implies_elim 
    71     | RewriteC		of cterm
    73     | RewriteC		of cterm
    72     | CongC		of cterm
    74     | CongC		of cterm
    73     | Rewrite_cterm	of cterm
    75     | Rewrite_cterm	of cterm
    74     | Rename_params_rule of string list * int;
    76     | Rename_params_rule of string list * int;
    75 
    77 
    76   datatype deriv = Infer  of rule * deriv list
    78   type deriv   (* = rule mtree *)
    77 		 | Oracle of theory * Sign.sg * exn;
       
    78 
    79 
    79   (*meta theorems*)
    80   (*meta theorems*)
    80   type thm
    81   type thm
    81   exception THM of string * int * thm list
    82   exception THM of string * int * thm list
    82   val rep_thm           : thm -> {sign: Sign.sg, der: deriv, maxidx: int,
    83   val rep_thm           : thm -> {sign: Sign.sg, der: deriv, maxidx: int,
    94   val extra_shyps       : thm -> sort list
    95   val extra_shyps       : thm -> sort list
    95   val force_strip_shyps : bool ref      (* FIXME tmp *)
    96   val force_strip_shyps : bool ref      (* FIXME tmp *)
    96   val strip_shyps       : thm -> thm
    97   val strip_shyps       : thm -> thm
    97   val implies_intr_shyps: thm -> thm
    98   val implies_intr_shyps: thm -> thm
    98   val get_axiom         : theory -> string -> thm
    99   val get_axiom         : theory -> string -> thm
    99   val name_thm          : theory * string * thm -> thm
   100   val name_thm          : string * thm -> thm
   100   val axioms_of         : theory -> (string * thm) list
   101   val axioms_of         : theory -> (string * thm) list
   101 
   102 
   102   (*meta rules*)
   103   (*meta rules*)
   103   val assume            : cterm -> thm
   104   val assume            : cterm -> thm
   104   val compress          : thm -> thm
   105   val compress          : thm -> thm
   278 
   279 
   279 (*Names of rules in derivations.  Includes logically trivial rules, if 
   280 (*Names of rules in derivations.  Includes logically trivial rules, if 
   280   executed in ML.*)
   281   executed in ML.*)
   281 datatype rule = 
   282 datatype rule = 
   282     MinProof				(*for building minimal proof terms*)
   283     MinProof				(*for building minimal proof terms*)
       
   284   | Oracle   	        of theory * Sign.sg * exn	(*oracles*)
   283 (*Axioms/theorems*)
   285 (*Axioms/theorems*)
   284   | Axiom		of theory * string
   286   | Axiom		of theory * string
   285   | Theorem		of theory * string	(*via theorem db*)
   287   | Theorem		of string
   286 (*primitive inferences and compound versions of them*)
   288 (*primitive inferences and compound versions of them*)
   287   | Assume		of cterm
   289   | Assume		of cterm
   288   | Implies_intr	of cterm
   290   | Implies_intr	of cterm
   289   | Implies_intr_shyps
   291   | Implies_intr_shyps
   290   | Implies_intr_hyps
   292   | Implies_intr_hyps
   319   | Rewrite_cterm	of cterm
   321   | Rewrite_cterm	of cterm
   320 (*Logical identities, recorded since they are part of the proof process*)
   322 (*Logical identities, recorded since they are part of the proof process*)
   321   | Rename_params_rule	of string list * int;
   323   | Rename_params_rule	of string list * int;
   322 
   324 
   323 
   325 
   324 datatype deriv = Infer	of rule * deriv list
   326 type deriv = rule mtree;
   325 	       | Oracle	of theory * Sign.sg * exn;
   327 
   326 
   328 datatype deriv_kind = MinDeriv | ThmDeriv | FullDeriv;
   327 
   329 
   328 val full_deriv = ref false;
   330 val keep_derivs = ref MinDeriv;
   329 
   331 
   330 
   332 
   331 (*Suppress all atomic inferences, if using minimal derivations*)
   333 (*Build a minimal derivation.  Keep oracles; suppress atomic inferences;
   332 fun squash_derivs (Infer (_, []) :: drvs) =        squash_derivs drvs
   334   retain Theorems or their underlying links; keep anything else*)
   333   | squash_derivs (der :: ders)           = der :: squash_derivs ders
   335 fun squash_derivs [] = []
   334   | squash_derivs []                      = [];
   336   | squash_derivs (der::ders) =
       
   337      (case der of
       
   338 	  Join (Oracle _, _) => der :: squash_derivs ders
       
   339 	| Join (Theorem _, [der']) => if !keep_derivs=ThmDeriv 
       
   340 				      then der :: squash_derivs ders
       
   341 				      else squash_derivs (der'::ders)
       
   342 	| Join (Axiom _, _) => if !keep_derivs=ThmDeriv 
       
   343 			       then der :: squash_derivs ders
       
   344 			       else squash_derivs ders
       
   345 	| Join (_, [])      => squash_derivs ders
       
   346 	| _                 => der :: squash_derivs ders);
       
   347 
   335 
   348 
   336 (*Ensure sharing of the most likely derivation, the empty one!*)
   349 (*Ensure sharing of the most likely derivation, the empty one!*)
   337 val min_infer = Infer (MinProof, []);
   350 val min_infer = Join (MinProof, []);
   338 
   351 
   339 (*Make a minimal inference*)
   352 (*Make a minimal inference*)
   340 fun make_min_infer []    = min_infer
   353 fun make_min_infer []    = min_infer
   341   | make_min_infer [der] = der
   354   | make_min_infer [der] = der
   342   | make_min_infer ders  = Infer (MinProof, ders);
   355   | make_min_infer ders  = Join (MinProof, ders);
   343 
   356 
   344 fun infer_derivs (rl, [])   = Infer (rl, [])
   357 fun infer_derivs (rl, [])   = Join (rl, [])
   345   | infer_derivs (rl, ders) =
   358   | infer_derivs (rl, ders) =
   346     if !full_deriv then Infer (rl, ders)
   359     if !keep_derivs=FullDeriv then Join (rl, ders)
   347     else make_min_infer (squash_derivs ders);
   360     else make_min_infer (squash_derivs ders);
   348 
   361 
   349 
   362 
   350 (*** Meta theorems ***)
   363 (*** Meta theorems ***)
   351 
   364 
   369 
   382 
   370 (*errors involving theorems*)
   383 (*errors involving theorems*)
   371 exception THM of string * int * thm list;
   384 exception THM of string * int * thm list;
   372 
   385 
   373 
   386 
   374 val sign_of_thm = #sign o rep_thm;
   387 val stamps_of_thm = #stamps o Sign.rep_sg o #sign o rep_thm;
   375 val stamps_of_thm = #stamps o Sign.rep_sg o sign_of_thm;
       
   376 
   388 
   377 (*merge signatures of two theorems; raise exception if incompatible*)
   389 (*merge signatures of two theorems; raise exception if incompatible*)
   378 fun merge_thm_sgs (th1, th2) =
   390 fun merge_thm_sgs (th1, th2) =
   379   Sign.merge (pairself sign_of_thm (th1, th2))
   391   Sign.merge (pairself (#sign o rep_thm) (th1, th2))
   380     handle TERM (msg, _) => raise THM (msg, 0, [th1, th2]);
   392     handle TERM (msg, _) => raise THM (msg, 0, [th1, th2]);
   381 
   393 
   382 
   394 
   383 (*maps object-rule to tpairs*)
   395 (*maps object-rule to tpairs*)
   384 fun tpairs_of (Thm {prop, ...}) = #1 (Logic.strip_flexpairs prop);
   396 fun tpairs_of (Thm {prop, ...}) = #1 (Logic.strip_flexpairs prop);
   537 (*return additional axioms of this theory node*)
   549 (*return additional axioms of this theory node*)
   538 fun axioms_of thy =
   550 fun axioms_of thy =
   539   map (fn (s, _) => (s, get_axiom thy s))
   551   map (fn (s, _) => (s, get_axiom thy s))
   540     (Symtab.dest (#new_axioms (rep_theory thy)));
   552     (Symtab.dest (#new_axioms (rep_theory thy)));
   541 
   553 
   542 fun name_thm (thy, name, th as Thm {sign, der, maxidx, shyps, hyps, prop}) = 
   554 (*Attach a label to a theorem to make proof objects more readable*)
   543     if Sign.eq_sg (sign, sign_of thy) then
   555 fun name_thm (name, th as Thm {sign, der, maxidx, shyps, hyps, prop}) = 
   544 	Thm {sign = sign, 
   556     Thm {sign = sign, 
   545 	     der = Infer (Theorem(thy,name), [der]),
   557 	 der = Join (Theorem name, [der]),
   546 	     maxidx = maxidx,
   558 	 maxidx = maxidx,
   547 	     shyps = shyps, 
   559 	 shyps = shyps, 
   548 	     hyps = hyps, 
   560 	 hyps = hyps, 
   549 	     prop = prop}
   561 	 prop = prop};
   550     else raise THM ("name_thm", 0, [th]);
       
   551 
   562 
   552 
   563 
   553 (*Compression of theorems -- a separate rule, not integrated with the others,
   564 (*Compression of theorems -- a separate rule, not integrated with the others,
   554   as it could be slow.*)
   565   as it could be slow.*)
   555 fun compress (Thm {sign, der, maxidx, shyps, hyps, prop}) = 
   566 fun compress (Thm {sign, der, maxidx, shyps, hyps, prop}) = 
   683 (* Equality *)
   694 (* Equality *)
   684 
   695 
   685 (* Definition of the relation =?= *)
   696 (* Definition of the relation =?= *)
   686 val flexpair_def = fix_shyps [] []
   697 val flexpair_def = fix_shyps [] []
   687   (Thm{sign= Sign.proto_pure, 
   698   (Thm{sign= Sign.proto_pure, 
   688        der = Infer(Axiom(pure_thy, "flexpair_def"), []),
   699        der = Join(Axiom(pure_thy, "flexpair_def"), []),
   689        shyps = [], 
   700        shyps = [], 
   690        hyps = [], 
   701        hyps = [], 
   691        maxidx = 0,
   702        maxidx = 0,
   692        prop = term_of (read_cterm Sign.proto_pure
   703        prop = term_of (read_cterm Sign.proto_pure
   693 		       ("(?t =?= ?u) == (?t == ?u::?'a::{})", propT))});
   704 		       ("(?t =?= ?u) == (?t == ?u::?'a::{})", propT))});
  1752 		    Sign.certify_term sign' (oracle (sign', exn))
  1763 		    Sign.certify_term sign' (oracle (sign', exn))
  1753             in if T<>propT then
  1764             in if T<>propT then
  1754                   raise THM("Oracle's result must have type prop", 0, [])
  1765                   raise THM("Oracle's result must have type prop", 0, [])
  1755 	       else fix_shyps [] []
  1766 	       else fix_shyps [] []
  1756 		     (Thm {sign = sign', 
  1767 		     (Thm {sign = sign', 
  1757 			   der = Oracle(thy,sign,exn),
  1768 			   der = Join (Oracle(thy,sign,exn), []),
  1758 			   maxidx = maxidx,
  1769 			   maxidx = maxidx,
  1759 			   shyps = [], 
  1770 			   shyps = [], 
  1760 			   hyps = [], 
  1771 			   hyps = [], 
  1761 			   prop = prop})
  1772 			   prop = prop})
  1762             end;
  1773             end;