src/Pure/Isar/proof_context.ML
changeset 7606 7905a74eb068
parent 7575 e1e2d07287d8
child 7663 460fedf14b09
equal deleted inserted replaced
7605:8bbfcb54054e 7606:7905a74eb068
    31   val cert_term: context -> term -> term
    31   val cert_term: context -> term -> term
    32   val cert_prop: context -> term -> term
    32   val cert_prop: context -> term -> term
    33   val declare_term: term -> context -> context
    33   val declare_term: term -> context -> context
    34   val declare_terms: term list -> context -> context
    34   val declare_terms: term list -> context -> context
    35   val declare_thm: thm -> context -> context
    35   val declare_thm: thm -> context -> context
    36   val add_binds: (indexname * string) list -> context -> context
    36   val add_binds: (indexname * string option) list -> context -> context
    37   val add_binds_i: (indexname * term) list -> context -> context
    37   val add_binds_i: (indexname * term option) list -> context -> context
    38   val match_binds: (string list * string) list -> context -> context
    38   val match_binds: (string list * string) list -> context -> context
    39   val match_binds_i: (term list * term) list -> context -> context
    39   val match_binds_i: (term list * term) list -> context -> context
    40   val bind_propp: context * (string * (string list * string list)) -> context * term
    40   val bind_propp: context * (string * (string list * string list)) -> context * term
    41   val bind_propp_i: context * (term * (term list * term list)) -> context * term
    41   val bind_propp_i: context * (term * (term list * term list)) -> context * term
    42   val auto_bind_goal: term -> context -> context
    42   val auto_bind_goal: term -> context -> context
    45   val get_thms: context -> string -> thm list
    45   val get_thms: context -> string -> thm list
    46   val get_thmss: context -> string list -> thm list
    46   val get_thmss: context -> string list -> thm list
    47   val put_thm: string * thm -> context -> context
    47   val put_thm: string * thm -> context -> context
    48   val put_thms: string * thm list -> context -> context
    48   val put_thms: string * thm list -> context -> context
    49   val put_thmss: (string * thm list) list -> context -> context
    49   val put_thmss: (string * thm list) list -> context -> context
       
    50   val reset_thms: string -> context -> context
    50   val have_thmss: thm list -> string -> context attribute list
    51   val have_thmss: thm list -> string -> context attribute list
    51     -> (thm list * context attribute list) list -> context -> context * (string * thm list)
    52     -> (thm list * context attribute list) list -> context -> context * (string * thm list)
    52   val assumptions: context -> (cterm * ((int -> tactic) * (int -> tactic))) list
    53   val assumptions: context -> (cterm * ((int -> tactic) * (int -> tactic))) list
    53   val fixed_names: context -> string list
    54   val fixed_names: context -> string list
    54   val assume: ((int -> tactic) * (int -> tactic))
    55   val assume: ((int -> tactic) * (int -> tactic))
    84     data: Object.T Symtab.table,                                        (*user data*)
    85     data: Object.T Symtab.table,                                        (*user data*)
    85     asms:
    86     asms:
    86       ((cterm * ((int -> tactic) * (int -> tactic))) list *		(*assumes: A ==> _*)
    87       ((cterm * ((int -> tactic) * (int -> tactic))) list *		(*assumes: A ==> _*)
    87         (string * thm list) list) *
    88         (string * thm list) list) *
    88       ((string * string) list * string list),                           (*fixes: !!x. _*)
    89       ((string * string) list * string list),                           (*fixes: !!x. _*)
    89     binds: (term * typ) Vartab.table,                                   (*term bindings*)
    90     binds: (term * typ) option Vartab.table,                            (*term bindings*)
    90     thms: thm list Symtab.table,                                        (*local thms*)
    91     thms: thm list option Symtab.table,                                 (*local thms*)
    91     defs:
    92     defs:
    92       typ Vartab.table *                                                (*type constraints*)
    93       typ Vartab.table *                                                (*type constraints*)
    93       sort Vartab.table *                                               (*default sorts*)
    94       sort Vartab.table *                                               (*default sorts*)
    94       int *                                                             (*maxidx*)
    95       int *                                                             (*maxidx*)
    95       string list};                                                     (*used type var names*)
    96       string list};                                                     (*used type var names*)
   132   end;
   133   end;
   133 
   134 
   134 
   135 
   135 (* term bindings *)
   136 (* term bindings *)
   136 
   137 
       
   138 val smash_option = fn (_, None) => None | (xi, Some b) => Some (xi, b);
       
   139 
   137 fun strings_of_binds (ctxt as Context {binds, ...}) =
   140 fun strings_of_binds (ctxt as Context {binds, ...}) =
   138   let
   141   let
   139     val prt_term = Sign.pretty_term (sign_of ctxt);
   142     val prt_term = Sign.pretty_term (sign_of ctxt);
   140     fun pretty_bind (xi, (t, T)) = prt_term (Logic.mk_equals (Var (xi, T), t));
   143     fun pretty_bind (xi, (t, T)) = prt_term (Logic.mk_equals (Var (xi, T), t));
       
   144     val bs = mapfilter smash_option (Vartab.dest binds);
   141   in
   145   in
   142     if Vartab.is_empty binds andalso not (! verbose) then []
   146     if null bs andalso not (! verbose) then []
   143     else [Pretty.string_of (Pretty.big_list "term bindings:"
   147     else [Pretty.string_of (Pretty.big_list "term bindings:" (map pretty_bind bs))]
   144       (map pretty_bind (Vartab.dest binds)))]
       
   145   end;
   148   end;
   146 
   149 
   147 val print_binds = seq writeln o strings_of_binds;
   150 val print_binds = seq writeln o strings_of_binds;
   148 
   151 
   149 
   152 
   150 (* local theorems *)
   153 (* local theorems *)
   151 
   154 
   152 fun strings_of_thms (Context {thms, ...}) =
   155 fun strings_of_thms (Context {thms, ...}) =
   153   strings_of_items pretty_thm "local theorems:" (Symtab.dest thms);
   156   strings_of_items pretty_thm "local theorems:" (mapfilter smash_option (Symtab.dest thms));
   154 
   157 
   155 val print_thms = seq writeln o strings_of_thms;
   158 val print_thms = seq writeln o strings_of_thms;
   156 
   159 
   157 
   160 
   158 (* main context *)
   161 (* main context *)
   173 
   176 
   174     (*theory*)
   177     (*theory*)
   175     val pretty_thy = Pretty.block [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg sign];
   178     val pretty_thy = Pretty.block [Pretty.str "Theory:", Pretty.brk 1, Sign.pretty_sg sign];
   176 
   179 
   177     (*fixes*)
   180     (*fixes*)
       
   181     fun prt_fix (x, x') = Pretty.block
       
   182       [prt_term (Syntax.free x), Pretty.str " =", Pretty.brk 1, prt_term (Syntax.free x')];
       
   183 
   178     fun prt_fixes xs = Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
   184     fun prt_fixes xs = Pretty.block (Pretty.str "fixed variables:" :: Pretty.brk 1 ::
   179       Pretty.commas (map (fn (x, x') => Pretty.str (x ^ " = " ^ x')) xs));
   185       Pretty.commas (map prt_fix xs));
   180 
   186 
   181 
   187 
   182     (* defaults *)
   188     (* defaults *)
   183 
   189 
   184     fun prt_atom prt prtT (x, X) = Pretty.block
   190     fun prt_atom prt prtT (x, X) = Pretty.block
   385     (*raised when norm has no effect on a term, to do sharing instead of copying*)
   391     (*raised when norm has no effect on a term, to do sharing instead of copying*)
   386     exception SAME;
   392     exception SAME;
   387 
   393 
   388     fun norm (t as Var (xi, T)) =
   394     fun norm (t as Var (xi, T)) =
   389           (case Vartab.lookup (binds, xi) of
   395           (case Vartab.lookup (binds, xi) of
   390             Some (u, U) =>
   396             Some (Some (u, U)) =>
   391               if T = U then (norm u handle SAME => u)
   397               if T = U then (norm u handle SAME => u)
   392               else raise TYPE ("norm_term: ill-typed variable assigment", [T, U], [t, u])
   398               else raise TYPE ("norm_term: ill-typed variable assigment", [T, U], [t, u])
   393           | None => raise CONTEXT ("Unbound schematic variable: " ^ Syntax.string_of_vname xi, ctxt))
   399           | _ => raise CONTEXT ("Unbound schematic variable: " ^ Syntax.string_of_vname xi, ctxt))
   394       | norm (Abs (a, T, body)) =  Abs (a, T, norm body)
   400       | norm (Abs (a, T, body)) =  Abs (a, T, norm body)
   395       | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body))
   401       | norm (Abs (_, _, body) $ t) = normh (subst_bound (t, body))
   396       | norm (f $ t) =
   402       | norm (f $ t) =
   397           ((case norm f of
   403           ((case norm f of
   398             Abs (_, _, body) => normh (subst_bound (t, body))
   404             Abs (_, _, body) => normh (subst_bound (t, body))
   427 
   433 
   428 fun gen_read read app is_pat (ctxt as Context {binds, defs = (types, sorts, _, used), ...}) s =
   434 fun gen_read read app is_pat (ctxt as Context {binds, defs = (types, sorts, _, used), ...}) s =
   429   let
   435   let
   430     fun def_type xi =
   436     fun def_type xi =
   431       (case Vartab.lookup (types, xi) of
   437       (case Vartab.lookup (types, xi) of
   432         None => if is_pat then None else apsome #2 (Vartab.lookup (binds, xi))
   438         None =>
       
   439           if is_pat then None
       
   440           else (case Vartab.lookup (binds, xi) of Some (Some (_, T)) => Some T | _ => None)
   433       | some => some);
   441       | some => some);
   434 
   442 
   435     fun def_sort xi = Vartab.lookup (sorts, xi);
   443     fun def_sort xi = Vartab.lookup (sorts, xi);
   436   in
   444   in
   437     (transform_error (read (sign_of ctxt) (def_type, def_sort, used)) s
   445     (transform_error (read (sign_of ctxt) (def_type, def_sort, used)) s
   510 
   518 
   511 (** bindings **)
   519 (** bindings **)
   512 
   520 
   513 (* update_binds *)
   521 (* update_binds *)
   514 
   522 
       
   523 fun del_bind (ctxt, xi) =
       
   524   ctxt
       
   525   |> map_context (fn (thy, data, asms, binds, thms, defs) =>
       
   526       (thy, data, asms, Vartab.update ((xi, None), binds), thms, defs));
       
   527 
   515 fun upd_bind (ctxt, (xi, t)) =
   528 fun upd_bind (ctxt, (xi, t)) =
   516   let val T = fastype_of t in
   529   let val T = fastype_of t in
   517     ctxt
   530     ctxt
   518     |> declare_term t
   531     |> declare_term t
   519     |> map_context (fn (thy, data, asms, binds, thms, defs) =>
   532     |> map_context (fn (thy, data, asms, binds, thms, defs) =>
   520         (thy, data, asms, Vartab.update ((xi, (t, T)), binds), thms, defs))
   533         (thy, data, asms, Vartab.update ((xi, Some (t, T)), binds), thms, defs))
   521   end;
   534   end;
       
   535 
       
   536 fun del_upd_bind (ctxt, (xi, None)) = del_bind (ctxt, xi)
       
   537   | del_upd_bind (ctxt, (xi, Some t)) = upd_bind (ctxt, (xi, t));
   522 
   538 
   523 fun update_binds bs ctxt = foldl upd_bind (ctxt, bs);
   539 fun update_binds bs ctxt = foldl upd_bind (ctxt, bs);
   524 fun update_binds_env env = (*Envir.norm_term ensures proper type instantiation*)
   540 fun update_binds_env env = (*Envir.norm_term ensures proper type instantiation*)
   525   update_binds (map (apsnd (Envir.norm_term env)) (Envir.alist_of env));
   541   update_binds (map (apsnd (Envir.norm_term env)) (Envir.alist_of env));
   526 
   542 
       
   543 fun delete_update_binds bs ctxt = foldl del_upd_bind (ctxt, bs);
       
   544 
   527 
   545 
   528 (* add_binds(_i) -- sequential *)
   546 (* add_binds(_i) -- sequential *)
   529 
   547 
   530 fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) =
   548 fun gen_bind prep (ctxt, (xi as (x, _), raw_t)) =
   531   ctxt |> update_binds [(xi, prep ctxt raw_t)];
   549   ctxt |> delete_update_binds [(xi, apsome (prep ctxt) raw_t)];
   532 
   550 
   533 fun gen_binds prep binds ctxt = foldl (gen_bind prep) (ctxt, binds);
   551 fun gen_binds prep binds ctxt = foldl (gen_bind prep) (ctxt, binds);
   534 
   552 
   535 val add_binds = gen_binds read_term;
   553 val add_binds = gen_binds read_term;
   536 val add_binds_i = gen_binds cert_term;
   554 val add_binds_i = gen_binds cert_term;
   541 fun prep_declare_match (prep_pat, prep) (ctxt, (raw_pats, raw_t)) =
   559 fun prep_declare_match (prep_pat, prep) (ctxt, (raw_pats, raw_t)) =
   542   let
   560   let
   543     val t = prep ctxt raw_t;
   561     val t = prep ctxt raw_t;
   544     val ctxt' = ctxt |> declare_term t;
   562     val ctxt' = ctxt |> declare_term t;
   545     val matches = map (fn (f, raw_pat) => (prep_pat ctxt' raw_pat, f t)) raw_pats;
   563     val matches = map (fn (f, raw_pat) => (prep_pat ctxt' raw_pat, f t)) raw_pats;
   546            (* FIXME seq / par / simult (??) *)
       
   547   in (ctxt', (matches, t)) end;
   564   in (ctxt', (matches, t)) end;
   548 
   565 
   549 fun gen_match_binds _ [] ctxt = ctxt
   566 fun gen_match_binds _ [] ctxt = ctxt
   550   | gen_match_binds prepp args ctxt =
   567   | gen_match_binds prepp args ctxt =
   551       let
   568       let
   587 
   604 
   588 (* get_thm(s) *)
   605 (* get_thm(s) *)
   589 
   606 
   590 fun get_thm (ctxt as Context {thy, thms, ...}) name =
   607 fun get_thm (ctxt as Context {thy, thms, ...}) name =
   591   (case Symtab.lookup (thms, name) of
   608   (case Symtab.lookup (thms, name) of
   592     Some [th] => th
   609     Some (Some [th]) => th
   593   | Some _ => raise CONTEXT ("Single theorem expected: " ^ quote name, ctxt)
   610   | Some (Some _) => raise CONTEXT ("Single theorem expected: " ^ quote name, ctxt)
   594   | None => (PureThy.get_thm thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
   611   | _ => (PureThy.get_thm thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
   595 
   612 
   596 fun get_thms (ctxt as Context {thy, thms, ...}) name =
   613 fun get_thms (ctxt as Context {thy, thms, ...}) name =
   597   (case Symtab.lookup (thms, name) of
   614   (case Symtab.lookup (thms, name) of
   598     Some ths => ths
   615     Some (Some ths) => ths
   599   | None => (PureThy.get_thms thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
   616   | _ => (PureThy.get_thms thy name handle THEORY (msg, _) => raise CONTEXT (msg, ctxt)));
   600 
   617 
   601 fun get_thmss ctxt names = flat (map (get_thms ctxt) names);
   618 fun get_thmss ctxt names = flat (map (get_thms ctxt) names);
   602 
   619 
   603 
   620 
   604 (* put_thm(s) *)
   621 (* put_thm(s) *)
   605 
   622 
   606 fun put_thms ("", _) = I
   623 fun put_thms ("", _) = I
   607   | put_thms (name, ths) = map_context
   624   | put_thms (name, ths) = map_context
   608       (fn (thy, data, asms, binds, thms, defs) =>
   625       (fn (thy, data, asms, binds, thms, defs) =>
   609         (thy, data, asms, binds, Symtab.update ((name, ths), thms), defs));
   626         (thy, data, asms, binds, Symtab.update ((name, Some ths), thms), defs));
   610 
   627 
   611 fun put_thm (name, th) = put_thms (name, [th]);
   628 fun put_thm (name, th) = put_thms (name, [th]);
   612 
   629 
   613 fun put_thmss [] ctxt = ctxt
   630 fun put_thmss [] ctxt = ctxt
   614   | put_thmss (th :: ths) ctxt = ctxt |> put_thms th |> put_thmss ths;
   631   | put_thmss (th :: ths) ctxt = ctxt |> put_thms th |> put_thmss ths;
       
   632 
       
   633 
       
   634 (* reset_thms *)
       
   635 
       
   636 fun reset_thms name = map_context (fn (thy, data, asms, binds, thms, defs) =>
       
   637   (thy, data, asms, binds, Symtab.update ((name, None), thms), defs));
   615 
   638 
   616 
   639 
   617 (* have_thmss *)
   640 (* have_thmss *)
   618 
   641 
   619 fun have_thmss more_ths name more_attrs ths_attrs ctxt =
   642 fun have_thmss more_ths name more_attrs ths_attrs ctxt =