src/Pure/ML/ml_context.ML
changeset 30672 beaadd5af500
parent 30643 955830462054
child 30683 e8ac1f9d9469
equal deleted inserted replaced
30671:2f64540707d6 30672:beaadd5af500
    18   val the_generic_context: unit -> Context.generic
    18   val the_generic_context: unit -> Context.generic
    19   val the_global_context: unit -> theory
    19   val the_global_context: unit -> theory
    20   val the_local_context: unit -> Proof.context
    20   val the_local_context: unit -> Proof.context
    21   val exec: (unit -> unit) -> Context.generic -> Context.generic
    21   val exec: (unit -> unit) -> Context.generic -> Context.generic
    22   val inherit_env: Context.generic -> Context.generic -> Context.generic
    22   val inherit_env: Context.generic -> Context.generic -> Context.generic
    23   val name_space: ML_NameSpace.nameSpace
    23   val name_space: ML_Name_Space.T
       
    24   val local_context: use_context
    24   val stored_thms: thm list ref
    25   val stored_thms: thm list ref
    25   val ml_store_thm: string * thm -> unit
    26   val ml_store_thm: string * thm -> unit
    26   val ml_store_thms: string * thm list -> unit
    27   val ml_store_thms: string * thm list -> unit
    27   type antiq =
    28   type antiq =
    28     {struct_name: string, background: Proof.context} ->
    29     {struct_name: string, background: Proof.context} ->
    29       (Proof.context -> string * string) * Proof.context
    30       (Proof.context -> string * string) * Proof.context
    30   val add_antiq: string -> (Position.T -> antiq context_parser) -> unit
    31   val add_antiq: string -> (Position.T -> antiq context_parser) -> unit
    31   val trace: bool ref
    32   val trace: bool ref
    32   val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
    33   val eval_antiquotes: ML_Lex.token Antiquote.antiquote list * Position.T ->
    33     Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
    34     Context.generic option -> (ML_Lex.token list * ML_Lex.token list) * Context.generic option
    34   val eval_wrapper: (string -> unit) * (string -> 'a) -> bool ->
       
    35     Position.T -> Symbol_Pos.text -> unit
       
    36   val eval: bool -> Position.T -> Symbol_Pos.text -> unit
    35   val eval: bool -> Position.T -> Symbol_Pos.text -> unit
    37   val eval_file: Path.T -> unit
    36   val eval_file: Path.T -> unit
    38   val eval_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
    37   val eval_in: Proof.context option -> bool -> Position.T -> Symbol_Pos.text -> unit
    39   val evaluate: Proof.context -> (string -> unit) * (string -> 'b) -> bool ->
    38   val evaluate: Proof.context -> bool -> string * (unit -> 'a) option ref -> string -> 'a
    40     string * (unit -> 'a) option ref -> string -> 'a
       
    41   val expression: Position.T -> string -> string -> string -> Context.generic -> Context.generic
    39   val expression: Position.T -> string -> string -> string -> Context.generic -> Context.generic
    42 end
    40 end
    43 
    41 
    44 structure ML_Context: ML_CONTEXT =
    42 structure ML_Context: ML_CONTEXT =
    45 struct
    43 struct
    59 (* ML name space *)
    57 (* ML name space *)
    60 
    58 
    61 structure ML_Env = GenericDataFun
    59 structure ML_Env = GenericDataFun
    62 (
    60 (
    63   type T =
    61   type T =
    64     ML_NameSpace.valueVal Symtab.table *
    62     ML_Name_Space.valueVal Symtab.table *
    65     ML_NameSpace.typeVal Symtab.table *
    63     ML_Name_Space.typeVal Symtab.table *
    66     ML_NameSpace.fixityVal Symtab.table *
    64     ML_Name_Space.fixityVal Symtab.table *
    67     ML_NameSpace.structureVal Symtab.table *
    65     ML_Name_Space.structureVal Symtab.table *
    68     ML_NameSpace.signatureVal Symtab.table *
    66     ML_Name_Space.signatureVal Symtab.table *
    69     ML_NameSpace.functorVal Symtab.table;
    67     ML_Name_Space.functorVal Symtab.table;
    70   val empty = (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty);
    68   val empty = (Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty, Symtab.empty);
    71   val extend = I;
    69   val extend = I;
    72   fun merge _
    70   fun merge _
    73    ((val1, type1, fixity1, structure1, signature1, functor1),
    71    ((val1, type1, fixity1, structure1, signature1, functor1),
    74     (val2, type2, fixity2, structure2, signature2, functor2)) : T =
    72     (val2, type2, fixity2, structure2, signature2, functor2)) : T =
    80     Symtab.merge (K true) (functor1, functor2));
    78     Symtab.merge (K true) (functor1, functor2));
    81 );
    79 );
    82 
    80 
    83 val inherit_env = ML_Env.put o ML_Env.get;
    81 val inherit_env = ML_Env.put o ML_Env.get;
    84 
    82 
    85 val name_space: ML_NameSpace.nameSpace =
    83 val name_space: ML_Name_Space.T =
    86   let
    84   let
    87     fun lookup sel1 sel2 name =
    85     fun lookup sel1 sel2 name =
    88       Context.thread_data ()
    86       Context.thread_data ()
    89       |> (fn NONE => NONE | SOME context => Symtab.lookup (sel1 (ML_Env.get context)) name)
    87       |> (fn NONE => NONE | SOME context => Symtab.lookup (sel1 (ML_Env.get context)) name)
    90       |> (fn NONE => sel2 ML_NameSpace.global name | some => some);
    88       |> (fn NONE => sel2 ML_Name_Space.global name | some => some);
    91 
    89 
    92     fun all sel1 sel2 () =
    90     fun all sel1 sel2 () =
    93       Context.thread_data ()
    91       Context.thread_data ()
    94       |> (fn NONE => [] | SOME context => Symtab.dest (sel1 (ML_Env.get context)))
    92       |> (fn NONE => [] | SOME context => Symtab.dest (sel1 (ML_Env.get context)))
    95       |> append (sel2 ML_NameSpace.global ())
    93       |> append (sel2 ML_Name_Space.global ())
    96       |> sort_distinct (string_ord o pairself #1);
    94       |> sort_distinct (string_ord o pairself #1);
    97 
    95 
    98     fun enter ap1 sel2 entry =
    96     fun enter ap1 sel2 entry =
    99       if is_some (Context.thread_data ()) then
    97       if is_some (Context.thread_data ()) then
   100         Context.>> (ML_Env.map (ap1 (Symtab.update entry)))
    98         Context.>> (ML_Env.map (ap1 (Symtab.update entry)))
   101       else sel2 ML_NameSpace.global entry;
    99       else sel2 ML_Name_Space.global entry;
   102   in
   100   in
   103    {lookupVal    = lookup #1 #lookupVal,
   101    {lookupVal    = lookup #1 #lookupVal,
   104     lookupType   = lookup #2 #lookupType,
   102     lookupType   = lookup #2 #lookupType,
   105     lookupFix    = lookup #3 #lookupFix,
   103     lookupFix    = lookup #3 #lookupFix,
   106     lookupStruct = lookup #4 #lookupStruct,
   104     lookupStruct = lookup #4 #lookupStruct,
   118     allStruct    = all #4 #allStruct,
   116     allStruct    = all #4 #allStruct,
   119     allSig       = all #5 #allSig,
   117     allSig       = all #5 #allSig,
   120     allFunct     = all #6 #allFunct}
   118     allFunct     = all #6 #allFunct}
   121   end;
   119   end;
   122 
   120 
       
   121 val local_context: use_context =
       
   122  {tune_source = ML_Parse.fix_ints,
       
   123   name_space = name_space,
       
   124   str_of_pos = Position.str_of oo Position.line_file,
       
   125   print = writeln,
       
   126   error = error};
       
   127 
   123 
   128 
   124 (* theorem bindings *)
   129 (* theorem bindings *)
   125 
   130 
   126 val stored_thms: thm list ref = ref [];
   131 val stored_thms: thm list ref = ref [];
   127 
   132 
   132     val _ =
   137     val _ =
   133       if name = "" then ()
   138       if name = "" then ()
   134       else if not (ML_Syntax.is_identifier name) then
   139       else if not (ML_Syntax.is_identifier name) then
   135         error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
   140         error ("Cannot bind theorem(s) " ^ quote name ^ " as ML value")
   136       else setmp stored_thms ths' (fn () =>
   141       else setmp stored_thms ths' (fn () =>
   137         use_text name_space (0, "") Output.ml_output true
   142         use_text local_context (0, "") true
   138           ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) ();
   143           ("val " ^ name ^ " = " ^ sel ^ "(! ML_Context.stored_thms);")) ();
   139   in () end;
   144   in () end;
   140 
   145 
   141 val ml_store_thms = ml_store "";
   146 val ml_store_thms = ml_store "";
   142 fun ml_store_thm (name, th) = ml_store "hd" (name, [th]);
   147 fun ml_store_thm (name, th) = ml_store "hd" (name, [th]);
   231         in (ml, SOME (Context.Proof ctxt')) end;
   236         in (ml, SOME (Context.Proof ctxt')) end;
   232   in ((begin_env @ ml_env @ end_env, ml_body), opt_ctxt') end;
   237   in ((begin_env @ ml_env @ end_env, ml_body), opt_ctxt') end;
   233 
   238 
   234 val trace = ref false;
   239 val trace = ref false;
   235 
   240 
   236 fun eval_wrapper pr verbose pos txt =
   241 fun eval verbose pos txt =
   237   let
   242   let
   238     fun eval_raw p = use_text name_space
   243     fun eval_raw p = use_text local_context
   239       (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p)) pr;
   244       (the_default 1 (Position.line_of p), the_default "ML" (Position.file_of p));
   240 
   245 
   241     (*prepare source text*)
   246     (*prepare source text*)
   242     val _ = Position.report Markup.ML_source pos;
   247     val _ = Position.report Markup.ML_source pos;
   243     val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos);
   248     val ants = ML_Lex.read_antiq (Symbol_Pos.explode (txt, pos), pos);
   244     val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ())
   249     val ((env, body), env_ctxt) = eval_antiquotes (ants, pos) (Context.thread_data ())
   258   in () end;
   263   in () end;
   259 
   264 
   260 end;
   265 end;
   261 
   266 
   262 
   267 
   263 (* ML evaluation *)
   268 (* derived versions *)
   264 
   269 
   265 val eval = eval_wrapper Output.ml_output;
       
   266 fun eval_file path = eval true (Path.position path) (File.read path);
   270 fun eval_file path = eval true (Path.position path) (File.read path);
   267 
   271 
   268 fun eval_in ctxt verbose pos txt =
   272 fun eval_in ctxt verbose pos txt =
   269   Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) ();
   273   Context.setmp_thread_data (Option.map Context.Proof ctxt) (fn () => eval verbose pos txt) ();
   270 
   274 
   271 fun evaluate ctxt pr verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
   275 fun evaluate ctxt verbose (ref_name, r) txt = NAMED_CRITICAL "ML" (fn () =>
   272   let
   276   let
   273     val _ = r := NONE;
   277     val _ = r := NONE;
   274     val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () =>
   278     val _ = Context.setmp_thread_data (SOME (Context.Proof ctxt)) (fn () =>
   275       eval_wrapper pr verbose Position.none
   279       eval verbose Position.none ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) ();
   276         ("val _ = (" ^ ref_name ^ " := SOME (fn () => " ^ txt ^ "))")) ();
       
   277   in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
   280   in (case ! r of NONE => error ("Bad evaluation for " ^ ref_name) | SOME e => e) end) ();
   278 
   281 
   279 fun expression pos bind body txt =
   282 fun expression pos bind body txt =
   280   exec (fn () => eval false pos
   283   exec (fn () => eval false pos
   281     ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^
   284     ("Context.set_thread_data (SOME (let " ^ bind ^ " = " ^ txt ^ " in " ^ body ^