src/Pure/Isar/proof_context.ML
changeset 12550 32843ad8160a
parent 12530 c32d201d7c08
child 12576 9fd10052c3f7
equal deleted inserted replaced
12549:65f03a3f7998 12550:32843ad8160a
    42   val cert_term_pats: typ -> context -> term list -> term list
    42   val cert_term_pats: typ -> context -> term list -> term list
    43   val cert_prop_pats: context -> term list -> term list
    43   val cert_prop_pats: context -> term list -> term list
    44   val declare_term: term -> context -> context
    44   val declare_term: term -> context -> context
    45   val declare_terms: term list -> context -> context
    45   val declare_terms: term list -> context -> context
    46   val warn_extra_tfrees: context -> context -> context
    46   val warn_extra_tfrees: context -> context -> context
    47   val generalizeT: context -> context -> typ -> typ
    47   val generalize: context -> context -> term list -> term list
    48   val generalize: context -> context -> term -> term
       
    49   val find_free: term -> string -> term option
    48   val find_free: term -> string -> term option
    50   val export: bool -> context -> context -> thm -> thm Seq.seq
    49   val export: bool -> context -> context -> thm -> thm Seq.seq
    51   val drop_schematic: indexname * term option -> indexname * term option
    50   val drop_schematic: indexname * term option -> indexname * term option
    52   val add_binds: (indexname * string option) list -> context -> context
    51   val add_binds: (indexname * string option) list -> context -> context
    53   val add_binds_i: (indexname * term option) list -> context -> context
    52   val add_binds_i: (indexname * term option) list -> context -> context
   686   end;
   685   end;
   687 
   686 
   688 
   687 
   689 (* generalize type variables *)
   688 (* generalize type variables *)
   690 
   689 
   691 fun gen_tfrees inner outer =
   690 fun generalize_tfrees inner outer =
   692   let
   691   let
   693     val extra_fixes = fixed_names_of inner \\ fixed_names_of outer;
   692     val extra_fixes = fixed_names_of inner \\ fixed_names_of outer;
   694     fun still_fixed (Free (x, _)) = not (x mem_string extra_fixes)
   693     fun still_fixed (Free (x, _)) = not (x mem_string extra_fixes)
   695       | still_fixed _ = false;
   694       | still_fixed _ = false;
   696     fun add (gen, (a, xs)) =
   695     val occs_inner = type_occs inner;
   697       if is_some (Symtab.lookup (type_occs outer, a)) orelse exists still_fixed xs
   696     val occs_outer = type_occs outer;
       
   697     fun add (gen, a) =
       
   698       if is_some (Symtab.lookup (occs_outer, a)) orelse
       
   699         exists still_fixed (Symtab.lookup_multi (occs_inner, a))
   698       then gen else a :: gen;
   700       then gen else a :: gen;
   699   in Symtab.foldl add ([], type_occs inner) end;
   701   in fn tfrees => foldl add ([], tfrees) end;
   700 
   702 
   701 fun generalizeT inner outer =
   703 fun generalize inner outer ts =
   702   let
   704   let
   703     val tfrees = gen_tfrees inner outer;
   705     val tfrees = generalize_tfrees inner outer (foldr Term.add_term_tfree_names (ts, []));
   704     fun gen (x, S) = if x mem_string tfrees then TVar ((x, 0), S) else TFree (x, S);
   706     fun gen (x, S) = if x mem_string tfrees then TVar ((x, 0), S) else TFree (x, S);
   705   in Term.map_type_tfree gen end;
   707   in map (Term.map_term_types (Term.map_type_tfree gen)) ts end;
   706 
       
   707 val generalize = Term.map_term_types oo generalizeT;
       
   708 
   708 
   709 
   709 
   710 
   710 
   711 (** export theorems **)
   711 (** export theorems **)
   712 
   712 
   715 
   715 
   716 fun find_free t x = foldl_aterms (get_free x) (None, t);
   716 fun find_free t x = foldl_aterms (get_free x) (None, t);
   717 
   717 
   718 fun export is_goal inner outer =
   718 fun export is_goal inner outer =
   719   let
   719   let
   720     val tfrees = gen_tfrees inner outer;
   720     val gen = generalize_tfrees inner outer;
   721     val fixes = fixed_names_of inner \\ fixed_names_of outer;
   721     val fixes = fixed_names_of inner \\ fixed_names_of outer;
   722     val asms = Library.drop (length (assumptions_of outer), assumptions_of inner);
   722     val asms = Library.drop (length (assumptions_of outer), assumptions_of inner);
   723     val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms;
   723     val exp_asms = map (fn (cprops, exp) => exp is_goal cprops) asms;
   724   in fn thm =>
   724   in fn thm => thm
   725     thm
       
   726     |> Tactic.norm_hhf
   725     |> Tactic.norm_hhf
   727     |> Seq.EVERY (rev exp_asms)
   726     |> Seq.EVERY (rev exp_asms)
   728     |> Seq.map (fn rule =>
   727     |> Seq.map (fn rule =>
   729       let
   728       let
   730         val {sign, prop, ...} = Thm.rep_thm rule;
   729         val {sign, prop, ...} = Thm.rep_thm rule;
   731         val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) fixes);
   730         val frees = map (Thm.cterm_of sign) (mapfilter (find_free prop) fixes);
       
   731         val tfrees = gen (Term.add_term_tfree_names (prop, []));
   732       in
   732       in
   733         rule
   733         rule
   734         |> Drule.forall_intr_list frees
   734         |> Drule.forall_intr_list frees
   735         |> Tactic.norm_hhf
   735         |> Tactic.norm_hhf
   736         |> (#1 o Drule.tvars_intr_list tfrees)
   736         |> (#1 o Drule.tvars_intr_list tfrees)
   831   let
   831   let
   832     val ts = prep_terms ctxt (map snd raw_binds);
   832     val ts = prep_terms ctxt (map snd raw_binds);
   833     val (ctxt', binds) =
   833     val (ctxt', binds) =
   834       apsnd flat (foldl_map (prep_bind prep_pats) (ctxt, map fst raw_binds ~~ ts));
   834       apsnd flat (foldl_map (prep_bind prep_pats) (ctxt, map fst raw_binds ~~ ts));
   835     val binds' =
   835     val binds' =
   836       if gen then map (apsnd (generalize ctxt' ctxt)) binds
   836       if gen then map #1 binds ~~ generalize ctxt' ctxt (map #2 binds)
   837       else binds;
   837       else binds;
   838     val binds'' = map (apsnd Some) binds';
   838     val binds'' = map (apsnd Some) binds';
   839   in
   839   in
   840     warn_extra_tfrees ctxt
   840     warn_extra_tfrees ctxt
   841      (if gen then ctxt (*sic!*) |> declare_terms (map #2 binds') |> add_binds_i binds''
   841      (if gen then ctxt (*sic!*) |> declare_terms (map #2 binds') |> add_binds_i binds''
   876     val binds = flat (flat (map (map (matches ctxt')) args));
   876     val binds = flat (flat (map (map (matches ctxt')) args));
   877     val propss = map (map #1) args;
   877     val propss = map (map #1) args;
   878 
   878 
   879     (*generalize result: context evaluated now, binds added later*)
   879     (*generalize result: context evaluated now, binds added later*)
   880     val gen = generalize ctxt' ctxt;
   880     val gen = generalize ctxt' ctxt;
   881     fun gen_binds c = c |> add_binds_i (map (apsnd (Some o gen)) binds);
   881     fun gen_binds c = c |> add_binds_i (map #1 binds ~~ map Some (gen (map #2 binds)));
   882   in (ctxt' |> add_binds_i (map (apsnd Some) binds), (propss, gen_binds)) end;
   882   in (ctxt' |> add_binds_i (map (apsnd Some) binds), (propss, gen_binds)) end;
   883 
   883 
   884 in
   884 in
   885 
   885 
   886 val read_propp = prep_propp false read_props read_prop_pats;
   886 val read_propp = prep_propp false read_props read_prop_pats;