src/Pure/thm.ML
changeset 37309 38ce84c83738
parent 37297 a1acd424645a
child 38709 04414091f3b5
equal deleted inserted replaced
37308:6e44af45b8c5 37309:38ce84c83738
   154 end;
   154 end;
   155 
   155 
   156 structure Thm: THM =
   156 structure Thm: THM =
   157 struct
   157 struct
   158 
   158 
   159 structure Pt = Proofterm;
       
   160 
       
   161 
       
   162 (*** Certified terms and types ***)
   159 (*** Certified terms and types ***)
   163 
   160 
   164 (** certified types **)
   161 (** certified types **)
   165 
   162 
   166 abstype ctyp = Ctyp of
   163 abstype ctyp = Ctyp of
   347   hyps: term OrdList.T,                         (*hypotheses*)
   344   hyps: term OrdList.T,                         (*hypotheses*)
   348   tpairs: (term * term) list,                   (*flex-flex pairs*)
   345   tpairs: (term * term) list,                   (*flex-flex pairs*)
   349   prop: term}                                   (*conclusion*)
   346   prop: term}                                   (*conclusion*)
   350 and deriv = Deriv of
   347 and deriv = Deriv of
   351  {promises: (serial * thm future) OrdList.T,
   348  {promises: (serial * thm future) OrdList.T,
   352   body: Pt.proof_body}
   349   body: Proofterm.proof_body}
   353 with
   350 with
   354 
   351 
   355 type conv = cterm -> thm;
   352 type conv = cterm -> thm;
   356 
   353 
   357 (*attributes subsume any kind of rules or context modifiers*)
   354 (*attributes subsume any kind of rules or context modifiers*)
   484 (** derivations and promised proofs **)
   481 (** derivations and promised proofs **)
   485 
   482 
   486 fun make_deriv promises oracles thms proof =
   483 fun make_deriv promises oracles thms proof =
   487   Deriv {promises = promises, body = PBody {oracles = oracles, thms = thms, proof = proof}};
   484   Deriv {promises = promises, body = PBody {oracles = oracles, thms = thms, proof = proof}};
   488 
   485 
   489 val empty_deriv = make_deriv [] [] [] Pt.MinProof;
   486 val empty_deriv = make_deriv [] [] [] Proofterm.MinProof;
   490 
   487 
   491 
   488 
   492 (* inference rules *)
   489 (* inference rules *)
   493 
   490 
   494 fun promise_ord ((i, _), (j, _)) = int_ord (j, i);
   491 fun promise_ord ((i, _), (j, _)) = int_ord (j, i);
   496 fun deriv_rule2 f
   493 fun deriv_rule2 f
   497     (Deriv {promises = ps1, body = PBody {oracles = oras1, thms = thms1, proof = prf1}})
   494     (Deriv {promises = ps1, body = PBody {oracles = oras1, thms = thms1, proof = prf1}})
   498     (Deriv {promises = ps2, body = PBody {oracles = oras2, thms = thms2, proof = prf2}}) =
   495     (Deriv {promises = ps2, body = PBody {oracles = oras2, thms = thms2, proof = prf2}}) =
   499   let
   496   let
   500     val ps = OrdList.union promise_ord ps1 ps2;
   497     val ps = OrdList.union promise_ord ps1 ps2;
   501     val oras = Pt.merge_oracles oras1 oras2;
   498     val oras = Proofterm.merge_oracles oras1 oras2;
   502     val thms = Pt.merge_thms thms1 thms2;
   499     val thms = Proofterm.merge_thms thms1 thms2;
   503     val prf =
   500     val prf =
   504       (case ! Pt.proofs of
   501       (case ! Proofterm.proofs of
   505         2 => f prf1 prf2
   502         2 => f prf1 prf2
   506       | 1 => MinProof
   503       | 1 => MinProof
   507       | 0 => MinProof
   504       | 0 => MinProof
   508       | i => error ("Illegal level of detail for proof objects: " ^ string_of_int i));
   505       | i => error ("Illegal level of detail for proof objects: " ^ string_of_int i));
   509   in make_deriv ps oras thms prf end;
   506   in make_deriv ps oras thms prf end;
   518 (* fulfilled proofs *)
   515 (* fulfilled proofs *)
   519 
   516 
   520 fun raw_body (Thm (Deriv {body, ...}, _)) = body;
   517 fun raw_body (Thm (Deriv {body, ...}, _)) = body;
   521 
   518 
   522 fun fulfill_body (Thm (Deriv {promises, body}, {thy_ref, ...})) =
   519 fun fulfill_body (Thm (Deriv {promises, body}, {thy_ref, ...})) =
   523   Pt.fulfill_norm_proof (Theory.deref thy_ref)
   520   Proofterm.fulfill_norm_proof (Theory.deref thy_ref)
   524     (map #1 promises ~~ fulfill_bodies (map #2 promises)) body
   521     (map #1 promises ~~ fulfill_bodies (map #2 promises)) body
   525 and fulfill_bodies futures = map fulfill_body (Exn.release_all (Future.join_results futures));
   522 and fulfill_bodies futures = map fulfill_body (Exn.release_all (Future.join_results futures));
   526 
   523 
   527 val join_proofs = Pt.join_bodies o map fulfill_body;
   524 val join_proofs = Proofterm.join_bodies o map fulfill_body;
   528 
   525 
   529 fun proof_body_of thm = (Pt.join_bodies [raw_body thm]; fulfill_body thm);
   526 fun proof_body_of thm = (Proofterm.join_bodies [raw_body thm]; fulfill_body thm);
   530 val proof_of = Pt.proof_of o proof_body_of;
   527 val proof_of = Proofterm.proof_of o proof_body_of;
   531 
   528 
   532 
   529 
   533 (* derivation status *)
   530 (* derivation status *)
   534 
   531 
   535 fun status_of (Thm (Deriv {promises, body}, _)) =
   532 fun status_of (Thm (Deriv {promises, body}, _)) =
   536   let
   533   let
   537     val ps = map (Future.peek o snd) promises;
   534     val ps = map (Future.peek o snd) promises;
   538     val bodies = body ::
   535     val bodies = body ::
   539       map_filter (fn SOME (Exn.Result th) => SOME (raw_body th) | _ => NONE) ps;
   536       map_filter (fn SOME (Exn.Result th) => SOME (raw_body th) | _ => NONE) ps;
   540     val {oracle, unfinished, failed} = Pt.status_of bodies;
   537     val {oracle, unfinished, failed} = Proofterm.status_of bodies;
   541   in
   538   in
   542    {oracle = oracle,
   539    {oracle = oracle,
   543     unfinished = unfinished orelse exists is_none ps,
   540     unfinished = unfinished orelse exists is_none ps,
   544     failed = failed orelse exists (fn SOME (Exn.Exn _) => true | _ => false) ps}
   541     failed = failed orelse exists (fn SOME (Exn.Exn _) => true | _ => false) ps}
   545   end;
   542   end;
   569     val _ = T <> propT andalso raise CTERM ("future: prop expected", [ct]);
   566     val _ = T <> propT andalso raise CTERM ("future: prop expected", [ct]);
   570 
   567 
   571     val i = serial ();
   568     val i = serial ();
   572     val future = future_thm |> Future.map (future_result i thy sorts prop);
   569     val future = future_thm |> Future.map (future_result i thy sorts prop);
   573   in
   570   in
   574     Thm (make_deriv [(i, future)] [] [] (Pt.promise_proof thy i prop),
   571     Thm (make_deriv [(i, future)] [] [] (Proofterm.promise_proof thy i prop),
   575      {thy_ref = thy_ref,
   572      {thy_ref = thy_ref,
   576       tags = [],
   573       tags = [],
   577       maxidx = maxidx,
   574       maxidx = maxidx,
   578       shyps = sorts,
   575       shyps = sorts,
   579       hyps = [],
   576       hyps = [],
   583 
   580 
   584 
   581 
   585 (* closed derivations with official name *)
   582 (* closed derivations with official name *)
   586 
   583 
   587 fun derivation_name (Thm (Deriv {body, ...}, {shyps, hyps, prop, ...})) =
   584 fun derivation_name (Thm (Deriv {body, ...}, {shyps, hyps, prop, ...})) =
   588   Pt.get_name shyps hyps prop (Pt.proof_of body);
   585   Proofterm.get_name shyps hyps prop (Proofterm.proof_of body);
   589 
   586 
   590 fun name_derivation name (thm as Thm (der, args)) =
   587 fun name_derivation name (thm as Thm (der, args)) =
   591   let
   588   let
   592     val Deriv {promises, body} = der;
   589     val Deriv {promises, body} = der;
   593     val {thy_ref, shyps, hyps, prop, tpairs, ...} = args;
   590     val {thy_ref, shyps, hyps, prop, tpairs, ...} = args;
   594     val _ = null tpairs orelse raise THM ("put_name: unsolved flex-flex constraints", 0, [thm]);
   591     val _ = null tpairs orelse raise THM ("put_name: unsolved flex-flex constraints", 0, [thm]);
   595 
   592 
   596     val ps = map (apsnd (Future.map proof_body_of)) promises;
   593     val ps = map (apsnd (Future.map proof_body_of)) promises;
   597     val thy = Theory.deref thy_ref;
   594     val thy = Theory.deref thy_ref;
   598     val (pthm, proof) = Pt.thm_proof thy name shyps hyps prop ps body;
   595     val (pthm, proof) = Proofterm.thm_proof thy name shyps hyps prop ps body;
   599     val der' = make_deriv [] [] [pthm] proof;
   596     val der' = make_deriv [] [] [pthm] proof;
   600     val _ = Theory.check_thy thy;
   597     val _ = Theory.check_thy thy;
   601   in Thm (der', args) end;
   598   in Thm (der', args) end;
   602 
   599 
   603 
   600 
   608   let
   605   let
   609     fun get_ax thy =
   606     fun get_ax thy =
   610       Symtab.lookup (Theory.axiom_table thy) name
   607       Symtab.lookup (Theory.axiom_table thy) name
   611       |> Option.map (fn prop =>
   608       |> Option.map (fn prop =>
   612            let
   609            let
   613              val der = deriv_rule0 (Pt.axm_proof name prop);
   610              val der = deriv_rule0 (Proofterm.axm_proof name prop);
   614              val maxidx = maxidx_of_term prop;
   611              val maxidx = maxidx_of_term prop;
   615              val shyps = Sorts.insert_term prop [];
   612              val shyps = Sorts.insert_term prop [];
   616            in
   613            in
   617              Thm (der, {thy_ref = Theory.check_thy thy, tags = [],
   614              Thm (der, {thy_ref = Theory.check_thy thy, tags = [],
   618                maxidx = maxidx, shyps = shyps, hyps = [], tpairs = [], prop = prop})
   615                maxidx = maxidx, shyps = shyps, hyps = [], tpairs = [], prop = prop})
   638 
   635 
   639 
   636 
   640 fun norm_proof (Thm (der, args as {thy_ref, ...})) =
   637 fun norm_proof (Thm (der, args as {thy_ref, ...})) =
   641   let
   638   let
   642     val thy = Theory.deref thy_ref;
   639     val thy = Theory.deref thy_ref;
   643     val der' = deriv_rule1 (Pt.rew_proof thy) der;
   640     val der' = deriv_rule1 (Proofterm.rew_proof thy) der;
   644     val _ = Theory.check_thy thy;
   641     val _ = Theory.check_thy thy;
   645   in Thm (der', args) end;
   642   in Thm (der', args) end;
   646 
   643 
   647 fun adjust_maxidx_thm i (th as Thm (der, {thy_ref, tags, maxidx, shyps, hyps, tpairs, prop})) =
   644 fun adjust_maxidx_thm i (th as Thm (der, {thy_ref, tags, maxidx, shyps, hyps, tpairs, prop})) =
   648   if maxidx = i then th
   645   if maxidx = i then th
   664   let val Cterm {thy_ref, t = prop, T, maxidx, sorts} = adjust_maxidx_cterm ~1 raw_ct in
   661   let val Cterm {thy_ref, t = prop, T, maxidx, sorts} = adjust_maxidx_cterm ~1 raw_ct in
   665     if T <> propT then
   662     if T <> propT then
   666       raise THM ("assume: prop", 0, [])
   663       raise THM ("assume: prop", 0, [])
   667     else if maxidx <> ~1 then
   664     else if maxidx <> ~1 then
   668       raise THM ("assume: variables", maxidx, [])
   665       raise THM ("assume: variables", maxidx, [])
   669     else Thm (deriv_rule0 (Pt.Hyp prop),
   666     else Thm (deriv_rule0 (Proofterm.Hyp prop),
   670      {thy_ref = thy_ref,
   667      {thy_ref = thy_ref,
   671       tags = [],
   668       tags = [],
   672       maxidx = ~1,
   669       maxidx = ~1,
   673       shyps = sorts,
   670       shyps = sorts,
   674       hyps = [prop],
   671       hyps = [prop],
   687     (ct as Cterm {t = A, T, maxidx = maxidxA, sorts, ...})
   684     (ct as Cterm {t = A, T, maxidx = maxidxA, sorts, ...})
   688     (th as Thm (der, {maxidx, hyps, shyps, tpairs, prop, ...})) =
   685     (th as Thm (der, {maxidx, hyps, shyps, tpairs, prop, ...})) =
   689   if T <> propT then
   686   if T <> propT then
   690     raise THM ("implies_intr: assumptions must have type prop", 0, [th])
   687     raise THM ("implies_intr: assumptions must have type prop", 0, [th])
   691   else
   688   else
   692     Thm (deriv_rule1 (Pt.implies_intr_proof A) der,
   689     Thm (deriv_rule1 (Proofterm.implies_intr_proof A) der,
   693      {thy_ref = merge_thys1 ct th,
   690      {thy_ref = merge_thys1 ct th,
   694       tags = [],
   691       tags = [],
   695       maxidx = Int.max (maxidxA, maxidx),
   692       maxidx = Int.max (maxidxA, maxidx),
   696       shyps = Sorts.union sorts shyps,
   693       shyps = Sorts.union sorts shyps,
   697       hyps = remove_hyps A hyps,
   694       hyps = remove_hyps A hyps,
   712     fun err () = raise THM ("implies_elim: major premise", 0, [thAB, thA]);
   709     fun err () = raise THM ("implies_elim: major premise", 0, [thAB, thA]);
   713   in
   710   in
   714     case prop of
   711     case prop of
   715       Const ("==>", _) $ A $ B =>
   712       Const ("==>", _) $ A $ B =>
   716         if A aconv propA then
   713         if A aconv propA then
   717           Thm (deriv_rule2 (curry Pt.%%) der derA,
   714           Thm (deriv_rule2 (curry Proofterm.%%) der derA,
   718            {thy_ref = merge_thys2 thAB thA,
   715            {thy_ref = merge_thys2 thAB thA,
   719             tags = [],
   716             tags = [],
   720             maxidx = Int.max (maxA, maxidx),
   717             maxidx = Int.max (maxA, maxidx),
   721             shyps = Sorts.union shypsA shyps,
   718             shyps = Sorts.union shypsA shyps,
   722             hyps = union_hyps hypsA hyps,
   719             hyps = union_hyps hypsA hyps,
   736 fun forall_intr
   733 fun forall_intr
   737     (ct as Cterm {t = x, T, sorts, ...})
   734     (ct as Cterm {t = x, T, sorts, ...})
   738     (th as Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...})) =
   735     (th as Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...})) =
   739   let
   736   let
   740     fun result a =
   737     fun result a =
   741       Thm (deriv_rule1 (Pt.forall_intr_proof x a) der,
   738       Thm (deriv_rule1 (Proofterm.forall_intr_proof x a) der,
   742        {thy_ref = merge_thys1 ct th,
   739        {thy_ref = merge_thys1 ct th,
   743         tags = [],
   740         tags = [],
   744         maxidx = maxidx,
   741         maxidx = maxidx,
   745         shyps = Sorts.union sorts shyps,
   742         shyps = Sorts.union sorts shyps,
   746         hyps = hyps,
   743         hyps = hyps,
   768   (case prop of
   765   (case prop of
   769     Const ("all", Type ("fun", [Type ("fun", [qary, _]), _])) $ A =>
   766     Const ("all", Type ("fun", [Type ("fun", [qary, _]), _])) $ A =>
   770       if T <> qary then
   767       if T <> qary then
   771         raise THM ("forall_elim: type mismatch", 0, [th])
   768         raise THM ("forall_elim: type mismatch", 0, [th])
   772       else
   769       else
   773         Thm (deriv_rule1 (Pt.% o rpair (SOME t)) der,
   770         Thm (deriv_rule1 (Proofterm.% o rpair (SOME t)) der,
   774          {thy_ref = merge_thys1 ct th,
   771          {thy_ref = merge_thys1 ct th,
   775           tags = [],
   772           tags = [],
   776           maxidx = Int.max (maxidx, maxt),
   773           maxidx = Int.max (maxidx, maxt),
   777           shyps = Sorts.union sorts shyps,
   774           shyps = Sorts.union sorts shyps,
   778           hyps = hyps,
   775           hyps = hyps,
   785 
   782 
   786 (*Reflexivity
   783 (*Reflexivity
   787   t == t
   784   t == t
   788 *)
   785 *)
   789 fun reflexive (Cterm {thy_ref, t, T = _, maxidx, sorts}) =
   786 fun reflexive (Cterm {thy_ref, t, T = _, maxidx, sorts}) =
   790   Thm (deriv_rule0 Pt.reflexive,
   787   Thm (deriv_rule0 Proofterm.reflexive,
   791    {thy_ref = thy_ref,
   788    {thy_ref = thy_ref,
   792     tags = [],
   789     tags = [],
   793     maxidx = maxidx,
   790     maxidx = maxidx,
   794     shyps = sorts,
   791     shyps = sorts,
   795     hyps = [],
   792     hyps = [],
   802   u == t
   799   u == t
   803 *)
   800 *)
   804 fun symmetric (th as Thm (der, {thy_ref, maxidx, shyps, hyps, tpairs, prop, ...})) =
   801 fun symmetric (th as Thm (der, {thy_ref, maxidx, shyps, hyps, tpairs, prop, ...})) =
   805   (case prop of
   802   (case prop of
   806     (eq as Const ("==", _)) $ t $ u =>
   803     (eq as Const ("==", _)) $ t $ u =>
   807       Thm (deriv_rule1 Pt.symmetric der,
   804       Thm (deriv_rule1 Proofterm.symmetric der,
   808        {thy_ref = thy_ref,
   805        {thy_ref = thy_ref,
   809         tags = [],
   806         tags = [],
   810         maxidx = maxidx,
   807         maxidx = maxidx,
   811         shyps = shyps,
   808         shyps = shyps,
   812         hyps = hyps,
   809         hyps = hyps,
   829   in
   826   in
   830     case (prop1, prop2) of
   827     case (prop1, prop2) of
   831       ((eq as Const ("==", Type (_, [T, _]))) $ t1 $ u, Const ("==", _) $ u' $ t2) =>
   828       ((eq as Const ("==", Type (_, [T, _]))) $ t1 $ u, Const ("==", _) $ u' $ t2) =>
   832         if not (u aconv u') then err "middle term"
   829         if not (u aconv u') then err "middle term"
   833         else
   830         else
   834           Thm (deriv_rule2 (Pt.transitive u T) der1 der2,
   831           Thm (deriv_rule2 (Proofterm.transitive u T) der1 der2,
   835            {thy_ref = merge_thys2 th1 th2,
   832            {thy_ref = merge_thys2 th1 th2,
   836             tags = [],
   833             tags = [],
   837             maxidx = Int.max (max1, max2),
   834             maxidx = Int.max (max1, max2),
   838             shyps = Sorts.union shyps1 shyps2,
   835             shyps = Sorts.union shyps1 shyps2,
   839             hyps = union_hyps hyps1 hyps2,
   836             hyps = union_hyps hyps1 hyps2,
   851     if full then Envir.beta_norm t
   848     if full then Envir.beta_norm t
   852     else
   849     else
   853       (case t of Abs (_, _, bodt) $ u => subst_bound (u, bodt)
   850       (case t of Abs (_, _, bodt) $ u => subst_bound (u, bodt)
   854       | _ => raise THM ("beta_conversion: not a redex", 0, []));
   851       | _ => raise THM ("beta_conversion: not a redex", 0, []));
   855   in
   852   in
   856     Thm (deriv_rule0 Pt.reflexive,
   853     Thm (deriv_rule0 Proofterm.reflexive,
   857      {thy_ref = thy_ref,
   854      {thy_ref = thy_ref,
   858       tags = [],
   855       tags = [],
   859       maxidx = maxidx,
   856       maxidx = maxidx,
   860       shyps = sorts,
   857       shyps = sorts,
   861       hyps = [],
   858       hyps = [],
   862       tpairs = [],
   859       tpairs = [],
   863       prop = Logic.mk_equals (t, t')})
   860       prop = Logic.mk_equals (t, t')})
   864   end;
   861   end;
   865 
   862 
   866 fun eta_conversion (Cterm {thy_ref, t, T = _, maxidx, sorts}) =
   863 fun eta_conversion (Cterm {thy_ref, t, T = _, maxidx, sorts}) =
   867   Thm (deriv_rule0 Pt.reflexive,
   864   Thm (deriv_rule0 Proofterm.reflexive,
   868    {thy_ref = thy_ref,
   865    {thy_ref = thy_ref,
   869     tags = [],
   866     tags = [],
   870     maxidx = maxidx,
   867     maxidx = maxidx,
   871     shyps = sorts,
   868     shyps = sorts,
   872     hyps = [],
   869     hyps = [],
   873     tpairs = [],
   870     tpairs = [],
   874     prop = Logic.mk_equals (t, Envir.eta_contract t)});
   871     prop = Logic.mk_equals (t, Envir.eta_contract t)});
   875 
   872 
   876 fun eta_long_conversion (Cterm {thy_ref, t, T = _, maxidx, sorts}) =
   873 fun eta_long_conversion (Cterm {thy_ref, t, T = _, maxidx, sorts}) =
   877   Thm (deriv_rule0 Pt.reflexive,
   874   Thm (deriv_rule0 Proofterm.reflexive,
   878    {thy_ref = thy_ref,
   875    {thy_ref = thy_ref,
   879     tags = [],
   876     tags = [],
   880     maxidx = maxidx,
   877     maxidx = maxidx,
   881     shyps = sorts,
   878     shyps = sorts,
   882     hyps = [],
   879     hyps = [],
   894     (th as Thm (der, {thy_ref, maxidx, hyps, shyps, tpairs, prop, ...})) =
   891     (th as Thm (der, {thy_ref, maxidx, hyps, shyps, tpairs, prop, ...})) =
   895   let
   892   let
   896     val (t, u) = Logic.dest_equals prop
   893     val (t, u) = Logic.dest_equals prop
   897       handle TERM _ => raise THM ("abstract_rule: premise not an equality", 0, [th]);
   894       handle TERM _ => raise THM ("abstract_rule: premise not an equality", 0, [th]);
   898     val result =
   895     val result =
   899       Thm (deriv_rule1 (Pt.abstract_rule x a) der,
   896       Thm (deriv_rule1 (Proofterm.abstract_rule x a) der,
   900        {thy_ref = thy_ref,
   897        {thy_ref = thy_ref,
   901         tags = [],
   898         tags = [],
   902         maxidx = maxidx,
   899         maxidx = maxidx,
   903         shyps = Sorts.union sorts shyps,
   900         shyps = Sorts.union sorts shyps,
   904         hyps = hyps,
   901         hyps = hyps,
   937   in
   934   in
   938     case (prop1, prop2) of
   935     case (prop1, prop2) of
   939       (Const ("==", Type ("fun", [fT, _])) $ f $ g,
   936       (Const ("==", Type ("fun", [fT, _])) $ f $ g,
   940        Const ("==", Type ("fun", [tT, _])) $ t $ u) =>
   937        Const ("==", Type ("fun", [tT, _])) $ t $ u) =>
   941         (chktypes fT tT;
   938         (chktypes fT tT;
   942           Thm (deriv_rule2 (Pt.combination f g t u fT) der1 der2,
   939           Thm (deriv_rule2 (Proofterm.combination f g t u fT) der1 der2,
   943            {thy_ref = merge_thys2 th1 th2,
   940            {thy_ref = merge_thys2 th1 th2,
   944             tags = [],
   941             tags = [],
   945             maxidx = Int.max (max1, max2),
   942             maxidx = Int.max (max1, max2),
   946             shyps = Sorts.union shyps1 shyps2,
   943             shyps = Sorts.union shyps1 shyps2,
   947             hyps = union_hyps hyps1 hyps2,
   944             hyps = union_hyps hyps1 hyps2,
   964     fun err msg = raise THM ("equal_intr: " ^ msg, 0, [th1, th2]);
   961     fun err msg = raise THM ("equal_intr: " ^ msg, 0, [th1, th2]);
   965   in
   962   in
   966     case (prop1, prop2) of
   963     case (prop1, prop2) of
   967       (Const("==>", _) $ A $ B, Const("==>", _) $ B' $ A') =>
   964       (Const("==>", _) $ A $ B, Const("==>", _) $ B' $ A') =>
   968         if A aconv A' andalso B aconv B' then
   965         if A aconv A' andalso B aconv B' then
   969           Thm (deriv_rule2 (Pt.equal_intr A B) der1 der2,
   966           Thm (deriv_rule2 (Proofterm.equal_intr A B) der1 der2,
   970            {thy_ref = merge_thys2 th1 th2,
   967            {thy_ref = merge_thys2 th1 th2,
   971             tags = [],
   968             tags = [],
   972             maxidx = Int.max (max1, max2),
   969             maxidx = Int.max (max1, max2),
   973             shyps = Sorts.union shyps1 shyps2,
   970             shyps = Sorts.union shyps1 shyps2,
   974             hyps = union_hyps hyps1 hyps2,
   971             hyps = union_hyps hyps1 hyps2,
   992     fun err msg = raise THM ("equal_elim: " ^ msg, 0, [th1, th2]);
   989     fun err msg = raise THM ("equal_elim: " ^ msg, 0, [th1, th2]);
   993   in
   990   in
   994     case prop1 of
   991     case prop1 of
   995       Const ("==", _) $ A $ B =>
   992       Const ("==", _) $ A $ B =>
   996         if prop2 aconv A then
   993         if prop2 aconv A then
   997           Thm (deriv_rule2 (Pt.equal_elim A B) der1 der2,
   994           Thm (deriv_rule2 (Proofterm.equal_elim A B) der1 der2,
   998            {thy_ref = merge_thys2 th1 th2,
   995            {thy_ref = merge_thys2 th1 th2,
   999             tags = [],
   996             tags = [],
  1000             maxidx = Int.max (max1, max2),
   997             maxidx = Int.max (max1, max2),
  1001             shyps = Sorts.union shyps1 shyps2,
   998             shyps = Sorts.union shyps1 shyps2,
  1002             hyps = union_hyps hyps1 hyps2,
   999             hyps = union_hyps hyps1 hyps2,
  1022         else
  1019         else
  1023           let
  1020           let
  1024             val tpairs' = tpairs |> map (pairself (Envir.norm_term env))
  1021             val tpairs' = tpairs |> map (pairself (Envir.norm_term env))
  1025               (*remove trivial tpairs, of the form t==t*)
  1022               (*remove trivial tpairs, of the form t==t*)
  1026               |> filter_out (op aconv);
  1023               |> filter_out (op aconv);
  1027             val der' = deriv_rule1 (Pt.norm_proof' env) der;
  1024             val der' = deriv_rule1 (Proofterm.norm_proof' env) der;
  1028             val prop' = Envir.norm_term env prop;
  1025             val prop' = Envir.norm_term env prop;
  1029             val maxidx = maxidx_tpairs tpairs' (maxidx_of_term prop');
  1026             val maxidx = maxidx_tpairs tpairs' (maxidx_of_term prop');
  1030             val shyps = Envir.insert_sorts env shyps;
  1027             val shyps = Envir.insert_sorts env shyps;
  1031           in
  1028           in
  1032             Thm (der', {thy_ref = Theory.check_thy thy, tags = [], maxidx = maxidx,
  1029             Thm (der', {thy_ref = Theory.check_thy thy, tags = [], maxidx = maxidx,
  1062         val gen = Term_Subst.generalize (tfrees, frees) idx;
  1059         val gen = Term_Subst.generalize (tfrees, frees) idx;
  1063         val prop' = gen prop;
  1060         val prop' = gen prop;
  1064         val tpairs' = map (pairself gen) tpairs;
  1061         val tpairs' = map (pairself gen) tpairs;
  1065         val maxidx' = maxidx_tpairs tpairs' (maxidx_of_term prop');
  1062         val maxidx' = maxidx_tpairs tpairs' (maxidx_of_term prop');
  1066       in
  1063       in
  1067         Thm (deriv_rule1 (Pt.generalize (tfrees, frees) idx) der,
  1064         Thm (deriv_rule1 (Proofterm.generalize (tfrees, frees) idx) der,
  1068          {thy_ref = thy_ref,
  1065          {thy_ref = thy_ref,
  1069           tags = [],
  1066           tags = [],
  1070           maxidx = maxidx',
  1067           maxidx = maxidx',
  1071           shyps = shyps,
  1068           shyps = shyps,
  1072           hyps = hyps,
  1069           hyps = hyps,
  1133         val subst = Term_Subst.instantiate_maxidx (instT', inst');
  1130         val subst = Term_Subst.instantiate_maxidx (instT', inst');
  1134         val (prop', maxidx1) = subst prop ~1;
  1131         val (prop', maxidx1) = subst prop ~1;
  1135         val (tpairs', maxidx') =
  1132         val (tpairs', maxidx') =
  1136           fold_map (fn (t, u) => fn i => subst t i ||>> subst u) tpairs maxidx1;
  1133           fold_map (fn (t, u) => fn i => subst t i ||>> subst u) tpairs maxidx1;
  1137       in
  1134       in
  1138         Thm (deriv_rule1 (fn d => Pt.instantiate (map (apsnd #1) instT', map (apsnd #1) inst') d) der,
  1135         Thm (deriv_rule1
       
  1136           (fn d => Proofterm.instantiate (map (apsnd #1) instT', map (apsnd #1) inst') d) der,
  1139          {thy_ref = thy_ref',
  1137          {thy_ref = thy_ref',
  1140           tags = [],
  1138           tags = [],
  1141           maxidx = maxidx',
  1139           maxidx = maxidx',
  1142           shyps = shyps',
  1140           shyps = shyps',
  1143           hyps = hyps,
  1141           hyps = hyps,
  1166   A can contain Vars, not so for assume!*)
  1164   A can contain Vars, not so for assume!*)
  1167 fun trivial (Cterm {thy_ref, t =A, T, maxidx, sorts}) =
  1165 fun trivial (Cterm {thy_ref, t =A, T, maxidx, sorts}) =
  1168   if T <> propT then
  1166   if T <> propT then
  1169     raise THM ("trivial: the term must have type prop", 0, [])
  1167     raise THM ("trivial: the term must have type prop", 0, [])
  1170   else
  1168   else
  1171     Thm (deriv_rule0 (Pt.AbsP ("H", NONE, Pt.PBound 0)),
  1169     Thm (deriv_rule0 (Proofterm.AbsP ("H", NONE, Proofterm.PBound 0)),
  1172      {thy_ref = thy_ref,
  1170      {thy_ref = thy_ref,
  1173       tags = [],
  1171       tags = [],
  1174       maxidx = maxidx,
  1172       maxidx = maxidx,
  1175       shyps = sorts,
  1173       shyps = sorts,
  1176       hyps = [],
  1174       hyps = [],
  1188     val thy = Theory.deref thy_ref;
  1186     val thy = Theory.deref thy_ref;
  1189     val c = Sign.certify_class thy raw_c;
  1187     val c = Sign.certify_class thy raw_c;
  1190     val Cterm {t = prop, maxidx, sorts, ...} = cterm_of thy (Logic.mk_of_class (T, c));
  1188     val Cterm {t = prop, maxidx, sorts, ...} = cterm_of thy (Logic.mk_of_class (T, c));
  1191   in
  1189   in
  1192     if Sign.of_sort thy (T, [c]) then
  1190     if Sign.of_sort thy (T, [c]) then
  1193       Thm (deriv_rule0 (Pt.OfClass (T, c)),
  1191       Thm (deriv_rule0 (Proofterm.OfClass (T, c)),
  1194        {thy_ref = Theory.check_thy thy,
  1192        {thy_ref = Theory.check_thy thy,
  1195         tags = [],
  1193         tags = [],
  1196         maxidx = maxidx,
  1194         maxidx = maxidx,
  1197         shyps = sorts,
  1195         shyps = sorts,
  1198         hyps = [],
  1196         hyps = [],
  1213         val witnessed = Sign.witness_sorts thy present extra;
  1211         val witnessed = Sign.witness_sorts thy present extra;
  1214         val extra' = fold (Sorts.remove_sort o #2) witnessed extra
  1212         val extra' = fold (Sorts.remove_sort o #2) witnessed extra
  1215           |> Sorts.minimal_sorts algebra;
  1213           |> Sorts.minimal_sorts algebra;
  1216         val shyps' = fold (Sorts.insert_sort o #2) present extra';
  1214         val shyps' = fold (Sorts.insert_sort o #2) present extra';
  1217       in
  1215       in
  1218         Thm (deriv_rule_unconditional (Pt.strip_shyps_proof algebra present witnessed extra') der,
  1216         Thm (deriv_rule_unconditional
       
  1217           (Proofterm.strip_shyps_proof algebra present witnessed extra') der,
  1219          {thy_ref = Theory.check_thy thy, tags = tags, maxidx = maxidx,
  1218          {thy_ref = Theory.check_thy thy, tags = tags, maxidx = maxidx,
  1220           shyps = shyps', hyps = hyps, tpairs = tpairs, prop = prop})
  1219           shyps = shyps', hyps = hyps, tpairs = tpairs, prop = prop})
  1221       end;
  1220       end;
  1222 
  1221 
  1223 (*Internalize sort constraints of type variables*)
  1222 (*Internalize sort constraints of type variables*)
  1232     val tfrees = rev (Term.add_tfree_names prop []);
  1231     val tfrees = rev (Term.add_tfree_names prop []);
  1233     val _ = null tfrees orelse err ("illegal free type variables " ^ commas_quote tfrees);
  1232     val _ = null tfrees orelse err ("illegal free type variables " ^ commas_quote tfrees);
  1234 
  1233 
  1235     val ps = map (apsnd (Future.map proof_body_of)) promises;
  1234     val ps = map (apsnd (Future.map proof_body_of)) promises;
  1236     val thy = Theory.deref thy_ref;
  1235     val thy = Theory.deref thy_ref;
  1237     val (pthm as (_, (_, prop', _)), proof) = Pt.unconstrain_thm_proof thy shyps prop ps body;
  1236     val (pthm as (_, (_, prop', _)), proof) =
       
  1237       Proofterm.unconstrain_thm_proof thy shyps prop ps body;
  1238     val der' = make_deriv [] [] [pthm] proof;
  1238     val der' = make_deriv [] [] [pthm] proof;
  1239     val _ = Theory.check_thy thy;
  1239     val _ = Theory.check_thy thy;
  1240   in
  1240   in
  1241     Thm (der',
  1241     Thm (der',
  1242      {thy_ref = thy_ref,
  1242      {thy_ref = thy_ref,
  1254     val tfrees = fold Term.add_tfrees hyps fixed;
  1254     val tfrees = fold Term.add_tfrees hyps fixed;
  1255     val prop1 = attach_tpairs tpairs prop;
  1255     val prop1 = attach_tpairs tpairs prop;
  1256     val (al, prop2) = Type.varify_global tfrees prop1;
  1256     val (al, prop2) = Type.varify_global tfrees prop1;
  1257     val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
  1257     val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
  1258   in
  1258   in
  1259     (al, Thm (deriv_rule1 (Pt.varify_proof prop tfrees) der,
  1259     (al, Thm (deriv_rule1 (Proofterm.varify_proof prop tfrees) der,
  1260      {thy_ref = thy_ref,
  1260      {thy_ref = thy_ref,
  1261       tags = [],
  1261       tags = [],
  1262       maxidx = Int.max (0, maxidx),
  1262       maxidx = Int.max (0, maxidx),
  1263       shyps = shyps,
  1263       shyps = shyps,
  1264       hyps = hyps,
  1264       hyps = hyps,
  1273   let
  1273   let
  1274     val prop1 = attach_tpairs tpairs prop;
  1274     val prop1 = attach_tpairs tpairs prop;
  1275     val prop2 = Type.legacy_freeze prop1;
  1275     val prop2 = Type.legacy_freeze prop1;
  1276     val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
  1276     val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
  1277   in
  1277   in
  1278     Thm (deriv_rule1 (Pt.legacy_freezeT prop1) der,
  1278     Thm (deriv_rule1 (Proofterm.legacy_freezeT prop1) der,
  1279      {thy_ref = thy_ref,
  1279      {thy_ref = thy_ref,
  1280       tags = [],
  1280       tags = [],
  1281       maxidx = maxidx_of_term prop2,
  1281       maxidx = maxidx_of_term prop2,
  1282       shyps = shyps,
  1282       shyps = shyps,
  1283       hyps = hyps,
  1283       hyps = hyps,
  1306     val Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...}) = orule;
  1306     val Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...}) = orule;
  1307     val (As, B) = Logic.strip_horn prop;
  1307     val (As, B) = Logic.strip_horn prop;
  1308   in
  1308   in
  1309     if T <> propT then raise THM ("lift_rule: the term must have type prop", 0, [])
  1309     if T <> propT then raise THM ("lift_rule: the term must have type prop", 0, [])
  1310     else
  1310     else
  1311       Thm (deriv_rule1 (Pt.lift_proof gprop inc prop) der,
  1311       Thm (deriv_rule1 (Proofterm.lift_proof gprop inc prop) der,
  1312        {thy_ref = merge_thys1 goal orule,
  1312        {thy_ref = merge_thys1 goal orule,
  1313         tags = [],
  1313         tags = [],
  1314         maxidx = maxidx + inc,
  1314         maxidx = maxidx + inc,
  1315         shyps = Sorts.union shyps sorts,  (*sic!*)
  1315         shyps = Sorts.union shyps sorts,  (*sic!*)
  1316         hyps = hyps,
  1316         hyps = hyps,
  1320 
  1320 
  1321 fun incr_indexes i (thm as Thm (der, {thy_ref, maxidx, shyps, hyps, tpairs, prop, ...})) =
  1321 fun incr_indexes i (thm as Thm (der, {thy_ref, maxidx, shyps, hyps, tpairs, prop, ...})) =
  1322   if i < 0 then raise THM ("negative increment", 0, [thm])
  1322   if i < 0 then raise THM ("negative increment", 0, [thm])
  1323   else if i = 0 then thm
  1323   else if i = 0 then thm
  1324   else
  1324   else
  1325     Thm (deriv_rule1 (Pt.incr_indexes i) der,
  1325     Thm (deriv_rule1 (Proofterm.incr_indexes i) der,
  1326      {thy_ref = thy_ref,
  1326      {thy_ref = thy_ref,
  1327       tags = [],
  1327       tags = [],
  1328       maxidx = maxidx + i,
  1328       maxidx = maxidx + i,
  1329       shyps = shyps,
  1329       shyps = shyps,
  1330       hyps = hyps,
  1330       hyps = hyps,
  1337     val Thm (der, {thy_ref, maxidx, shyps, hyps, ...}) = state;
  1337     val Thm (der, {thy_ref, maxidx, shyps, hyps, ...}) = state;
  1338     val thy = Theory.deref thy_ref;
  1338     val thy = Theory.deref thy_ref;
  1339     val (tpairs, Bs, Bi, C) = dest_state (state, i);
  1339     val (tpairs, Bs, Bi, C) = dest_state (state, i);
  1340     fun newth n (env, tpairs) =
  1340     fun newth n (env, tpairs) =
  1341       Thm (deriv_rule1
  1341       Thm (deriv_rule1
  1342           ((if Envir.is_empty env then I else (Pt.norm_proof' env)) o
  1342           ((if Envir.is_empty env then I else (Proofterm.norm_proof' env)) o
  1343             Pt.assumption_proof Bs Bi n) der,
  1343             Proofterm.assumption_proof Bs Bi n) der,
  1344        {tags = [],
  1344        {tags = [],
  1345         maxidx = Envir.maxidx_of env,
  1345         maxidx = Envir.maxidx_of env,
  1346         shyps = Envir.insert_sorts env shyps,
  1346         shyps = Envir.insert_sorts env shyps,
  1347         hyps = hyps,
  1347         hyps = hyps,
  1348         tpairs =
  1348         tpairs =
  1375     val (_, asms, concl) = Logic.assum_problems (~1, Bi);
  1375     val (_, asms, concl) = Logic.assum_problems (~1, Bi);
  1376   in
  1376   in
  1377     (case find_index (fn asm => Pattern.aeconv (asm, concl)) asms of
  1377     (case find_index (fn asm => Pattern.aeconv (asm, concl)) asms of
  1378       ~1 => raise THM ("eq_assumption", 0, [state])
  1378       ~1 => raise THM ("eq_assumption", 0, [state])
  1379     | n =>
  1379     | n =>
  1380         Thm (deriv_rule1 (Pt.assumption_proof Bs Bi (n + 1)) der,
  1380         Thm (deriv_rule1 (Proofterm.assumption_proof Bs Bi (n + 1)) der,
  1381          {thy_ref = thy_ref,
  1381          {thy_ref = thy_ref,
  1382           tags = [],
  1382           tags = [],
  1383           maxidx = maxidx,
  1383           maxidx = maxidx,
  1384           shyps = shyps,
  1384           shyps = shyps,
  1385           hyps = hyps,
  1385           hyps = hyps,
  1404       else if 0 < m andalso m < n then
  1404       else if 0 < m andalso m < n then
  1405         let val (ps, qs) = chop m asms
  1405         let val (ps, qs) = chop m asms
  1406         in list_all (params, Logic.list_implies (qs @ ps, concl)) end
  1406         in list_all (params, Logic.list_implies (qs @ ps, concl)) end
  1407       else raise THM ("rotate_rule", k, [state]);
  1407       else raise THM ("rotate_rule", k, [state]);
  1408   in
  1408   in
  1409     Thm (deriv_rule1 (Pt.rotate_proof Bs Bi m) der,
  1409     Thm (deriv_rule1 (Proofterm.rotate_proof Bs Bi m) der,
  1410      {thy_ref = thy_ref,
  1410      {thy_ref = thy_ref,
  1411       tags = [],
  1411       tags = [],
  1412       maxidx = maxidx,
  1412       maxidx = maxidx,
  1413       shyps = shyps,
  1413       shyps = shyps,
  1414       hyps = hyps,
  1414       hyps = hyps,
  1435       else if 0 < m andalso m < n_j then
  1435       else if 0 < m andalso m < n_j then
  1436         let val (ps, qs) = chop m moved_prems
  1436         let val (ps, qs) = chop m moved_prems
  1437         in Logic.list_implies (fixed_prems @ qs @ ps, concl) end
  1437         in Logic.list_implies (fixed_prems @ qs @ ps, concl) end
  1438       else raise THM ("permute_prems: k", k, [rl]);
  1438       else raise THM ("permute_prems: k", k, [rl]);
  1439   in
  1439   in
  1440     Thm (deriv_rule1 (Pt.permute_prems_proof prems j m) der,
  1440     Thm (deriv_rule1 (Proofterm.permute_prems_proof prems j m) der,
  1441      {thy_ref = thy_ref,
  1441      {thy_ref = thy_ref,
  1442       tags = [],
  1442       tags = [],
  1443       maxidx = maxidx,
  1443       maxidx = maxidx,
  1444       shyps = shyps,
  1444       shyps = shyps,
  1445       hyps = hyps,
  1445       hyps = hyps,
  1603              end
  1603              end
  1604            val th =
  1604            val th =
  1605              Thm (deriv_rule2
  1605              Thm (deriv_rule2
  1606                    ((if Envir.is_empty env then I
  1606                    ((if Envir.is_empty env then I
  1607                      else if Envir.above env smax then
  1607                      else if Envir.above env smax then
  1608                        (fn f => fn der => f (Pt.norm_proof' env der))
  1608                        (fn f => fn der => f (Proofterm.norm_proof' env der))
  1609                      else
  1609                      else
  1610                        curry op oo (Pt.norm_proof' env))
  1610                        curry op oo (Proofterm.norm_proof' env))
  1611                     (Pt.bicompose_proof flatten Bs oldAs As A n (nlift+1))) rder' sder,
  1611                     (Proofterm.bicompose_proof flatten Bs oldAs As A n (nlift+1))) rder' sder,
  1612                 {tags = [],
  1612                 {tags = [],
  1613                  maxidx = Envir.maxidx_of env,
  1613                  maxidx = Envir.maxidx_of env,
  1614                  shyps = Envir.insert_sorts env (Sorts.union rshyps sshyps),
  1614                  shyps = Envir.insert_sorts env (Sorts.union rshyps sshyps),
  1615                  hyps = union_hyps rhyps shyps,
  1615                  hyps = union_hyps rhyps shyps,
  1616                  tpairs = ntpairs,
  1616                  tpairs = ntpairs,
  1622      (*Modify assumptions, deleting n-th if n>0 for e-resolution*)
  1622      (*Modify assumptions, deleting n-th if n>0 for e-resolution*)
  1623      fun newAs(As0, n, dpairs, tpairs) =
  1623      fun newAs(As0, n, dpairs, tpairs) =
  1624        let val (As1, rder') =
  1624        let val (As1, rder') =
  1625          if not lifted then (As0, rder)
  1625          if not lifted then (As0, rder)
  1626          else (map (rename_bvars(dpairs,tpairs,B)) As0,
  1626          else (map (rename_bvars(dpairs,tpairs,B)) As0,
  1627            deriv_rule1 (Pt.map_proof_terms
  1627            deriv_rule1 (Proofterm.map_proof_terms
  1628              (rename_bvars (dpairs, tpairs, Bound 0)) I) rder);
  1628              (rename_bvars (dpairs, tpairs, Bound 0)) I) rder);
  1629        in (map (if flatten then (Logic.flatten_params n) else I) As1, As1, rder', n)
  1629        in (map (if flatten then (Logic.flatten_params n) else I) As1, As1, rder', n)
  1630           handle TERM _ =>
  1630           handle TERM _ =>
  1631           raise THM("bicompose: 1st premise", 0, [orule])
  1631           raise THM("bicompose: 1st premise", 0, [orule])
  1632        end;
  1632        end;
  1709 fun invoke_oracle thy_ref1 name oracle arg =
  1709 fun invoke_oracle thy_ref1 name oracle arg =
  1710   let val Cterm {thy_ref = thy_ref2, t = prop, T, maxidx, sorts} = oracle arg in
  1710   let val Cterm {thy_ref = thy_ref2, t = prop, T, maxidx, sorts} = oracle arg in
  1711     if T <> propT then
  1711     if T <> propT then
  1712       raise THM ("Oracle's result must have type prop: " ^ name, 0, [])
  1712       raise THM ("Oracle's result must have type prop: " ^ name, 0, [])
  1713     else
  1713     else
  1714       let val (ora, prf) = Pt.oracle_proof name prop in
  1714       let val (ora, prf) = Proofterm.oracle_proof name prop in
  1715         Thm (make_deriv [] [ora] [] prf,
  1715         Thm (make_deriv [] [ora] [] prf,
  1716          {thy_ref = Theory.merge_refs (thy_ref1, thy_ref2),
  1716          {thy_ref = Theory.merge_refs (thy_ref1, thy_ref2),
  1717           tags = [],
  1717           tags = [],
  1718           maxidx = maxidx,
  1718           maxidx = maxidx,
  1719           shyps = sorts,
  1719           shyps = sorts,