src/Pure/ML/ml_context.ML
changeset 56069 451d5b73f8cf
parent 56032 b034b9f0fa2a
child 56070 1bc0bea908c3
equal deleted inserted replaced
56068:f74d0a4d8ae3 56069:451d5b73f8cf
    21   val exec: (unit -> unit) -> Context.generic -> Context.generic
    21   val exec: (unit -> unit) -> Context.generic -> Context.generic
    22   val get_stored_thms: unit -> thm list
    22   val get_stored_thms: unit -> thm list
    23   val get_stored_thm: unit -> thm
    23   val get_stored_thm: unit -> thm
    24   val ml_store_thms: string * thm list -> unit
    24   val ml_store_thms: string * thm list -> unit
    25   val ml_store_thm: string * thm -> unit
    25   val ml_store_thm: string * thm -> unit
    26   type antiq = Proof.context -> string * string
    26   val print_antiquotations: Proof.context -> unit
    27   val add_antiq: binding -> antiq context_parser -> theory -> theory
    27   type decl = Proof.context -> string * string
    28   val check_antiq: Proof.context -> xstring * Position.T -> string
    28   val antiquotation: binding -> 'a context_parser ->
       
    29     (Args.src -> 'a -> Proof.context -> decl * Proof.context) -> theory -> theory
    29   val trace_raw: Config.raw
    30   val trace_raw: Config.raw
    30   val trace: bool Config.T
    31   val trace: bool Config.T
    31   val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
    32   val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
    32     Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
    33     Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
    33   val eval: bool -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
    34   val eval: bool -> Position.T -> ML_Lex.token Antiquote.antiquote list -> unit
    93 
    94 
    94 
    95 
    95 
    96 
    96 (** ML antiquotations **)
    97 (** ML antiquotations **)
    97 
    98 
    98 (* antiquotation commands *)
    99 (* theory data *)
    99 
   100 
   100 type antiq = Proof.context -> string * string;
   101 type decl = Proof.context -> string * string;  (*final context -> ML env, ML body*)
   101 
   102 structure Antiquotations = Theory_Data
   102 structure Antiq_Parsers = Theory_Data
       
   103 (
   103 (
   104   type T = antiq context_parser Name_Space.table;
   104   type T = (Args.src -> Proof.context -> decl * Proof.context) Name_Space.table;
   105   val empty : T = Name_Space.empty_table Markup.ML_antiquotationN;
   105   val empty : T = Name_Space.empty_table Markup.ML_antiquotationN;
   106   val extend = I;
   106   val extend = I;
   107   fun merge data : T = Name_Space.merge_tables data;
   107   fun merge data : T = Name_Space.merge_tables data;
   108 );
   108 );
   109 
   109 
   110 val get_antiqs = Antiq_Parsers.get o Proof_Context.theory_of;
   110 val get_antiquotations = Antiquotations.get o Proof_Context.theory_of;
   111 
   111 
   112 fun add_antiq name scan thy = thy
   112 fun add_antiquotation name f thy = thy
   113   |> Antiq_Parsers.map (Name_Space.define (Context.Theory thy) true (name, scan) #> snd);
   113   |> Antiquotations.map (Name_Space.define (Context.Theory thy) true (name, f) #> snd);
   114 
   114 
   115 fun check_antiq ctxt = #1 o Name_Space.check (Context.Proof ctxt) (get_antiqs ctxt);
   115 fun print_antiquotations ctxt =
   116 
   116   Pretty.big_list "ML antiquotations:"
   117 fun antiquotation src ctxt =
   117     (map (Pretty.mark_str o #1) (Name_Space.markup_table ctxt (get_antiquotations ctxt)))
   118   let val (src', scan) = Args.check_src ctxt (get_antiqs ctxt) src
   118   |> Pretty.writeln;
   119   in Args.syntax scan src' ctxt end;
   119 
       
   120 fun apply_antiquotation src ctxt =
       
   121   let val (src', f) = Args.check_src ctxt (get_antiquotations ctxt) src
       
   122   in f src' ctxt end;
       
   123 
       
   124 fun antiquotation name scan body =
       
   125   add_antiquotation name
       
   126     (fn src => fn orig_ctxt =>
       
   127       let val (x, _) = Args.syntax scan src orig_ctxt
       
   128       in body src x orig_ctxt end);
   120 
   129 
   121 
   130 
   122 (* parsing and evaluation *)
   131 (* parsing and evaluation *)
   123 
   132 
   124 local
   133 local
   157           fun no_decl _ = ([], []);
   166           fun no_decl _ = ([], []);
   158 
   167 
   159           fun expand (Antiquote.Text tok) ctxt = (K ([], [tok]), ctxt)
   168           fun expand (Antiquote.Text tok) ctxt = (K ([], [tok]), ctxt)
   160             | expand (Antiquote.Antiq (ss, {range, ...})) ctxt =
   169             | expand (Antiquote.Antiq (ss, {range, ...})) ctxt =
   161                 let
   170                 let
   162                   val (decl, ctxt') = antiquotation (Token.read_antiq lex antiq (ss, #1 range)) ctxt;
   171                   val (decl, ctxt') =
       
   172                     apply_antiquotation (Token.read_antiq lex antiq (ss, #1 range)) ctxt;
   163                   val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
   173                   val decl' = decl #> pairself (ML_Lex.tokenize #> map (ML_Lex.set_range range));
   164                 in (decl', ctxt') end;
   174                 in (decl', ctxt') end;
   165 
   175 
   166           val ctxt =
   176           val ctxt =
   167             (case opt_ctxt of
   177             (case opt_ctxt of
   208 
   218 
   209 fun eval_source verbose source =
   219 fun eval_source verbose source =
   210   eval verbose (#pos source) (ML_Lex.read_source source);
   220   eval verbose (#pos source) (ML_Lex.read_source source);
   211 
   221 
   212 fun eval_in ctxt verbose pos ants =
   222 fun eval_in ctxt verbose pos ants =
   213   Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos ants) ();
   223   Context.setmp_thread_data (Option.map Context.Proof ctxt)
       
   224     (fn () => eval verbose pos ants) ();
   214 
   225 
   215 fun eval_source_in ctxt verbose source =
   226 fun eval_source_in ctxt verbose source =
   216   Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval_source verbose source) ();
   227   Context.setmp_thread_data (Option.map Context.Proof ctxt)
       
   228     (fn () => eval_source verbose source) ();
   217 
   229 
   218 fun expression pos bind body ants =
   230 fun expression pos bind body ants =
   219   exec (fn () => eval false pos
   231   exec (fn () => eval false pos
   220     (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
   232     (ML_Lex.read Position.none ("Context.set_thread_data (SOME (let " ^ bind ^ " = ") @ ants @
   221       ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));
   233       ML_Lex.read Position.none (" in " ^ body ^ " end (ML_Context.the_generic_context ())));")));