src/Pure/Isar/proof_context.ML
changeset 7670 e302e4269087
parent 7663 460fedf14b09
child 7679 99912beb8fa0
equal deleted inserted replaced
7669:fcd9c2050836 7670:e302e4269087
    21   val strings_of_context: context -> string list
    21   val strings_of_context: context -> string list
    22   val print_proof_data: theory -> unit
    22   val print_proof_data: theory -> unit
    23   val init: theory -> context
    23   val init: theory -> context
    24   val def_sort: context -> indexname -> sort option
    24   val def_sort: context -> indexname -> sort option
    25   val def_type: context -> bool -> indexname -> typ option
    25   val def_type: context -> bool -> indexname -> typ option
       
    26   val cert_skolem: context -> string -> string * typ option
    26   val read_typ: context -> string -> typ
    27   val read_typ: context -> string -> typ
    27   val cert_typ: context -> typ -> typ
    28   val cert_typ: context -> typ -> typ
    28   val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
    29   val read_termTs: context -> (string * typ) list -> term list * (indexname * typ) list
    29   val read_term: context -> string -> term
    30   val read_term: context -> string -> term
    30   val read_prop: context -> string -> term
    31   val read_prop: context -> string -> term
    32   val read_prop_pat: context -> string -> term
    33   val read_prop_pat: context -> string -> term
    33   val cert_term: context -> term -> term
    34   val cert_term: context -> term -> term
    34   val cert_prop: context -> term -> term
    35   val cert_prop: context -> term -> term
    35   val declare_term: term -> context -> context
    36   val declare_term: term -> context -> context
    36   val declare_terms: term list -> context -> context
    37   val declare_terms: term list -> context -> context
    37   val declare_thm: thm -> context -> context
       
    38   val add_binds: (indexname * string option) list -> context -> context
    38   val add_binds: (indexname * string option) list -> context -> context
    39   val add_binds_i: (indexname * term option) list -> context -> context
    39   val add_binds_i: (indexname * term option) list -> context -> context
    40   val match_binds: (string list * string) list -> context -> context
    40   val match_binds: (string list * string) list -> context -> context
    41   val match_binds_i: (term list * term) list -> context -> context
    41   val match_binds_i: (term list * term) list -> context -> context
    42   val bind_propp: context * (string * (string list * string list)) -> context * term
    42   val bind_propp: context * (string * (string list * string list)) -> context * term
    92     binds: (term * typ) option Vartab.table,                            (*term bindings*)
    92     binds: (term * typ) option Vartab.table,                            (*term bindings*)
    93     thms: thm list option Symtab.table,                                 (*local thms*)
    93     thms: thm list option Symtab.table,                                 (*local thms*)
    94     defs:
    94     defs:
    95       typ Vartab.table *                                                (*type constraints*)
    95       typ Vartab.table *                                                (*type constraints*)
    96       sort Vartab.table *                                               (*default sorts*)
    96       sort Vartab.table *                                               (*default sorts*)
    97       int *                                                             (*maxidx*)
       
    98       string list};                                                     (*used type var names*)
    97       string list};                                                     (*used type var names*)
    99 
    98 
   100 exception CONTEXT of string * context;
    99 exception CONTEXT of string * context;
   101 
   100 
   102 
   101 
   166   (case prems_of ctxt of
   165   (case prems_of ctxt of
   167     [] => []
   166     [] => []
   168   | prems => [Pretty.string_of (Pretty.big_list "prems:" (map pretty_thm prems))]);
   167   | prems => [Pretty.string_of (Pretty.big_list "prems:" (map pretty_thm prems))]);
   169 
   168 
   170 fun strings_of_context (ctxt as Context {asms = (_, (fixes, _)),
   169 fun strings_of_context (ctxt as Context {asms = (_, (fixes, _)),
   171     defs = (types, sorts, maxidx, used), ...}) =
   170     defs = (types, sorts, used), ...}) =
   172   let
   171   let
   173     val sign = sign_of ctxt;
   172     val sign = sign_of ctxt;
   174 
   173 
   175     val prt_term = Sign.pretty_term sign;
   174     val prt_term = Sign.pretty_term sign;
   176     val prt_typ = Sign.pretty_typ sign;
   175     val prt_typ = Sign.pretty_typ sign;
   206     strings_of_prems ctxt @
   205     strings_of_prems ctxt @
   207     verb strings_of_binds ctxt @
   206     verb strings_of_binds ctxt @
   208     verb strings_of_thms ctxt @
   207     verb strings_of_thms ctxt @
   209     verb_string (Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
   208     verb_string (Pretty.big_list "type constraints:" (map prt_defT (Vartab.dest types))) @
   210     verb_string (Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @
   209     verb_string (Pretty.big_list "default sorts:" (map prt_defS (Vartab.dest sorts))) @
   211     verb_string (Pretty.str ("maxidx: " ^ string_of_int maxidx)) @
       
   212     verb_string (Pretty.strs ("used type variable names:" :: used))
   210     verb_string (Pretty.strs ("used type variable names:" :: used))
   213   end;
   211   end;
   214 
   212 
   215 
   213 
   216 
   214 
   301 (* init context *)
   299 (* init context *)
   302 
   300 
   303 fun init thy =
   301 fun init thy =
   304   let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in
   302   let val data = Symtab.map (fn (_, (f, _)) => f thy) (ProofDataData.get thy) in
   305     make_context (thy, data, (([], []), ([], [])), Vartab.empty, Symtab.empty,
   303     make_context (thy, data, (([], []), ([], [])), Vartab.empty, Symtab.empty,
   306       (Vartab.empty, Vartab.empty, 0, []))
   304       (Vartab.empty, Vartab.empty, []))
   307         (*Note: maxidx = 0 is workaround for obscure bug in Unify.smash_unifiers*)
       
   308   end;
   305   end;
   309 
   306 
   310 
   307 
   311 (** default sorts and types **)
   308 (** default sorts and types **)
   312 
   309 
   313 fun def_sort (Context {defs = (_, sorts, _, _), ...}) xi = Vartab.lookup (sorts, xi);
   310 fun def_sort (Context {defs = (_, sorts, _), ...}) xi = Vartab.lookup (sorts, xi);
   314 
   311 
   315 fun def_type (Context {binds, defs = (types, _, _, _), ...}) is_pat xi =
   312 fun def_type (Context {binds, defs = (types, _, _), ...}) is_pat xi =
   316   (case Vartab.lookup (types, xi) of
   313   (case Vartab.lookup (types, xi) of
   317     None =>
   314     None =>
   318       if is_pat then None
   315       if is_pat then None
   319       else (case Vartab.lookup (binds, xi) of Some (Some (_, T)) => Some T | _ => None)
   316       else (case Vartab.lookup (binds, xi) of Some (Some (_, T)) => Some T | _ => None)
   320   | some => some);
   317   | some => some);
   368     if T = propT then t
   365     if T = propT then t
   369     else raise TERM ("Term not of type prop", [t])
   366     else raise TERM ("Term not of type prop", [t])
   370   end;
   367   end;
   371 
   368 
   372 
   369 
   373 (* intern_skolem *)
   370 (* internalize Skolem constants *)
   374 
   371 
   375 fun get_skolem (Context {asms = (_, (fixes, _)), ...}) x = assoc (fixes, x);
   372 fun get_skolem (Context {asms = (_, (fixes, _)), ...}) x = assoc (fixes, x);
   376 
   373 
   377 fun check_skolem ctxt check x =
   374 fun check_skolem ctxt check x =
   378   if not check then x
   375   if not check then x
   390           | None => t)
   387           | None => t)
   391       | intern (t $ u) = intern t $ intern u
   388       | intern (t $ u) = intern t $ intern u
   392       | intern (Abs (x, T, t)) = Abs (x, T, intern t)
   389       | intern (Abs (x, T, t)) = Abs (x, T, intern t)
   393       | intern a = a;
   390       | intern a = a;
   394   in intern end;
   391   in intern end;
       
   392 
       
   393 fun cert_skolem ctxt x =
       
   394   (case get_skolem ctxt x of
       
   395     None => raise CONTEXT ("Undeclared variable: " ^ quote x, ctxt)
       
   396   | Some x' => (x', def_type ctxt false (x', ~1)));
   395 
   397 
   396 
   398 
   397 (* norm_term *)
   399 (* norm_term *)
   398 
   400 
   399 (*beta normal form for terms (not eta normal form), chase variables in
   401 (*beta normal form for terms (not eta normal form), chase variables in
   442 end;
   444 end;
   443 
   445 
   444 
   446 
   445 (* read terms *)
   447 (* read terms *)
   446 
   448 
   447 fun gen_read read app is_pat (ctxt as Context {defs = (_, _, _, used), ...}) s =
   449 fun gen_read read app is_pat (ctxt as Context {defs = (_, _, used), ...}) s =
   448   (transform_error (read (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s
   450   (transform_error (read (sign_of ctxt) (def_type ctxt is_pat, def_sort ctxt, used)) s
   449     handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
   451     handle TERM (msg, _) => raise CONTEXT (msg, ctxt)
   450     | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
   452     | ERROR_MESSAGE msg => raise CONTEXT (msg, ctxt))
   451   |> app (intern_skolem ctxt true)
   453   |> app (intern_skolem ctxt true)
   452   |> app (if is_pat then I else norm_term ctxt)
   454   |> app (if is_pat then I else norm_term ctxt)
   498 fun map_defaults f = map_context
   500 fun map_defaults f = map_context
   499   (fn (thy, data, asms, binds, thms, defs) => (thy, data, asms, binds, thms, f defs));
   501   (fn (thy, data, asms, binds, thms, defs) => (thy, data, asms, binds, thms, f defs));
   500 
   502 
   501 fun declare (ctxt as Context {asms = (_, (fixes, _)), ...}, t) =
   503 fun declare (ctxt as Context {asms = (_, (fixes, _)), ...}, t) =
   502   ctxt
   504   ctxt
   503   |> map_defaults (fn (types, sorts, maxidx, used) => (ins_types (types, t), sorts, maxidx, used))
   505   |> map_defaults (fn (types, sorts, used) => (ins_types (types, t), sorts, used))
   504   |> map_defaults (fn (types, sorts, maxidx, used) => (types, ins_sorts (sorts, t), maxidx, used))
   506   |> map_defaults (fn (types, sorts, used) => (types, ins_sorts (sorts, t), used))
   505   |> map_defaults (fn (types, sorts, maxidx, used) => (types, sorts, maxidx, ins_used (used, t)))
   507   |> map_defaults (fn (types, sorts, used) => (types, sorts, ins_used (used, t)))
   506   |> map_defaults (fn (types, sorts, maxidx, used) =>
   508   |> map_defaults (fn (types, sorts, used) =>
   507       (types, sorts, Int.max (maxidx, Term.maxidx_of_term t), used))
   509       (ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) (fixes, types), sorts, used));
   508   |> map_defaults (fn (types, sorts, maxidx, used) =>
       
   509       (ins_skolem (fn x => Vartab.lookup (types, (x, ~1))) (fixes, types), sorts, maxidx, used));
       
   510 
   510 
   511 
   511 
   512 fun declare_term t ctxt = declare (ctxt, t);
   512 fun declare_term t ctxt = declare (ctxt, t);
   513 fun declare_terms ts ctxt = foldl declare (ctxt, ts);
   513 fun declare_terms ts ctxt = foldl declare (ctxt, ts);
   514 
       
   515 fun declare_thm thm ctxt =
       
   516   let val {prop, hyps, ...} = Thm.rep_thm thm
       
   517   in ctxt |> declare_terms (prop :: hyps) end;
       
   518 
   514 
   519 
   515 
   520 
   516 
   521 (** bindings **)
   517 (** bindings **)
   522 
   518 
   563     val t = prep ctxt raw_t;
   559     val t = prep ctxt raw_t;
   564     val ctxt' = ctxt |> declare_term t;
   560     val ctxt' = ctxt |> declare_term t;
   565     val matches = map (fn (f, raw_pat) => (prep_pat ctxt' raw_pat, f t)) raw_pats;
   561     val matches = map (fn (f, raw_pat) => (prep_pat ctxt' raw_pat, f t)) raw_pats;
   566   in (ctxt', (matches, t)) end;
   562   in (ctxt', (matches, t)) end;
   567 
   563 
       
   564 fun maxidx_of_pair (t1, t2) = Int.max (Term.maxidx_of_term t1, Term.maxidx_of_term t2);
       
   565 
   568 fun gen_match_binds _ [] ctxt = ctxt
   566 fun gen_match_binds _ [] ctxt = ctxt
   569   | gen_match_binds prepp args ctxt =
   567   | gen_match_binds prepp args ctxt =
   570       let
   568       let
   571         val raw_pairs = map (apfst (map (pair I))) args;
   569         val raw_pairs = map (apfst (map (pair I))) args;
   572         val (ctxt', matches) = foldl_map (prep_declare_match prepp) (ctxt, raw_pairs);
   570         val (ctxt', matches) = foldl_map (prep_declare_match prepp) (ctxt, raw_pairs);
   573         val pairs = flat (map #1 matches);
   571         val pairs = flat (map #1 matches);
   574         val Context {defs = (_, _, maxidx, _), ...} = ctxt';
   572         val maxidx = foldl (fn (i, p) => Int.max (i, maxidx_of_pair p)) (~1, pairs);
   575         val envs = Unify.smash_unifiers (sign_of ctxt', Envir.empty maxidx, pairs);
   573         val envs = Unify.smash_unifiers (sign_of ctxt', Envir.empty maxidx, pairs);
   576         val env =
   574         val env =
   577           (case Seq.pull envs of
   575           (case Seq.pull envs of
   578             None => raise CONTEXT ("Pattern match failed!", ctxt')
   576             None => raise CONTEXT ("Pattern match failed!", ctxt')
   579           | Some (env, _) => env);
   577           | Some (env, _) => env);
   665 local
   663 local
   666 
   664 
   667 fun gen_assm prepp tac (ctxt, (name, attrs, raw_prop_pats)) =
   665 fun gen_assm prepp tac (ctxt, (name, attrs, raw_prop_pats)) =
   668   let
   666   let
   669     val (ctxt', props) = foldl_map prepp (ctxt, raw_prop_pats);
   667     val (ctxt', props) = foldl_map prepp (ctxt, raw_prop_pats);
   670     val Context {defs = (_, _, maxidx, _), ...} = ctxt';
       
   671 
   668 
   672     val cprops = map (Thm.cterm_of (sign_of ctxt')) props;
   669     val cprops = map (Thm.cterm_of (sign_of ctxt')) props;
   673     val cprops_tac = map (rpair tac) cprops;
   670     val cprops_tac = map (rpair tac) cprops;
   674     val asms = map (Drule.forall_elim_vars (maxidx + 1) o Drule.assume_goal) cprops;
   671     val asms = map (Drule.forall_elim_vars 0 o Drule.assume_goal) cprops;
   675 
   672 
   676     val ths = map (fn th => ([th], [])) asms;
   673     val ths = map (fn th => ([th], [])) asms;
   677     val (ctxt'', (_, thms)) =
   674     val (ctxt'', (_, thms)) =
   678       ctxt'
   675       ctxt'
   679       |> auto_bind_facts name props
   676       |> auto_bind_facts name props