| author | haftmann | 
| Fri, 09 May 2014 08:13:36 +0200 | |
| changeset 56926 | aaea99edc040 | 
| parent 56925 | 601edd9a6859 | 
| child 56927 | 4044a7d1720f | 
| permissions | -rw-r--r-- | 
| 37744 | 1  | 
(* Title: Tools/value.ML  | 
| 28227 | 2  | 
Author: Florian Haftmann, TU Muenchen  | 
3  | 
||
| 
28952
 
15a4b2cf8c34
made repository layout more coherent with logical distribution structure; stripped some $Id$s
 
haftmann 
parents: 
28227 
diff
changeset
 | 
4  | 
Generic value command for arbitrary evaluators.  | 
| 28227 | 5  | 
*)  | 
6  | 
||
7  | 
signature VALUE =  | 
|
8  | 
sig  | 
|
9  | 
val value: Proof.context -> term -> term  | 
|
10  | 
val value_select: string -> Proof.context -> term -> term  | 
|
11  | 
val value_cmd: string option -> string list -> string -> Toplevel.state -> unit  | 
|
12  | 
val add_evaluator: string * (Proof.context -> term -> term) -> theory -> theory  | 
|
13  | 
end;  | 
|
14  | 
||
15  | 
structure Value : VALUE =  | 
|
16  | 
struct  | 
|
17  | 
||
| 33522 | 18  | 
structure Evaluator = Theory_Data  | 
19  | 
(  | 
|
| 28227 | 20  | 
type T = (string * (Proof.context -> term -> term)) list;  | 
21  | 
val empty = [];  | 
|
22  | 
val extend = I;  | 
|
| 33522 | 23  | 
fun merge data : T = AList.merge (op =) (K true) data;  | 
| 28227 | 24  | 
)  | 
25  | 
||
26  | 
val add_evaluator = Evaluator.map o AList.update (op =);  | 
|
27  | 
||
28  | 
fun value_select name ctxt =  | 
|
| 42361 | 29  | 
case AList.lookup (op =) (Evaluator.get (Proof_Context.theory_of ctxt)) name  | 
| 28227 | 30  | 
   of NONE => error ("No such evaluator: " ^ name)
 | 
31  | 
| SOME f => f ctxt;  | 
|
32  | 
||
| 42361 | 33  | 
fun value ctxt t = let val evaluators = Evaluator.get (Proof_Context.theory_of ctxt)  | 
| 28227 | 34  | 
in if null evaluators then error "No evaluators"  | 
35  | 
else let val (evaluators, (_, evaluator)) = split_last evaluators  | 
|
36  | 
in case get_first (fn (_, f) => try (f ctxt) t) evaluators  | 
|
37  | 
of SOME t' => t'  | 
|
38  | 
| NONE => evaluator ctxt t  | 
|
39  | 
end end;  | 
|
40  | 
||
| 43612 | 41  | 
fun value_maybe_select some_name =  | 
42  | 
case some_name  | 
|
43  | 
of NONE => value  | 
|
44  | 
| SOME name => value_select name;  | 
|
45  | 
||
| 28227 | 46  | 
fun value_cmd some_name modes raw_t state =  | 
47  | 
let  | 
|
48  | 
val ctxt = Toplevel.context_of state;  | 
|
49  | 
val t = Syntax.read_term ctxt raw_t;  | 
|
| 43612 | 50  | 
val t' = value_maybe_select some_name ctxt t;  | 
| 28227 | 51  | 
val ty' = Term.type_of t';  | 
| 31218 | 52  | 
val ctxt' = Variable.auto_fixes t' ctxt;  | 
| 
37146
 
f652333bbf8e
renamed structure PrintMode to Print_Mode, keeping the old name as legacy alias for some time;
 
wenzelm 
parents: 
36960 
diff
changeset
 | 
53  | 
val p = Print_Mode.with_modes modes (fn () =>  | 
| 28227 | 54  | 
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,  | 
55  | 
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) ();  | 
|
56  | 
in Pretty.writeln p end;  | 
|
57  | 
||
| 
36960
 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 
wenzelm 
parents: 
33522 
diff
changeset
 | 
58  | 
val opt_modes =  | 
| 46949 | 59  | 
  Scan.optional (@{keyword "("} |-- Parse.!!! (Scan.repeat1 Parse.xname --| @{keyword ")"})) [];
 | 
| 28227 | 60  | 
|
| 43612 | 61  | 
val opt_evaluator =  | 
| 46949 | 62  | 
  Scan.option (@{keyword "["} |-- Parse.xname --| @{keyword "]"})
 | 
| 43612 | 63  | 
|
| 
36960
 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 
wenzelm 
parents: 
33522 
diff
changeset
 | 
64  | 
val _ =  | 
| 
46961
 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 
wenzelm 
parents: 
46949 
diff
changeset
 | 
65  | 
  Outer_Syntax.improper_command @{command_spec "value"} "evaluate and print term"
 | 
| 43612 | 66  | 
(opt_evaluator -- opt_modes -- Parse.term  | 
| 
51658
 
21c10672633b
discontinued Toplevel.no_timing complication -- also recovers timing of diagnostic commands, e.g. 'find_theorems';
 
wenzelm 
parents: 
46961 
diff
changeset
 | 
67  | 
>> (fn ((some_name, modes), t) => Toplevel.keep (value_cmd some_name modes t)));  | 
| 28227 | 68  | 
|
| 56926 | 69  | 
val _ = Context.>> (Context.map_theory  | 
70  | 
  (Thy_Output.antiquotation @{binding value}
 | 
|
| 43612 | 71  | 
(Scan.lift opt_evaluator -- Term_Style.parse -- Args.term)  | 
72  | 
    (fn {source, context, ...} => fn ((some_name, style), t) => Thy_Output.output context
 | 
|
73  | 
(Thy_Output.maybe_pretty_source Thy_Output.pretty_term context source  | 
|
| 56926 | 74  | 
[style (value_maybe_select some_name context t)]))));  | 
| 43612 | 75  | 
|
| 28227 | 76  | 
end;  |