src/Pure/thm.ML
changeset 16656 18b0cb22057d
parent 16601 ee8eefade568
child 16679 abd1461fa288
equal deleted inserted replaced
16655:3e4d726aaed1 16656:18b0cb22057d
     9 
     9 
    10 signature BASIC_THM =
    10 signature BASIC_THM =
    11   sig
    11   sig
    12   (*certified types*)
    12   (*certified types*)
    13   type ctyp
    13   type ctyp
    14   val rep_ctyp: ctyp -> {thy: theory, sign: theory, T: typ}
    14   val rep_ctyp: ctyp ->
       
    15    {thy: theory,
       
    16     sign: theory,       (*obsolete*)
       
    17     T: typ,
       
    18     sorts: sort list}
    15   val theory_of_ctyp: ctyp -> theory
    19   val theory_of_ctyp: ctyp -> theory
    16   val typ_of: ctyp -> typ
    20   val typ_of: ctyp -> typ
    17   val ctyp_of: theory -> typ -> ctyp
    21   val ctyp_of: theory -> typ -> ctyp
    18   val read_ctyp: theory -> string -> ctyp
    22   val read_ctyp: theory -> string -> ctyp
    19 
    23 
    20   (*certified terms*)
    24   (*certified terms*)
    21   type cterm
    25   type cterm
    22   exception CTERM of string
    26   exception CTERM of string
    23   val rep_cterm: cterm ->
    27   val rep_cterm: cterm ->
    24     {thy: theory, sign: theory, t: term, T: typ, maxidx: int, sorts: sort list}
    28    {thy: theory,
       
    29     sign: theory,       (*obsolete*)
       
    30     t: term,
       
    31     T: typ,
       
    32     maxidx: int,
       
    33     sorts: sort list}
    25   val crep_cterm: cterm ->
    34   val crep_cterm: cterm ->
    26     {thy: theory, sign: theory, t: term, T: ctyp, maxidx: int, sorts: sort list}
    35     {thy: theory, sign: theory, t: term, T: ctyp, maxidx: int, sorts: sort list}
    27   val theory_of_cterm: cterm -> theory
    36   val theory_of_cterm: cterm -> theory
    28   val sign_of_cterm: cterm -> theory    (*obsolete*)
    37   val sign_of_cterm: cterm -> theory    (*obsolete*)
    29   val term_of: cterm -> term
    38   val term_of: cterm -> term
    42   type tag              (* = string * string list *)
    51   type tag              (* = string * string list *)
    43 
    52 
    44   (*meta theorems*)
    53   (*meta theorems*)
    45   type thm
    54   type thm
    46   val rep_thm: thm ->
    55   val rep_thm: thm ->
    47    {thy: theory, sign: theory,
    56    {thy: theory,
       
    57     sign: theory,       (*obsolete*)
    48     der: bool * Proofterm.proof,
    58     der: bool * Proofterm.proof,
    49     maxidx: int,
    59     maxidx: int,
    50     shyps: sort list,
    60     shyps: sort list,
    51     hyps: term list,
    61     hyps: term list,
    52     tpairs: (term * term) list,
    62     tpairs: (term * term) list,
    53     prop: term}
    63     prop: term}
    54   val crep_thm: thm ->
    64   val crep_thm: thm ->
    55    {thy: theory, sign: theory,
    65    {thy: theory,
       
    66     sign: theory,       (*obsolete*)
    56     der: bool * Proofterm.proof,
    67     der: bool * Proofterm.proof,
    57     maxidx: int,
    68     maxidx: int,
    58     shyps: sort list,
    69     shyps: sort list,
    59     hyps: cterm list,
    70     hyps: cterm list,
    60     tpairs: (cterm * cterm) list,
    71     tpairs: (cterm * cterm) list,
    65   val eq_thms: thm list * thm list -> bool
    76   val eq_thms: thm list * thm list -> bool
    66   val theory_of_thm: thm -> theory
    77   val theory_of_thm: thm -> theory
    67   val sign_of_thm: thm -> theory    (*obsolete*)
    78   val sign_of_thm: thm -> theory    (*obsolete*)
    68   val prop_of: thm -> term
    79   val prop_of: thm -> term
    69   val proof_of: thm -> Proofterm.proof
    80   val proof_of: thm -> Proofterm.proof
    70   val transfer: theory -> thm -> thm
       
    71   val tpairs_of: thm -> (term * term) list
    81   val tpairs_of: thm -> (term * term) list
       
    82   val concl_of: thm -> term
    72   val prems_of: thm -> term list
    83   val prems_of: thm -> term list
    73   val nprems_of: thm -> int
    84   val nprems_of: thm -> int
    74   val concl_of: thm -> term
       
    75   val cprop_of: thm -> cterm
    85   val cprop_of: thm -> cterm
       
    86   val transfer: theory -> thm -> thm
    76   val extra_shyps: thm -> sort list
    87   val extra_shyps: thm -> sort list
    77   val strip_shyps: thm -> thm
    88   val strip_shyps: thm -> thm
    78   val get_axiom_i: theory -> string -> thm
    89   val get_axiom_i: theory -> string -> thm
    79   val get_axiom: theory -> xstring -> thm
    90   val get_axiom: theory -> xstring -> thm
    80   val def_name: string -> string
    91   val def_name: string -> string
   144 end;
   155 end;
   145 
   156 
   146 structure Thm: THM =
   157 structure Thm: THM =
   147 struct
   158 struct
   148 
   159 
       
   160 
   149 (*** Certified terms and types ***)
   161 (*** Certified terms and types ***)
   150 
   162 
       
   163 (** collect occurrences of sorts -- unless all sorts non-empty **)
       
   164 
       
   165 fun may_insert_typ_sorts thy T =
       
   166   if Sign.all_sorts_nonempty thy then I
       
   167   else Sorts.insert_typ T;
       
   168 
       
   169 fun may_insert_term_sorts thy t =
       
   170   if Sign.all_sorts_nonempty thy then I
       
   171   else Sorts.insert_term t;
       
   172 
       
   173 (*NB: type unification may invent new sorts*)
       
   174 fun may_insert_env_sorts thy (env as Envir.Envir {iTs, ...}) =
       
   175   if Sign.all_sorts_nonempty thy then I
       
   176   else Vartab.fold (fn (_, (_, T)) => Sorts.insert_typ T) iTs;
       
   177 
       
   178 
       
   179 
   151 (** certified types **)
   180 (** certified types **)
   152 
   181 
   153 datatype ctyp = Ctyp of {thy_ref: theory_ref, T: typ};
   182 datatype ctyp = Ctyp of {thy_ref: theory_ref, T: typ, sorts: sort list};
   154 
   183 
   155 fun rep_ctyp (Ctyp {thy_ref, T}) =
   184 fun rep_ctyp (Ctyp {thy_ref, T, sorts}) =
   156   let val thy = Theory.deref thy_ref
   185   let val thy = Theory.deref thy_ref
   157   in {thy = thy, sign = thy, T = T} end;
   186   in {thy = thy, sign = thy, T = T, sorts = sorts} end;
   158 
   187 
   159 val theory_of_ctyp = #thy o rep_ctyp;
   188 fun theory_of_ctyp (Ctyp {thy_ref, ...}) = Theory.deref thy_ref;
   160 
   189 
   161 fun typ_of (Ctyp {T, ...}) = T;
   190 fun typ_of (Ctyp {T, ...}) = T;
   162 
   191 
   163 fun ctyp_of thy T =
   192 fun ctyp_of thy raw_T =
   164   Ctyp {thy_ref = Theory.self_ref thy, T = Sign.certify_typ thy T};
   193   let val T = Sign.certify_typ thy raw_T
       
   194   in Ctyp {thy_ref = Theory.self_ref thy, T = T, sorts = may_insert_typ_sorts thy T []} end;
   165 
   195 
   166 fun read_ctyp thy s =
   196 fun read_ctyp thy s =
   167   Ctyp {thy_ref = Theory.self_ref thy, T = Sign.read_typ (thy, K NONE) s};
   197   let val T = Sign.read_typ (thy, K NONE) s
   168 
   198   in Ctyp {thy_ref = Theory.self_ref thy, T = T, sorts = may_insert_typ_sorts thy T []} end;
   169 fun dest_ctyp (Ctyp {thy_ref, T = Type (s, Ts)}) =
   199 
   170       map (fn T => Ctyp {thy_ref = thy_ref, T = T}) Ts
   200 fun dest_ctyp (Ctyp {thy_ref, T = Type (s, Ts), sorts}) =
       
   201       map (fn T => Ctyp {thy_ref = thy_ref, T = T, sorts = sorts}) Ts
   171   | dest_ctyp cT = [cT];
   202   | dest_ctyp cT = [cT];
   172 
   203 
   173 
   204 
   174 
   205 
   175 (** certified terms **)
   206 (** certified terms **)
   186   let val thy =  Theory.deref thy_ref
   217   let val thy =  Theory.deref thy_ref
   187   in {thy = thy, sign = thy, t = t, T = T, maxidx = maxidx, sorts = sorts} end;
   218   in {thy = thy, sign = thy, t = t, T = T, maxidx = maxidx, sorts = sorts} end;
   188 
   219 
   189 fun crep_cterm (Cterm {thy_ref, t, T, maxidx, sorts}) =
   220 fun crep_cterm (Cterm {thy_ref, t, T, maxidx, sorts}) =
   190   let val thy = Theory.deref thy_ref in
   221   let val thy = Theory.deref thy_ref in
   191    {thy = thy, sign = thy, t = t, T = Ctyp {thy_ref = thy_ref, T = T},
   222    {thy = thy, sign = thy, t = t, T = Ctyp {thy_ref = thy_ref, T = T, sorts = sorts},
   192     maxidx = maxidx, sorts = sorts}
   223     maxidx = maxidx, sorts = sorts}
   193   end;
   224   end;
   194 
   225 
   195 fun theory_of_cterm (Cterm {thy_ref, ...}) = Theory.deref thy_ref;
   226 fun theory_of_cterm (Cterm {thy_ref, ...}) = Theory.deref thy_ref;
   196 val sign_of_cterm = theory_of_cterm;
   227 val sign_of_cterm = theory_of_cterm;
   197 
   228 
   198 fun term_of (Cterm {t, ...}) = t;
   229 fun term_of (Cterm {t, ...}) = t;
   199 
   230 
   200 fun ctyp_of_term (Cterm {thy_ref, T, ...}) = Ctyp {thy_ref = thy_ref, T = T};
   231 fun ctyp_of_term (Cterm {thy_ref, T, sorts, ...}) =
       
   232   Ctyp {thy_ref = thy_ref, T = T, sorts = sorts};
   201 
   233 
   202 fun cterm_of thy tm =
   234 fun cterm_of thy tm =
   203   let
   235   let
   204     val (t, T, maxidx) = Sign.certify_term (Sign.pp thy) thy tm;
   236     val (t, T, maxidx) = Sign.certify_term (Sign.pp thy) thy tm;
   205     val sorts = Sorts.insert_term t [];
   237     val sorts = may_insert_term_sorts thy t [];
   206   in Cterm {thy_ref = Theory.self_ref thy, t = t, T = T, maxidx = maxidx, sorts = sorts} end;
   238   in Cterm {thy_ref = Theory.self_ref thy, t = t, T = T, maxidx = maxidx, sorts = sorts} end;
       
   239 
       
   240 fun merge_thys0 (Cterm {thy_ref = r1, ...}) (Cterm {thy_ref = r2, ...}) =
       
   241   Theory.merge_refs (r1, r2);
   207 
   242 
   208 exception CTERM of string;
   243 exception CTERM of string;
   209 
   244 
   210 (*Destruct application in cterms*)
   245 (*Destruct application in cterms*)
   211 fun dest_comb (Cterm {thy_ref, T, maxidx, sorts, t = A $ B}) =
   246 fun dest_comb (Cterm {thy_ref, T, maxidx, sorts, t = A $ B}) =
   212       let val typeA = fastype_of A;
   247       let
   213           val typeB =
   248         val typeA = fastype_of A;
   214             case typeA of Type("fun",[S,T]) => S
   249         val typeB =
   215                         | _ => sys_error "Function type expected in dest_comb";
   250           (case typeA of Type ("fun", [S, T]) => S
       
   251           | _ => sys_error "Function type expected in dest_comb");
   216       in
   252       in
   217       (Cterm {thy_ref=thy_ref, maxidx=maxidx, sorts=sorts, t=A, T=typeA},
   253         (Cterm {t = A, T = typeA, thy_ref = thy_ref, maxidx = maxidx, sorts = sorts},
   218        Cterm {thy_ref=thy_ref, maxidx=maxidx, sorts=sorts, t=B, T=typeB})
   254          Cterm {t = B, T = typeB, thy_ref = thy_ref, maxidx = maxidx, sorts = sorts})
   219       end
   255       end
   220   | dest_comb _ = raise CTERM "dest_comb";
   256   | dest_comb _ = raise CTERM "dest_comb";
   221 
   257 
   222 (*Destruct abstraction in cterms*)
   258 (*Destruct abstraction in cterms*)
   223 fun dest_abs a (Cterm {thy_ref, T as Type("fun",[_,S]), maxidx, sorts, t=Abs(x,ty,M)}) =
   259 fun dest_abs a (Cterm {t = Abs (x, ty, M), T as Type("fun",[_,S]), thy_ref, maxidx, sorts}) =
   224       let val (y,N) = variant_abs (if_none a x, ty, M)
   260       let val (y, N) = variant_abs (if_none a x, ty, M) in
   225       in (Cterm {thy_ref = thy_ref, T = ty, maxidx = 0, sorts = sorts, t = Free(y,ty)},
   261         (Cterm {t = Free (y, ty), T = ty, thy_ref = thy_ref, maxidx = 0, sorts = sorts},
   226           Cterm {thy_ref = thy_ref, T = S, maxidx = maxidx, sorts = sorts, t = N})
   262           Cterm {t = N, T = S, thy_ref = thy_ref, maxidx = maxidx, sorts = sorts})
   227       end
   263       end
   228   | dest_abs _ _ = raise CTERM "dest_abs";
   264   | dest_abs _ _ = raise CTERM "dest_abs";
   229 
   265 
   230 (*Makes maxidx precise: it is often too big*)
   266 (*Makes maxidx precise: it is often too big*)
   231 fun adjust_maxidx (ct as Cterm {thy_ref, t, T, maxidx, sorts}) =
   267 fun adjust_maxidx (ct as Cterm {thy_ref, t, T, maxidx, sorts}) =
   232   if maxidx = ~1 then ct
   268   if maxidx = ~1 then ct
   233   else Cterm {thy_ref = thy_ref, t = t, T = T, maxidx = maxidx_of_term t, sorts = sorts};
   269   else Cterm {thy_ref = thy_ref, t = t, T = T, maxidx = maxidx_of_term t, sorts = sorts};
   234 
   270 
   235 (*Form cterm out of a function and an argument*)
   271 (*Form cterm out of a function and an argument*)
   236 fun capply
   272 fun capply
   237   (Cterm {t=f, thy_ref=thy_ref1, T=Type("fun",[dty,rty]), maxidx=maxidx1, sorts = sorts1})
   273   (cf as Cterm {t = f, T = Type ("fun", [dty, rty]), maxidx = maxidx1, sorts = sorts1, ...})
   238   (Cterm {t=x, thy_ref=thy_ref2, T, maxidx=maxidx2, sorts = sorts2}) =
   274   (cx as Cterm {t = x, T, maxidx = maxidx2, sorts = sorts2, ...}) =
   239     if T = dty then
   275     if T = dty then
   240       Cterm{t = f $ x,
   276       Cterm {thy_ref = merge_thys0 cf cx,
   241         thy_ref=Theory.merge_refs(thy_ref1,thy_ref2), T=rty,
   277         t = f $ x,
   242         maxidx=Int.max(maxidx1, maxidx2),
   278         T = rty,
       
   279         maxidx = Int.max (maxidx1, maxidx2),
   243         sorts = Sorts.union sorts1 sorts2}
   280         sorts = Sorts.union sorts1 sorts2}
   244       else raise CTERM "capply: types don't agree"
   281       else raise CTERM "capply: types don't agree"
   245   | capply _ _ = raise CTERM "capply: first arg is not a function"
   282   | capply _ _ = raise CTERM "capply: first arg is not a function"
   246 
   283 
   247 fun cabs
   284 fun cabs
   248   (Cterm {t=t1, thy_ref=thy_ref1, T=T1, maxidx=maxidx1, sorts = sorts1})
   285   (ct1 as Cterm {t = t1, T = T1, maxidx = maxidx1, sorts = sorts1, ...})
   249   (Cterm {t=t2, thy_ref=thy_ref2, T=T2, maxidx=maxidx2, sorts = sorts2}) =
   286   (ct2 as Cterm {t = t2, T = T2, maxidx = maxidx2, sorts = sorts2, ...}) =
   250     let val t = lambda t1 t2 handle TERM _ => raise CTERM "cabs: first arg is not a variable" in
   287     let val t = lambda t1 t2 handle TERM _ => raise CTERM "cabs: first arg is not a variable" in
   251       Cterm {t = t, T = T1 --> T2, thy_ref = Theory.merge_refs (thy_ref1, thy_ref2),
   288       Cterm {thy_ref = merge_thys0 ct1 ct2,
   252         maxidx = Int.max (maxidx1, maxidx2), sorts = Sorts.union sorts1 sorts2}
   289         t = t, T = T1 --> T2,
       
   290         maxidx = Int.max (maxidx1, maxidx2),
       
   291         sorts = Sorts.union sorts1 sorts2}
   253     end;
   292     end;
   254 
   293 
   255 (*Matching of cterms*)
   294 (*Matching of cterms*)
   256 fun gen_cterm_match mtch
   295 fun gen_cterm_match match
   257     (Cterm {thy_ref = thy_ref1, maxidx = maxidx1, t = t1, sorts = sorts1, ...},
   296     (ct1 as Cterm {t = t1, maxidx = maxidx1, sorts = sorts1, ...},
   258      Cterm {thy_ref = thy_ref2, maxidx = maxidx2, t = t2, sorts = sorts2, ...}) =
   297      ct2 as Cterm {t = t2, maxidx = maxidx2, sorts = sorts2, ...}) =
   259   let
   298   let
   260     val thy_ref = Theory.merge_refs (thy_ref1, thy_ref2);
   299     val thy_ref = merge_thys0 ct1 ct2;
   261     val tsig = Sign.tsig_of (Theory.deref thy_ref);
   300     val (Tinsts, tinsts) = match (Sign.tsig_of (Theory.deref thy_ref)) (t1, t2);
   262     val (Tinsts, tinsts) = mtch tsig (t1, t2);
       
   263     val maxidx = Int.max (maxidx1, maxidx2);
   301     val maxidx = Int.max (maxidx1, maxidx2);
   264     val sorts = Sorts.union sorts1 sorts2;
   302     val sorts = Sorts.union sorts1 sorts2;
   265     fun mk_cTinsts (ixn, (S, T)) =
   303     fun mk_cTinst (ixn, (S, T)) =
   266       (Ctyp {thy_ref = thy_ref, T = TVar (ixn, S)},
   304       (Ctyp {T = TVar (ixn, S), thy_ref = thy_ref, sorts = sorts},
   267        Ctyp {thy_ref = thy_ref, T = T});
   305        Ctyp {T = T, thy_ref = thy_ref, sorts = sorts});
   268     fun mk_ctinsts (ixn, (T, t)) =
   306     fun mk_ctinst (ixn, (T, t)) =
   269       let val T = Envir.typ_subst_TVars Tinsts T in
   307       let val T = Envir.typ_subst_TVars Tinsts T in
   270         (Cterm {thy_ref = thy_ref, maxidx = maxidx, T = T, t = Var (ixn, T), sorts = sorts},
   308         (Cterm {t = Var (ixn, T), T = T, thy_ref = thy_ref, maxidx = maxidx, sorts = sorts},
   271          Cterm {thy_ref = thy_ref, maxidx = maxidx, T = T, t = t, sorts = sorts})
   309          Cterm {t = t, T = T, thy_ref = thy_ref, maxidx = maxidx, sorts = sorts})
   272       end;
   310       end;
   273   in (map mk_cTinsts (Vartab.dest Tinsts), map mk_ctinsts (Vartab.dest tinsts)) end;
   311   in (Vartab.fold (cons o mk_cTinst) Tinsts [], Vartab.fold (cons o mk_ctinst) tinsts []) end;
   274 
   312 
   275 val cterm_match = gen_cterm_match Pattern.match;
   313 val cterm_match = gen_cterm_match Pattern.match;
   276 val cterm_first_order_match = gen_cterm_match Pattern.first_order_match;
   314 val cterm_first_order_match = gen_cterm_match Pattern.first_order_match;
   277 
   315 
   278 (*Incrementing indexes*)
   316 (*Incrementing indexes*)
   318   shyps: sort list,            (*sort hypotheses as ordered list*)
   356   shyps: sort list,            (*sort hypotheses as ordered list*)
   319   hyps: term list,             (*hypotheses as ordered list*)
   357   hyps: term list,             (*hypotheses as ordered list*)
   320   tpairs: (term * term) list,  (*flex-flex pairs*)
   358   tpairs: (term * term) list,  (*flex-flex pairs*)
   321   prop: term};                 (*conclusion*)
   359   prop: term};                 (*conclusion*)
   322 
   360 
   323 fun terms_of_tpairs tpairs = List.concat (map (fn (t, u) => [t, u]) tpairs);
   361 fun terms_of_tpairs tpairs = fold_rev (fn (t, u) => cons t o cons u) tpairs [];
   324 val union_tpairs = gen_merge_lists (op = : (term * term) * (term * term) -> bool);
   362 
       
   363 fun eq_tpairs ((t, u), (t', u')) = t aconv t' andalso u aconv u';
       
   364 val union_tpairs = gen_merge_lists eq_tpairs;
   325 
   365 
   326 fun attach_tpairs tpairs prop =
   366 fun attach_tpairs tpairs prop =
   327   Logic.list_implies (map Logic.mk_equals tpairs, prop);
   367   Logic.list_implies (map Logic.mk_equals tpairs, prop);
   328 
   368 
   329 fun rep_thm (Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop}) =
   369 fun rep_thm (Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop}) =
   353 fun no_attributes x = (x, []);
   393 fun no_attributes x = (x, []);
   354 fun apply_attributes (x_th, atts) = Library.apply atts x_th;
   394 fun apply_attributes (x_th, atts) = Library.apply atts x_th;
   355 fun applys_attributes (x_ths, atts) = foldl_map (Library.apply atts) x_ths;
   395 fun applys_attributes (x_ths, atts) = foldl_map (Library.apply atts) x_ths;
   356 
   396 
   357 
   397 
   358 (* shyps and hyps *)
   398 (* hyps *)
   359 
   399 
   360 val remove_hyps = OrdList.remove Term.term_ord;
   400 val remove_hyps = OrdList.remove Term.term_ord;
   361 val union_hyps = OrdList.union Term.term_ord;
   401 val union_hyps = OrdList.union Term.term_ord;
   362 val eq_set_hyps = OrdList.eq_set Term.term_ord;
   402 val eq_set_hyps = OrdList.eq_set Term.term_ord;
   363 
   403 
   372       rep_thm th2;
   412       rep_thm th2;
   373   in
   413   in
   374     Context.joinable (thy1, thy2) andalso
   414     Context.joinable (thy1, thy2) andalso
   375     Sorts.eq_set (shyps1, shyps2) andalso
   415     Sorts.eq_set (shyps1, shyps2) andalso
   376     eq_set_hyps (hyps1, hyps2) andalso
   416     eq_set_hyps (hyps1, hyps2) andalso
   377     aconvs (terms_of_tpairs tpairs1, terms_of_tpairs tpairs2) andalso
   417     equal_lists eq_tpairs (tpairs1, tpairs2) andalso
   378     prop1 aconv prop2
   418     prop1 aconv prop2
   379   end;
   419   end;
   380 
   420 
   381 val eq_thms = Library.equal_lists eq_thm;
   421 val eq_thms = Library.equal_lists eq_thm;
   382 
   422 
   399 
   439 
   400 (*the statement of any thm is a cterm*)
   440 (*the statement of any thm is a cterm*)
   401 fun cprop_of (Thm {thy_ref, maxidx, shyps, prop, ...}) =
   441 fun cprop_of (Thm {thy_ref, maxidx, shyps, prop, ...}) =
   402   Cterm {thy_ref = thy_ref, maxidx = maxidx, T = propT, t = prop, sorts = shyps};
   442   Cterm {thy_ref = thy_ref, maxidx = maxidx, T = propT, t = prop, sorts = shyps};
   403 
   443 
   404 
   444 (*explicit transfer to a super theory*)
   405 (* merge theories of cterms/thms; raise exception if incompatible *)
       
   406 
       
   407 fun merge_thys1
       
   408     (Cterm {thy_ref = r1, ...}) (th as Thm {thy_ref = r2, ...}) =
       
   409   Theory.merge_refs (r1, r2) handle TERM (msg, _) => raise THM (msg, 0, [th]);
       
   410 
       
   411 fun merge_thys2
       
   412     (th1 as Thm {thy_ref = r1, ...}) (th2 as Thm {thy_ref = r2, ...}) =
       
   413   Theory.merge_refs (r1, r2) handle TERM (msg, _) => raise THM (msg, 0, [th1, th2]);
       
   414 
       
   415 
       
   416 (* explicit transfer thm to super theory *)
       
   417 
       
   418 fun transfer thy' thm =
   445 fun transfer thy' thm =
   419   let
   446   let
   420     val Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop} = thm;
   447     val Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop} = thm;
   421     val thy = Theory.deref thy_ref;
   448     val thy = Theory.deref thy_ref;
   422   in
   449   in
   426         shyps = shyps, hyps = hyps, tpairs = tpairs, prop = prop}
   453         shyps = shyps, hyps = hyps, tpairs = tpairs, prop = prop}
   427     else raise THM ("transfer: not a super theory", 0, [thm])
   454     else raise THM ("transfer: not a super theory", 0, [thm])
   428   end;
   455   end;
   429 
   456 
   430 
   457 
       
   458 (* merge theories of cterms/thms; raise exception if incompatible *)
       
   459 
       
   460 fun merge_thys1 (Cterm {thy_ref = r1, ...}) (th as Thm {thy_ref = r2, ...}) =
       
   461   Theory.merge_refs (r1, r2) handle TERM (msg, _) => raise THM (msg, 0, [th]);
       
   462 
       
   463 fun merge_thys2 (th1 as Thm {thy_ref = r1, ...}) (th2 as Thm {thy_ref = r2, ...}) =
       
   464   Theory.merge_refs (r1, r2) handle TERM (msg, _) => raise THM (msg, 0, [th1, th2]);
       
   465 
       
   466 
   431 
   467 
   432 (** sort contexts of theorems **)
   468 (** sort contexts of theorems **)
   433 
   469 
   434 fun insert_env_sorts (env as Envir.Envir {iTs, asol, ...}) =
   470 fun present_sorts (Thm {hyps, tpairs, prop, ...}) =
   435   Vartab.fold (fn (_, (_, t)) => Sorts.insert_term t) asol o
   471   fold (fn (t, u) => Sorts.insert_term t o Sorts.insert_term u) tpairs
   436   Vartab.fold (fn (_, (_, T)) => Sorts.insert_typ T) iTs;
   472     (Sorts.insert_terms hyps (Sorts.insert_term prop []));
   437 
       
   438 fun insert_thm_sorts (Thm {hyps, tpairs, prop, ...}) =
       
   439   fold (fn (t, u) => Sorts.insert_term t o Sorts.insert_term u) tpairs o
       
   440   Sorts.insert_terms hyps o Sorts.insert_term prop;
       
   441 
       
   442 (*dangling sort constraints of a thm*)
       
   443 fun extra_shyps (th as Thm {shyps, ...}) =
       
   444   Sorts.subtract (insert_thm_sorts th []) shyps;
       
   445 
       
   446 
       
   447 (* strip_shyps *)
       
   448 
   473 
   449 (*remove extra sorts that are non-empty by virtue of type signature information*)
   474 (*remove extra sorts that are non-empty by virtue of type signature information*)
   450 fun strip_shyps (thm as Thm {shyps = [], ...}) = thm
   475 fun strip_shyps (thm as Thm {shyps = [], ...}) = thm
   451   | strip_shyps (thm as Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop}) =
   476   | strip_shyps (thm as Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop}) =
   452       let
   477       let
   453         val thy = Theory.deref thy_ref;
   478         val thy = Theory.deref thy_ref;
   454         val present_sorts = insert_thm_sorts thm [];
   479         val shyps' =
   455         val extra_shyps = Sorts.subtract present_sorts shyps;
   480           if Sign.all_sorts_nonempty thy then []
   456         val witnessed_shyps = Sign.witness_sorts thy present_sorts extra_shyps;
   481           else
       
   482             let
       
   483               val present = present_sorts thm;
       
   484               val extra = Sorts.subtract present shyps;
       
   485               val witnessed = map #2 (Sign.witness_sorts thy present extra);
       
   486             in Sorts.subtract witnessed shyps end;
   457       in
   487       in
   458         Thm {thy_ref = thy_ref, der = der, maxidx = maxidx,
   488         Thm {thy_ref = thy_ref, der = der, maxidx = maxidx,
   459              shyps = Sorts.subtract (map #2 witnessed_shyps) shyps,
   489           shyps = shyps', hyps = hyps, tpairs = tpairs, prop = prop}
   460              hyps = hyps, tpairs = tpairs, prop = prop}
       
   461       end;
   490       end;
       
   491 
       
   492 (*dangling sort constraints of a thm*)
       
   493 fun extra_shyps (th as Thm {shyps, ...}) = Sorts.subtract (present_sorts th) shyps;
   462 
   494 
   463 
   495 
   464 
   496 
   465 (** Axioms **)
   497 (** Axioms **)
   466 
   498 
   471       Symtab.lookup (#2 (#axioms (Theory.rep_theory thy)), name)
   503       Symtab.lookup (#2 (#axioms (Theory.rep_theory thy)), name)
   472       |> Option.map (fn prop =>
   504       |> Option.map (fn prop =>
   473           Thm {thy_ref = Theory.self_ref thy,
   505           Thm {thy_ref = Theory.self_ref thy,
   474             der = Pt.infer_derivs' I (false, Pt.axm_proof name prop),
   506             der = Pt.infer_derivs' I (false, Pt.axm_proof name prop),
   475             maxidx = maxidx_of_term prop,
   507             maxidx = maxidx_of_term prop,
   476             shyps = Sorts.insert_term prop [],
   508             shyps = may_insert_term_sorts thy prop [],
   477             hyps = [],
   509             hyps = [],
   478             tpairs = [],
   510             tpairs = [],
   479             prop = prop});
   511             prop = prop});
   480   in
   512   in
   481     (case get_first get_ax (theory :: Theory.ancestors_of theory) of
   513     (case get_first get_ax (theory :: Theory.ancestors_of theory) of
   529 
   561 
   530 (*** Meta rules ***)
   562 (*** Meta rules ***)
   531 
   563 
   532 (** primitive rules **)
   564 (** primitive rules **)
   533 
   565 
   534 (*The assumption rule A |- A in a theory*)
   566 (*The assumption rule A |- A*)
   535 fun assume raw_ct =
   567 fun assume raw_ct =
   536   let val Cterm {thy_ref, t = prop, T, maxidx, sorts} = adjust_maxidx raw_ct in
   568   let val Cterm {thy_ref, t = prop, T, maxidx, sorts} = adjust_maxidx raw_ct in
   537     if T <> propT then
   569     if T <> propT then
   538       raise THM ("assume: assumptions must have type prop", 0, [])
   570       raise THM ("assume: assumptions must have type prop", 0, [])
   539     else if maxidx <> ~1 then
   571     else if maxidx <> ~1 then
   582     fun err () = raise THM ("implies_elim: major premise", 0, [thAB, thA]);
   614     fun err () = raise THM ("implies_elim: major premise", 0, [thAB, thA]);
   583   in
   615   in
   584     case prop of
   616     case prop of
   585       imp $ A $ B =>
   617       imp $ A $ B =>
   586         if imp = Term.implies andalso A aconv propA then
   618         if imp = Term.implies andalso A aconv propA then
   587           Thm {thy_ref= merge_thys2 thAB thA,
   619           Thm {thy_ref = merge_thys2 thAB thA,
   588             der = Pt.infer_derivs (curry Pt.%%) der derA,
   620             der = Pt.infer_derivs (curry Pt.%%) der derA,
   589             maxidx = Int.max (maxA, maxidx),
   621             maxidx = Int.max (maxA, maxidx),
   590             shyps = Sorts.union shypsA shyps,
   622             shyps = Sorts.union shypsA shyps,
   591             hyps = union_hyps hypsA hyps,
   623             hyps = union_hyps hypsA hyps,
   592             tpairs = union_tpairs tpairsA tpairs,
   624             tpairs = union_tpairs tpairsA tpairs,
   594         else err ()
   626         else err ()
   595     | _ => err ()
   627     | _ => err ()
   596   end;
   628   end;
   597 
   629 
   598 (*Forall introduction.  The Free or Var x must not be free in the hypotheses.
   630 (*Forall introduction.  The Free or Var x must not be free in the hypotheses.
   599    [x]
   631     [x]
   600     :
   632      :
   601     A
   633      A
   602   -----
   634   ------
   603   !!x.A
   635   !!x. A
   604 *)
   636 *)
   605 fun forall_intr
   637 fun forall_intr
   606     (ct as Cterm {t = x, T, sorts, ...})
   638     (ct as Cterm {t = x, T, sorts, ...})
   607     (th as Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop}) =
   639     (th as Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop}) =
   608   let
   640   let
   624     | Var ((a, _), _) => (check_occs x (terms_of_tpairs tpairs); result a)
   656     | Var ((a, _), _) => (check_occs x (terms_of_tpairs tpairs); result a)
   625     | _ => raise THM ("forall_intr: not a variable", 0, [th])
   657     | _ => raise THM ("forall_intr: not a variable", 0, [th])
   626   end;
   658   end;
   627 
   659 
   628 (*Forall elimination
   660 (*Forall elimination
   629   !!x.A
   661   !!x. A
   630   ------
   662   ------
   631   A[t/x]
   663   A[t/x]
   632 *)
   664 *)
   633 fun forall_elim
   665 fun forall_elim
   634     (ct as Cterm {t, T, maxidx = maxt, sorts, ...})
   666     (ct as Cterm {t, T, maxidx = maxt, sorts, ...})
   652 
   684 
   653 (*Reflexivity
   685 (*Reflexivity
   654   t == t
   686   t == t
   655 *)
   687 *)
   656 fun reflexive (ct as Cterm {thy_ref, t, T, maxidx, sorts}) =
   688 fun reflexive (ct as Cterm {thy_ref, t, T, maxidx, sorts}) =
   657   Thm {thy_ref= thy_ref,
   689   Thm {thy_ref = thy_ref,
   658     der = Pt.infer_derivs' I (false, Pt.reflexive),
   690     der = Pt.infer_derivs' I (false, Pt.reflexive),
   659     maxidx = maxidx,
   691     maxidx = maxidx,
   660     shyps = sorts,
   692     shyps = sorts,
   661     hyps = [],
   693     hyps = [],
   662     tpairs = [],
   694     tpairs = [],
   694   in
   726   in
   695     case (prop1, prop2) of
   727     case (prop1, prop2) of
   696       ((eq as Const ("==", Type (_, [T, _]))) $ t1 $ u, Const ("==", _) $ u' $ t2) =>
   728       ((eq as Const ("==", Type (_, [T, _]))) $ t1 $ u, Const ("==", _) $ u' $ t2) =>
   697         if not (u aconv u') then err "middle term"
   729         if not (u aconv u') then err "middle term"
   698         else
   730         else
   699           Thm {thy_ref= merge_thys2 th1 th2,
   731           Thm {thy_ref = merge_thys2 th1 th2,
   700             der = Pt.infer_derivs (Pt.transitive u T) der1 der2,
   732             der = Pt.infer_derivs (Pt.transitive u T) der1 der2,
   701             maxidx = Int.max (max1, max2),
   733             maxidx = Int.max (max1, max2),
   702             shyps = Sorts.union shyps1 shyps2,
   734             shyps = Sorts.union shyps1 shyps2,
   703             hyps = union_hyps hyps1 hyps2,
   735             hyps = union_hyps hyps1 hyps2,
   704             tpairs = union_tpairs tpairs1 tpairs2,
   736             tpairs = union_tpairs tpairs1 tpairs2,
   705             prop = eq $ t1 $ t2}
   737             prop = eq $ t1 $ t2}
   706      | _ =>  err "premises"
   738      | _ =>  err "premises"
   707   end;
   739   end;
   708 
   740 
   709 (*Beta-conversion
   741 (*Beta-conversion
   710   (%x.t)(u) == t[u/x]
   742   (%x. t)(u) == t[u/x]
   711   fully beta-reduces the term if full = true
   743   fully beta-reduces the term if full = true
   712 *)
   744 *)
   713 fun beta_conversion full (Cterm {thy_ref, t, T, maxidx, sorts}) =
   745 fun beta_conversion full (Cterm {thy_ref, t, T, maxidx, sorts}) =
   714   let val t' =
   746   let val t' =
   715     if full then Envir.beta_norm t
   747     if full then Envir.beta_norm t
   874           val prop' = Envir.norm_term env prop;
   906           val prop' = Envir.norm_term env prop;
   875         in
   907         in
   876           Thm {thy_ref = thy_ref,
   908           Thm {thy_ref = thy_ref,
   877             der = Pt.infer_derivs' (Pt.norm_proof' env) der,
   909             der = Pt.infer_derivs' (Pt.norm_proof' env) der,
   878             maxidx = maxidx_of_terms (prop' :: terms_of_tpairs tpairs'),
   910             maxidx = maxidx_of_terms (prop' :: terms_of_tpairs tpairs'),
   879             shyps = insert_env_sorts env shyps,
   911             shyps = may_insert_env_sorts (Theory.deref thy_ref) env shyps,
   880             hyps = hyps,
   912             hyps = hyps,
   881             tpairs = tpairs',
   913             tpairs = tpairs',
   882             prop = prop'}
   914             prop = prop'}
   883         end);
   915         end);
   884 
   916 
   885 
   917 
   886 (*Instantiation of Vars
   918 (*Instantiation of Vars
   887             A
   919            A
   888   ---------------------
   920   --------------------
   889   A[t1/v1, ...., tn/vn]
   921   A[t1/v1, ..., tn/vn]
   890 *)
   922 *)
   891 
   923 
   892 local
   924 local
   893 
       
   894 (*Check that all the terms are Vars and are distinct*)
       
   895 fun instl_ok ts = forall is_Var ts andalso null (findrep ts);
       
   896 
   925 
   897 fun pretty_typing thy t T =
   926 fun pretty_typing thy t T =
   898   Pretty.block [Sign.pretty_term thy t, Pretty.str " ::", Pretty.brk 1, Sign.pretty_typ thy T];
   927   Pretty.block [Sign.pretty_term thy t, Pretty.str " ::", Pretty.brk 1, Sign.pretty_typ thy T];
   899 
   928 
   900 (*For instantiate: process pair of cterms, merge theories*)
   929 fun add_ctpair ((thy, sorts), (ct, cu)) =
   901 fun add_ctpair ((ct, cu), (thy_ref, tpairs)) =
   930   let
   902   let
   931     val Cterm {t = t, T = T, sorts = sorts1, ...} = ct
   903     val Cterm {thy_ref = thy_reft, t = t, T = T, ...} = ct
   932     and Cterm {t = u, T = U, sorts = sorts2, ...} = cu;
   904     and Cterm {thy_ref = thy_refu, t = u, T = U, ...} = cu;
   933     val thy' = Theory.merge (thy, Theory.deref (merge_thys0 ct cu));
   905     val thy_ref_merged = Theory.merge_refs (thy_ref, Theory.merge_refs (thy_reft, thy_refu));
   934     val sorts' = Sorts.union sorts2 (Sorts.union sorts1 sorts);
   906     val thy_merged = Theory.deref thy_ref_merged;
   935   in
   907   in
   936     if T = U then ((thy', sorts'), (t, u))
   908     if T = U then (thy_ref_merged, (t, u) :: tpairs)
       
   909     else raise TYPE (Pretty.string_of (Pretty.block
   937     else raise TYPE (Pretty.string_of (Pretty.block
   910      [Pretty.str "instantiate: type conflict",
   938      [Pretty.str "instantiate: type conflict",
   911       Pretty.fbrk, pretty_typing thy_merged t T,
   939       Pretty.fbrk, pretty_typing thy' t T,
   912       Pretty.fbrk, pretty_typing thy_merged u U]), [T,U], [t,u])
   940       Pretty.fbrk, pretty_typing thy' u U]), [T,U], [t,u])
   913   end;
   941   end;
   914 
   942 
   915 fun add_ctyp
   943 fun add_ctyp ((thy, sorts), (cT, cU)) =
   916   ((Ctyp {T = T as TVar (_, S), thy_ref = thy_refT},
   944   let
   917     Ctyp {T = U, thy_ref = thy_refU}), (thy_ref, vTs)) =
   945     val Ctyp {T = T, thy_ref = thy_ref1, sorts = sorts1, ...} = cT
   918       let
   946     and Ctyp {T = U, thy_ref = thy_ref2, sorts = sorts2, ...} = cU;
   919         val thy_ref_merged = Theory.merge_refs
   947     val thy' = Theory.merge (thy, Theory.deref (Theory.merge_refs (thy_ref1, thy_ref2)));
   920           (thy_ref, Theory.merge_refs (thy_refT, thy_refU));
   948     val sorts' = Sorts.union sorts2 (Sorts.union sorts1 sorts);
   921         val thy_merged = Theory.deref thy_ref_merged;
   949   in
   922       in
   950     (case T of TVar (_, S) =>
   923         if Type.of_sort (Sign.tsig_of thy_merged) (U, S) then
   951       if Type.of_sort (Sign.tsig_of thy') (U, S) then ((thy', sorts'), (T, U))
   924           (thy_ref_merged, (T, U) :: vTs)
   952       else raise TYPE ("Type not of sort " ^ Sign.string_of_sort thy' S, [U], [])
   925         else raise TYPE ("Type not of sort " ^ Sign.string_of_sort thy_merged S, [U], [])
   953     | _ => raise TYPE (Pretty.string_of (Pretty.block
   926       end
       
   927   | add_ctyp ((Ctyp {T, thy_ref}, _), _) =
       
   928       raise TYPE (Pretty.string_of (Pretty.block
       
   929         [Pretty.str "instantiate: not a type variable",
   954         [Pretty.str "instantiate: not a type variable",
   930          Pretty.fbrk, Sign.pretty_typ (Theory.deref thy_ref) T]), [T], []);
   955          Pretty.fbrk, Sign.pretty_typ thy' T]), [T], []))
       
   956   end;
   931 
   957 
   932 in
   958 in
   933 
   959 
   934 (*Left-to-right replacements: ctpairs = [..., (vi, ti), ...].
   960 (*Left-to-right replacements: ctpairs = [..., (vi, ti), ...].
   935   Instantiates distinct Vars by terms of same type.
   961   Instantiates distinct Vars by terms of same type.
   936   Does NOT normalize the resulting theorem!*)
   962   Does NOT normalize the resulting theorem!*)
   937 fun instantiate ([], []) th = th
   963 fun instantiate ([], []) th = th
   938   | instantiate (vcTs, ctpairs) th =
   964   | instantiate (vcTs, ctpairs) th =
   939   let
   965       let
   940     val Thm {thy_ref, der, maxidx, hyps, shyps, tpairs = dpairs, prop} = th;
   966         val Thm {thy_ref, der, maxidx, hyps, shyps, tpairs = dpairs, prop} = th;
   941     val (newthy_ref, tpairs) = foldr add_ctpair (thy_ref, []) ctpairs;
   967         val (thy_sorts, tpairs) = foldl_map add_ctpair ((Theory.deref thy_ref, shyps), ctpairs);
   942     val (newthy_ref, vTs) = foldr add_ctyp (newthy_ref, []) vcTs;
   968         val ((thy', shyps'), vTs) = foldl_map add_ctyp (thy_sorts, vcTs);
   943     fun subst t = subst_atomic tpairs (map_term_types (typ_subst_atomic vTs) t);
   969         fun subst t = subst_atomic tpairs (map_term_types (typ_subst_atomic vTs) t);
   944     val newprop = subst prop;
   970         val prop' = subst prop;
   945     val newdpairs = map (pairself subst) dpairs;
   971         val dpairs' = map (pairself subst) dpairs;
   946     val newth =
   972       in
   947       Thm {thy_ref = newthy_ref,
   973         if not (forall (is_Var o #1) tpairs andalso null (gen_duplicates eq_fst tpairs)) then
   948         der = Pt.infer_derivs' (Pt.instantiate vTs tpairs) der,
   974           raise THM ("instantiate: variables not distinct", 0, [th])
   949         maxidx = maxidx_of_terms (newprop :: terms_of_tpairs newdpairs),
   975         else if not (null (gen_duplicates eq_fst vTs)) then
   950         shyps = shyps
   976           raise THM ("instantiate: type variables not distinct", 0, [th])
   951           |> fold (Sorts.insert_typ o #2) vTs
   977         else
   952           |> fold (Sorts.insert_term o #2) tpairs,
   978           Thm {thy_ref = Theory.self_ref thy',
   953         hyps = hyps,
   979             der = Pt.infer_derivs' (Pt.instantiate vTs tpairs) der,
   954         tpairs = newdpairs,
   980             maxidx = maxidx_of_terms (prop' :: terms_of_tpairs dpairs'),
   955         prop = newprop};
   981             shyps = shyps',
   956   in
   982             hyps = hyps,
   957     if not (instl_ok (map #1 tpairs)) then
   983             tpairs = dpairs',
   958       raise THM ("instantiate: variables not distinct", 0, [th])
   984             prop = prop'}
   959     else if not (null (findrep (map #1 vTs))) then
   985       end
   960       raise THM ("instantiate: type variables not distinct", 0, [th])
   986       handle TYPE (msg, _, _) => raise THM (msg, 0, [th]);
   961     else newth
       
   962   end
       
   963   handle TYPE (msg, _, _) => raise THM (msg, 0, [th]);
       
   964 
   987 
   965 end;
   988 end;
   966 
   989 
   967 
   990 
   968 (*The trivial implication A ==> A, justified by assume and forall rules.
   991 (*The trivial implication A ==> A, justified by assume and forall rules.
  1074 
  1097 
  1075 (*Solve subgoal Bi of proof state B1...Bn/C by assumption. *)
  1098 (*Solve subgoal Bi of proof state B1...Bn/C by assumption. *)
  1076 fun assumption i state =
  1099 fun assumption i state =
  1077   let
  1100   let
  1078     val Thm {thy_ref, der, maxidx, shyps, hyps, prop, ...} = state;
  1101     val Thm {thy_ref, der, maxidx, shyps, hyps, prop, ...} = state;
       
  1102     val thy = Theory.deref thy_ref;
  1079     val (tpairs, Bs, Bi, C) = dest_state (state, i);
  1103     val (tpairs, Bs, Bi, C) = dest_state (state, i);
  1080     fun newth n (env as Envir.Envir {maxidx, ...}, tpairs) =
  1104     fun newth n (env as Envir.Envir {maxidx, ...}, tpairs) =
  1081       Thm {thy_ref = thy_ref,
  1105       Thm {thy_ref = thy_ref,
  1082         der = Pt.infer_derivs'
  1106         der = Pt.infer_derivs'
  1083           ((if Envir.is_empty env then I else (Pt.norm_proof' env)) o
  1107           ((if Envir.is_empty env then I else (Pt.norm_proof' env)) o
  1084             Pt.assumption_proof Bs Bi n) der,
  1108             Pt.assumption_proof Bs Bi n) der,
  1085         maxidx = maxidx,
  1109         maxidx = maxidx,
  1086         shyps = insert_env_sorts env shyps,
  1110         shyps = may_insert_env_sorts thy env shyps,
  1087         hyps = hyps,
  1111         hyps = hyps,
  1088         tpairs =
  1112         tpairs =
  1089           if Envir.is_empty env then tpairs
  1113           if Envir.is_empty env then tpairs
  1090           else map (pairself (Envir.norm_term env)) tpairs,
  1114           else map (pairself (Envir.norm_term env)) tpairs,
  1091         prop =
  1115         prop =
  1094           else (*normalize the new rule fully*)
  1118           else (*normalize the new rule fully*)
  1095             Envir.norm_term env (Logic.list_implies (Bs, C))};
  1119             Envir.norm_term env (Logic.list_implies (Bs, C))};
  1096     fun addprfs [] _ = Seq.empty
  1120     fun addprfs [] _ = Seq.empty
  1097       | addprfs ((t, u) :: apairs) n = Seq.make (fn () => Seq.pull
  1121       | addprfs ((t, u) :: apairs) n = Seq.make (fn () => Seq.pull
  1098           (Seq.mapp (newth n)
  1122           (Seq.mapp (newth n)
  1099             (Unify.unifiers (Theory.deref thy_ref, Envir.empty maxidx, (t, u) :: tpairs))
  1123             (Unify.unifiers (thy, Envir.empty maxidx, (t, u) :: tpairs))
  1100             (addprfs apairs (n + 1))))
  1124             (addprfs apairs (n + 1))))
  1101   in addprfs (Logic.assum_pairs (~1, Bi)) 1 end;
  1125   in addprfs (Logic.assum_pairs (~1, Bi)) 1 end;
  1102 
  1126 
  1103 (*Solve subgoal Bi of proof state B1...Bn/C by assumption.
  1127 (*Solve subgoal Bi of proof state B1...Bn/C by assumption.
  1104   Checks if Bi's conclusion is alpha-convertible to one of its assumptions*)
  1128   Checks if Bi's conclusion is alpha-convertible to one of its assumptions*)
  1148   end;
  1172   end;
  1149 
  1173 
  1150 
  1174 
  1151 (*Rotates a rule's premises to the left by k, leaving the first j premises
  1175 (*Rotates a rule's premises to the left by k, leaving the first j premises
  1152   unchanged.  Does nothing if k=0 or if k equals n-j, where n is the
  1176   unchanged.  Does nothing if k=0 or if k equals n-j, where n is the
  1153   number of premises.  Useful with etac and underlies tactic/defer_tac*)
  1177   number of premises.  Useful with etac and underlies defer_tac*)
  1154 fun permute_prems j k rl =
  1178 fun permute_prems j k rl =
  1155   let
  1179   let
  1156     val Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop} = rl;
  1180     val Thm {thy_ref, der, maxidx, shyps, hyps, tpairs, prop} = rl;
  1157     val prems = Logic.strip_imp_prems prop
  1181     val prems = Logic.strip_imp_prems prop
  1158     and concl = Logic.strip_imp_concl prop;
  1182     and concl = Logic.strip_imp_concl prop;
  1225        shyps = shyps,
  1249        shyps = shyps,
  1226        tpairs = tpairs,
  1250        tpairs = tpairs,
  1227        prop = prop'});
  1251        prop = prop'});
  1228 
  1252 
  1229 
  1253 
  1230 (* strip_apply f A(,B) strips off all assumptions/parameters from A
  1254 (* strip_apply f (A, B) strips off all assumptions/parameters from A
  1231    introduced by lifting over B, and applies f to remaining part of A*)
  1255    introduced by lifting over B, and applies f to remaining part of A*)
  1232 fun strip_apply f =
  1256 fun strip_apply f =
  1233   let fun strip(Const("==>",_)$ A1 $ B1,
  1257   let fun strip(Const("==>",_)$ A1 $ B1,
  1234                 Const("==>",_)$ _  $ B2) = implies $ A1 $ strip(B1,B2)
  1258                 Const("==>",_)$ _  $ B2) = implies $ A1 $ strip(B1,B2)
  1235         | strip((c as Const("all",_)) $ Abs(a,T,t1),
  1259         | strip((c as Const("all",_)) $ Abs(a,T,t1),
  1336                        (fn f => fn der => f (Pt.norm_proof' env der))
  1360                        (fn f => fn der => f (Pt.norm_proof' env der))
  1337                      else
  1361                      else
  1338                        curry op oo (Pt.norm_proof' env))
  1362                        curry op oo (Pt.norm_proof' env))
  1339                     (Pt.bicompose_proof Bs oldAs As A n)) rder' sder,
  1363                     (Pt.bicompose_proof Bs oldAs As A n)) rder' sder,
  1340                  maxidx = maxidx,
  1364                  maxidx = maxidx,
  1341                  shyps = insert_env_sorts env (Sorts.union rshyps sshyps),
  1365                  shyps = may_insert_env_sorts thy env (Sorts.union rshyps sshyps),
  1342                  hyps = union_hyps rhyps shyps,
  1366                  hyps = union_hyps rhyps shyps,
  1343                  tpairs = ntpairs,
  1367                  tpairs = ntpairs,
  1344                  prop = Logic.list_implies normp}
  1368                  prop = Logic.list_implies normp}
  1345         in  Seq.cons(th, thq)  end  handle COMPOSE => thq;
  1369         in  Seq.cons(th, thq)  end  handle COMPOSE => thq;
  1346      val (rAs,B) = Logic.strip_prems(nsubgoal, [], rprop)
  1370      val (rAs,B) = Logic.strip_prems(nsubgoal, [], rprop)
  1431           raise THM ("Oracle's result must have type prop: " ^ name, 0, [])
  1455           raise THM ("Oracle's result must have type prop: " ^ name, 0, [])
  1432         else
  1456         else
  1433           Thm {thy_ref = Theory.self_ref thy',
  1457           Thm {thy_ref = Theory.self_ref thy',
  1434             der = (true, Pt.oracle_proof name prop),
  1458             der = (true, Pt.oracle_proof name prop),
  1435             maxidx = maxidx,
  1459             maxidx = maxidx,
  1436             shyps = Sorts.insert_term prop [],
  1460             shyps = may_insert_term_sorts thy' prop [],
  1437             hyps = [],
  1461             hyps = [],
  1438             tpairs = [],
  1462             tpairs = [],
  1439             prop = prop}
  1463             prop = prop}
  1440       end
  1464       end
  1441   end;
  1465   end;
  1442 
  1466 
  1443 fun invoke_oracle thy =
  1467 fun invoke_oracle thy =
  1444   invoke_oracle_i thy o NameSpace.intern (Theory.oracle_space thy);
  1468   invoke_oracle_i thy o NameSpace.intern (Theory.oracle_space thy);
  1445 
  1469 
  1446 
       
  1447 end;
  1470 end;
  1448 
       
  1449 
  1471 
  1450 structure BasicThm: BASIC_THM = Thm;
  1472 structure BasicThm: BASIC_THM = Thm;
  1451 open BasicThm;
  1473 open BasicThm;