src/Pure/Isar/proof_context.ML
changeset 24675 2be1253a20d3
parent 24612 d1b315bdb8d7
child 24684 80da599dea37
equal deleted inserted replaced
24674:4ade7ac6a21c 24675:2be1253a20d3
   150   val get_case: Proof.context -> string -> string option list -> RuleCases.T
   150   val get_case: Proof.context -> string -> string option list -> RuleCases.T
   151   val add_notation: Syntax.mode -> (term * mixfix) list ->
   151   val add_notation: Syntax.mode -> (term * mixfix) list ->
   152     Proof.context -> Proof.context
   152     Proof.context -> Proof.context
   153   val target_notation: Syntax.mode -> (term * mixfix) list -> morphism ->
   153   val target_notation: Syntax.mode -> (term * mixfix) list -> morphism ->
   154     Context.generic -> Context.generic
   154     Context.generic -> Context.generic
   155   val set_expand_abbrevs: bool -> Proof.context -> Proof.context
       
   156   val add_abbrev: string -> bstring * term -> Proof.context -> (term * term) * Proof.context
   155   val add_abbrev: string -> bstring * term -> Proof.context -> (term * term) * Proof.context
   157   val verbose: bool ref
   156   val verbose: bool ref
   158   val setmp_verbose: ('a -> 'b) -> 'a -> 'b
   157   val setmp_verbose: ('a -> 'b) -> 'a -> 'b
   159   val print_syntax: Proof.context -> unit
   158   val print_syntax: Proof.context -> unit
   160   val print_abbrevs: Proof.context -> unit
   159   val print_abbrevs: Proof.context -> unit
   198 
   197 
   199 (** Isar proof context information **)
   198 (** Isar proof context information **)
   200 
   199 
   201 datatype ctxt =
   200 datatype ctxt =
   202   Ctxt of
   201   Ctxt of
   203    {mode: mode,                                   (*inner syntax mode*)
   202    {mode: mode,                                       (*inner syntax mode*)
   204     naming: NameSpace.naming,                     (*local naming conventions*)
   203     naming: NameSpace.naming,                         (*local naming conventions*)
   205     syntax: LocalSyntax.T,                        (*local syntax*)
   204     syntax: LocalSyntax.T,                            (*local syntax*)
   206     consts: Consts.T * Consts.T,                  (*global/local consts*)
   205     consts: Consts.T * Consts.T,                      (*global/local consts*)
   207     thms: thm list NameSpace.table * FactIndex.T, (*local thms*)
   206     thms: thm list NameSpace.table * FactIndex.T,     (*local thms*)
   208     cases: (string * (RuleCases.T * bool)) list}; (*local contexts*)
   207     cases: (string * (RuleCases.T * bool)) list};     (*local contexts*)
   209 
   208 
   210 fun make_ctxt (mode, naming, syntax, consts, thms, cases) =
   209 fun make_ctxt (mode, naming, syntax, consts, thms, cases) =
   211   Ctxt {mode = mode, naming = naming, syntax = syntax,
   210   Ctxt {mode = mode, naming = naming, syntax = syntax,
   212     consts = consts, thms = thms, cases = cases};
   211     consts = consts, thms = thms, cases = cases};
   213 
   212 
   301 
   300 
   302 (** pretty printing **)
   301 (** pretty printing **)
   303 
   302 
   304 local
   303 local
   305 
   304 
   306 fun rewrite_term warn thy rews t =
   305 fun rewrite_term thy rews t =
   307   if can Term.type_of t then Pattern.rewrite_term thy rews [] t
   306   if can Term.type_of t then Pattern.rewrite_term thy rews [] t else t;
   308   else (if warn then warning "Printing malformed term -- cannot expand abbreviations" else (); t);
       
   309 
   307 
   310 fun pretty_term' abbrevs ctxt t =
   308 fun pretty_term' abbrevs ctxt t =
   311   let
   309   let
   312     val thy = theory_of ctxt;
   310     val thy = theory_of ctxt;
   313     val syntax = syntax_of ctxt;
   311     val syntax = syntax_of ctxt;
   314     val consts = consts_of ctxt;
   312     val consts = consts_of ctxt;
   315     val do_abbrevs = abbrevs andalso not (print_mode_active "no_abbrevs");
   313     val do_abbrevs = abbrevs andalso not (print_mode_active "no_abbrevs");
   316     val t' = t
   314     val t' = t
   317       |> do_abbrevs ? rewrite_term true thy (Consts.abbrevs_of consts (print_mode_value () @ [""]))
   315       |> do_abbrevs ? rewrite_term thy (Consts.abbrevs_of consts (print_mode_value () @ [""]))
   318       |> do_abbrevs ? rewrite_term false thy (Consts.abbrevs_of consts [Syntax.internalM])
   316       |> do_abbrevs ? rewrite_term thy (Consts.abbrevs_of consts [Syntax.internalM])
   319       |> Sign.extern_term (Consts.extern_early consts) thy
   317       |> Sign.extern_term (Consts.extern_early consts) thy
   320       |> LocalSyntax.extern_term syntax;
   318       |> LocalSyntax.extern_term syntax;
   321   in Sign.pretty_term' ctxt (LocalSyntax.syn_of syntax) (Consts.extern consts) t' end;
   319   in Sign.pretty_term' ctxt (LocalSyntax.syn_of syntax) (Consts.extern consts) t' end;
   322 
   320 
   323 in
   321 in
   455 (* local abbreviations *)
   453 (* local abbreviations *)
   456 
   454 
   457 local
   455 local
   458 
   456 
   459 fun certify_consts ctxt =
   457 fun certify_consts ctxt =
   460   Consts.certify (pp ctxt) (tsig_of ctxt) (consts_of ctxt);
   458   let val Mode {abbrev, ...} = get_mode ctxt
       
   459   in Consts.certify (pp ctxt) (tsig_of ctxt) (not abbrev) (consts_of ctxt) end;
   461 
   460 
   462 fun reject_schematic (Var (xi, _)) =
   461 fun reject_schematic (Var (xi, _)) =
   463       error ("Unbound schematic variable: " ^ Term.string_of_vname xi)
   462       error ("Unbound schematic variable: " ^ Term.string_of_vname xi)
   464   | reject_schematic (Abs (_, _, t)) = reject_schematic t
   463   | reject_schematic (Abs (_, _, t)) = reject_schematic t
   465   | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
   464   | reject_schematic (t $ u) = (reject_schematic t; reject_schematic u)
   592 
   591 
   593 fun gen_cert prop mode ctxt0 t =
   592 fun gen_cert prop mode ctxt0 t =
   594   let val ctxt = set_mode mode ctxt0 in
   593   let val ctxt = set_mode mode ctxt0 in
   595     t
   594     t
   596     |> expand_abbrevs ctxt
   595     |> expand_abbrevs ctxt
   597     |> (fn t' => #1 (Sign.certify' false prop (pp ctxt) (consts_of ctxt) (theory_of ctxt) t')
   596     |> (fn t' => #1 (Sign.certify' prop (pp ctxt) false (consts_of ctxt) (theory_of ctxt) t')
   598       handle TYPE (msg, _, _) => error msg
   597       handle TYPE (msg, _, _) => error msg
   599         | TERM (msg, _) => error msg)
   598         | TERM (msg, _) => error msg)
   600    end;
   599    end;
   601 
   600 
   602 in
   601 in
   738 
   737 
   739 local
   738 local
   740 
   739 
   741 fun gen_bind prep (xi as (x, _), raw_t) ctxt =
   740 fun gen_bind prep (xi as (x, _), raw_t) ctxt =
   742   ctxt
   741   ctxt
   743   |> set_mode mode_default
   742   |> Variable.add_binds [(xi, Option.map (prep (set_mode mode_default ctxt)) raw_t)];
   744   |> Variable.add_binds [(xi, Option.map (prep ctxt) raw_t)];
       
   745 
   743 
   746 in
   744 in
   747 
   745 
   748 fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
   746 fun drop_schematic (b as (xi, SOME t)) = if Term.exists_subterm is_Var t then (xi, NONE) else b
   749   | drop_schematic b = b;
   747   | drop_schematic b = b;
  1027   in Context.mapping (Sign.add_notation mode args') (add_notation mode args') end;
  1025   in Context.mapping (Sign.add_notation mode args') (add_notation mode args') end;
  1028 
  1026 
  1029 
  1027 
  1030 (* abbreviations *)
  1028 (* abbreviations *)
  1031 
  1029 
  1032 val set_expand_abbrevs = map_consts o apsnd o Consts.set_expand;
  1030 fun read_term_abbrev ctxt = Syntax.read_term (set_mode mode_abbrev ctxt);
  1033 fun read_term_abbrev ctxt = Syntax.read_term (set_expand_abbrevs false ctxt);
       
  1034 
  1031 
  1035 fun add_abbrev mode (c, raw_t) ctxt =
  1032 fun add_abbrev mode (c, raw_t) ctxt =
  1036   let
  1033   let
  1037     val t0 = cert_term (ctxt |> set_expand_abbrevs false) raw_t
  1034     val t0 = cert_term (ctxt |> set_mode mode_abbrev) raw_t
  1038       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
  1035       handle ERROR msg => cat_error msg ("in constant abbreviation " ^ quote c);
  1039     val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
  1036     val [t] = Variable.exportT_terms (Variable.declare_term t0 ctxt) ctxt [t0];
  1040     val ((lhs, rhs), consts') = consts_of ctxt
  1037     val ((lhs, rhs), consts') = consts_of ctxt
  1041       |> Consts.abbreviate (pp ctxt) (tsig_of ctxt) (naming_of ctxt) mode (c, t);
  1038       |> Consts.abbreviate (pp ctxt) (tsig_of ctxt) (naming_of ctxt) mode (c, t);
  1042   in
  1039   in