src/Pure/Isar/proof_context.ML
changeset 11925 4747b4b84093
parent 11918 dfdf0798d7b8
child 11926 e31f781611b3
equal deleted inserted replaced
11924:b6def266cbb9 11925:4747b4b84093
    26   val intern_skolem: context -> term -> term
    26   val intern_skolem: context -> term -> term
    27   val extern_skolem: context -> term -> term
    27   val extern_skolem: context -> term -> term
    28   val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
    28   val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
    29   val read_term: context -> string -> term
    29   val read_term: context -> string -> term
    30   val read_prop: context -> string -> term
    30   val read_prop: context -> string -> term
       
    31   val read_prop_schematic: context -> string -> term
    31   val read_terms: context -> string list -> term list
    32   val read_terms: context -> string list -> term list
    32   val read_termT_pats: context -> (string * typ) list -> term list
    33   val read_termT_pats: context -> (string * typ) list -> term list
    33   val read_term_pats: typ -> context -> string list -> term list
    34   val read_term_pats: typ -> context -> string list -> term list
    34   val read_prop_pats: context -> string list -> term list
    35   val read_prop_pats: context -> string list -> term list
    35   val cert_term: context -> term -> term
    36   val cert_term: context -> term -> term
    80   val export_assume: exporter
    81   val export_assume: exporter
    81   val export_presume: exporter
    82   val export_presume: exporter
    82   val export_def: exporter
    83   val export_def: exporter
    83   val assume: exporter
    84   val assume: exporter
    84     -> ((string * context attribute list) * (string * (string list * string list)) list) list
    85     -> ((string * context attribute list) * (string * (string list * string list)) list) list
    85     -> context -> context * ((string * thm list) list * thm list)
    86     -> context -> context * (string * thm list) list
    86   val assume_i: exporter
    87   val assume_i: exporter
    87     -> ((string * context attribute list) * (term * (term list * term list)) list) list
    88     -> ((string * context attribute list) * (term * (term list * term list)) list) list
    88     -> context -> context * ((string * thm list) list * thm list)
    89     -> context -> context * (string * thm list) list
    89   val read_vars: context * (string list * string option) -> context * (string list * typ option)
    90   val read_vars: context * (string list * string option) -> context * (string list * typ option)
    90   val cert_vars: context * (string list * typ option) -> context * (string list * typ option)
    91   val cert_vars: context * (string list * typ option) -> context * (string list * typ option)
    91   val fix: (string list * string option) list -> context -> context
    92   val fix: (string list * string option) list -> context -> context
    92   val fix_i: (string list * typ option) list -> context -> context
    93   val fix_i: (string list * typ option) list -> context -> context
       
    94   val fix_direct: (string list * typ option) list -> context -> context
    93   val bind_skolem: context -> string list -> term -> term
    95   val bind_skolem: context -> string list -> term -> term
    94   val get_case: context -> string -> string option list -> RuleCases.T
    96   val get_case: context -> string -> string option list -> RuleCases.T
    95   val add_cases: (string * RuleCases.T) list -> context -> context
    97   val add_cases: (string * RuleCases.T) list -> context -> context
    96   val apply_case: RuleCases.T -> context -> context * ((indexname * term option) list * term list)
    98   val apply_case: RuleCases.T -> context -> context * ((indexname * term option) list * term list)
    97   val show_hyps: bool ref
    99   val show_hyps: bool ref
   155 val sign_of = Theory.sign_of o theory_of;
   157 val sign_of = Theory.sign_of o theory_of;
   156 
   158 
   157 fun prems_of (Context {asms = ((_, asms), _), ...}) = flat (map #2 asms);
   159 fun prems_of (Context {asms = ((_, asms), _), ...}) = flat (map #2 asms);
   158 
   160 
   159 
   161 
   160 
       
   161 (** user data **)
   162 (** user data **)
   162 
   163 
   163 (* errors *)
   164 (* errors *)
   164 
   165 
   165 fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy);
   166 fun of_theory thy = "\nof theory " ^ Sign.str_of_sg (Theory.sign_of thy);
   286   cert (sign_of ctxt) raw_T
   287   cert (sign_of ctxt) raw_T
   287     handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
   288     handle TYPE (msg, _, _) => raise CONTEXT (msg, ctxt);
   288 
   289 
   289 in
   290 in
   290 
   291 
   291 val read_typ         = read_typ_aux Sign.read_typ;
   292 val read_typ = read_typ_aux Sign.read_typ;
   292 val read_typ_no_norm = read_typ_aux Sign.read_typ_no_norm;
   293 val read_typ_no_norm = read_typ_aux Sign.read_typ_no_norm;
   293 val cert_typ         = cert_typ_aux Sign.certify_typ;
   294 val cert_typ = cert_typ_aux Sign.certify_typ;
   294 val cert_typ_no_norm = cert_typ_aux Sign.certify_typ_no_norm;
   295 val cert_typ_no_norm = cert_typ_aux Sign.certify_typ_no_norm;
   295 
   296 
   296 end;
   297 end;
   297 
   298 
   298 
   299 
   433 fun read_term_pats T ctxt pats = read_termT_pats ctxt (map (rpair T) pats);
   434 fun read_term_pats T ctxt pats = read_termT_pats ctxt (map (rpair T) pats);
   434 val read_prop_pats = read_term_pats propT;
   435 val read_prop_pats = read_term_pats propT;
   435 
   436 
   436 val read_term = gen_read (read_term_sg true) I false false;
   437 val read_term = gen_read (read_term_sg true) I false false;
   437 val read_prop = gen_read (read_prop_sg true) I false false;
   438 val read_prop = gen_read (read_prop_sg true) I false false;
       
   439 val read_prop_schematic = gen_read (read_prop_sg true) I false true;
   438 val read_terms = gen_read (read_terms_sg true) map false false;
   440 val read_terms = gen_read (read_terms_sg true) map false false;
   439 val read_props = gen_read (read_props_sg true) map false;
   441 val read_props = gen_read (read_props_sg true) map false;
   440 
   442 
   441 end;
   443 end;
   442 
   444 
   760     fun gen_binds c = c |> add_binds_i (map (apsnd (Some o gen)) binds);
   762     fun gen_binds c = c |> add_binds_i (map (apsnd (Some o gen)) binds);
   761   in (ctxt' |> add_binds_i (map (apsnd Some) binds), (propss, gen_binds)) end;
   763   in (ctxt' |> add_binds_i (map (apsnd Some) binds), (propss, gen_binds)) end;
   762 
   764 
   763 in
   765 in
   764 
   766 
   765 val read_propp           = prep_propp false read_props read_prop_pats;
   767 val read_propp = prep_propp false read_props read_prop_pats;
   766 val cert_propp           = prep_propp false cert_props cert_prop_pats;
   768 val cert_propp = prep_propp false cert_props cert_prop_pats;
   767 val read_propp_schematic = prep_propp true read_props read_prop_pats;
   769 val read_propp_schematic = prep_propp true read_props read_prop_pats;
   768 val cert_propp_schematic = prep_propp true cert_props cert_prop_pats;
   770 val cert_propp_schematic = prep_propp true cert_props cert_prop_pats;
   769 
   771 
   770 val bind_propp             = gen_bind_propp read_propp;
   772 val bind_propp = gen_bind_propp read_propp;
   771 val bind_propp_i           = gen_bind_propp cert_propp;
   773 val bind_propp_i = gen_bind_propp cert_propp;
   772 val bind_propp_schematic   = gen_bind_propp read_propp_schematic;
   774 val bind_propp_schematic = gen_bind_propp read_propp_schematic;
   773 val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic;
   775 val bind_propp_schematic_i = gen_bind_propp cert_propp_schematic;
   774 
   776 
   775 end;
   777 end;
   776 
   778 
   777 
   779 
   884     val ctxt3 =
   886     val ctxt3 =
   885       ctxt2
   887       ctxt2
   886       |> map_context (fn (thy, data, ((asms_ct, asms_th), fixes), binds, thms, cases, defs) =>
   888       |> map_context (fn (thy, data, ((asms_ct, asms_th), fixes), binds, thms, cases, defs) =>
   887         (thy, data, ((asms_ct @ [(cprops, exp)], asms_th @ asmss), fixes), binds, thms,
   889         (thy, data, ((asms_ct @ [(cprops, exp)], asms_th @ asmss), fixes), binds, thms,
   888           cases, defs));
   890           cases, defs));
   889   in (warn_extra_tfrees ctxt ctxt3, (thmss, prems_of ctxt3)) end;
   891     val ctxt4 = ctxt3 |> put_thms ("prems", prems_of ctxt3);
       
   892   in (warn_extra_tfrees ctxt ctxt4, thmss) end;
   890 
   893 
   891 in
   894 in
   892 
   895 
   893 val assume = gen_assms (apsnd #1 o bind_propp);
   896 val assume = gen_assms (apsnd #1 o bind_propp);
   894 val assume_i = gen_assms (apsnd #1 o bind_propp_i);
   897 val assume_i = gen_assms (apsnd #1 o bind_propp_i);
   898 
   901 
   899 (* variables *)
   902 (* variables *)
   900 
   903 
   901 local
   904 local
   902 
   905 
   903 fun prep_vars prep_typ (ctxt, (xs, raw_T)) =
   906 fun prep_vars prep_typ no_internal (ctxt, (xs, raw_T)) =
   904   let
   907   let
   905     val _ = (case filter (not o Syntax.is_identifier) (map (no_skolem false ctxt) xs) of
   908     val _ = (case filter (not o Syntax.is_identifier) (map (no_skolem no_internal ctxt) xs) of
   906       [] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt));
   909       [] => () | bads => raise CONTEXT ("Bad variable name(s): " ^ commas_quote bads, ctxt));
   907 
   910 
   908     val opt_T = apsome (prep_typ ctxt) raw_T;
   911     val opt_T = apsome (prep_typ ctxt) raw_T;
   909     val T = if_none opt_T TypeInfer.logicT;
   912     val T = if_none opt_T TypeInfer.logicT;
   910     val ctxt' = ctxt |> predeclare_terms (map (fn x => Free (x, T)) xs);
   913     val ctxt' = ctxt |> predeclare_terms (map (fn x => Free (x, T)) xs);
   911   in (ctxt', (xs, opt_T)) end;
   914   in (ctxt', (xs, opt_T)) end;
   912 
   915 
   913 in
   916 in
   914 
   917 
   915 val read_vars = prep_vars read_typ;
   918 val read_vars = prep_vars read_typ true;
   916 val cert_vars = prep_vars cert_typ;
   919 val cert_vars = prep_vars cert_typ false;
   917 
   920 
   918 end;
   921 end;
   919 
   922 
   920 
   923 
   921 (* fix *)
   924 (* fix *)
   922 
   925 
   923 local
   926 local
   924 
       
   925 fun add_vars xs (fixes, names) =
       
   926   let
       
   927     val xs' = variantlist (xs, names);
       
   928     val fixes' = (xs ~~ map Syntax.skolem xs') @ fixes;
       
   929     val names' = xs' @ names;
       
   930   in (fixes', names') end;
       
   931 
   927 
   932 fun map_vars f = map_context (fn (thy, data, (assumes, vars), binds, thms, cases, defs) =>
   928 fun map_vars f = map_context (fn (thy, data, (assumes, vars), binds, thms, cases, defs) =>
   933   (thy, data, (assumes, f vars), binds, thms, cases, defs));
   929   (thy, data, (assumes, f vars), binds, thms, cases, defs));
   934 
   930 
   935 fun gen_fix prep raw_vars ctxt =
   931 fun err_dups ctxt xs = raise CONTEXT ("Duplicate variable(s): " ^ commas_quote xs, ctxt);
       
   932 
       
   933 fun add_vars _ xs (fixes, used) =
       
   934   let
       
   935     val xs' = variantlist (xs, used);
       
   936     val fixes' = (xs ~~ map Syntax.skolem xs') @ fixes;
       
   937     val used' = xs' @ used;
       
   938   in (fixes', used') end;
       
   939 
       
   940 fun add_vars_direct ctxt xs (fixes, used) =
       
   941   (case xs inter_string map #1 fixes of
       
   942     [] => ((xs ~~ xs) @ fixes, xs @ used)
       
   943   | dups => err_dups ctxt dups);
       
   944 
       
   945 fun gen_fix prep add raw_vars ctxt =
   936   let
   946   let
   937     val (ctxt', varss) = foldl_map prep (ctxt, raw_vars);
   947     val (ctxt', varss) = foldl_map prep (ctxt, raw_vars);
   938     val xs = flat (map fst varss);
   948     val xs = flat (map fst varss);
   939   in
   949   in
   940     (case Library.duplicates xs of
   950     (case Library.duplicates xs of [] => () | dups => err_dups ctxt dups);
   941       [] => ()
   951     ctxt' |> map_vars (add ctxt xs)
   942     | dups => raise CONTEXT ("Duplicate variable name(s): " ^ commas_quote dups, ctxt));
       
   943     ctxt' |> map_vars (add_vars xs)
       
   944   end;
   952   end;
   945 
   953 
   946 in
   954 in
   947 
   955 
   948 val fix = gen_fix read_vars;
   956 val fix = gen_fix read_vars add_vars;
   949 val fix_i = gen_fix cert_vars;
   957 val fix_i = gen_fix cert_vars add_vars;
       
   958 val fix_direct = gen_fix cert_vars add_vars_direct;
   950 
   959 
   951 end;
   960 end;
   952 
   961 
   953 
   962 
   954 (*Note: improper use may result in variable capture / dynamic scoping!*)
   963 (*Note: improper use may result in variable capture / dynamic scoping!*)