author | haftmann |
Fri, 04 Jul 2014 20:18:47 +0200 | |
changeset 57512 | cc97b347b301 |
parent 56975 | 1f3e60572081 |
child 58100 | f54a8a4134d3 |
permissions | -rw-r--r-- |
56927 | 1 |
(* Title: HOL/Tools/value.ML |
28227 | 2 |
Author: Florian Haftmann, TU Muenchen |
3 |
||
56927 | 4 |
Evaluation using nbe or SML. |
28227 | 5 |
*) |
6 |
||
7 |
signature VALUE = |
|
8 |
sig |
|
9 |
val value: Proof.context -> term -> term |
|
56927 | 10 |
val value_cmd: string list -> string -> Toplevel.state -> unit |
28227 | 11 |
end; |
12 |
||
13 |
structure Value : VALUE = |
|
14 |
struct |
|
15 |
||
56927 | 16 |
fun value ctxt t = |
17 |
if null (Term.add_frees t []) |
|
18 |
then case try (Code_Evaluation.dynamic_value_strict ctxt) t of |
|
19 |
SOME t' => t' |
|
20 |
| NONE => Nbe.dynamic_value ctxt t |
|
21 |
else Nbe.dynamic_value ctxt t; |
|
28227 | 22 |
|
56927 | 23 |
fun value_cmd modes raw_t state = |
28227 | 24 |
let |
25 |
val ctxt = Toplevel.context_of state; |
|
26 |
val t = Syntax.read_term ctxt raw_t; |
|
56927 | 27 |
val t' = value ctxt t; |
28227 | 28 |
val ty' = Term.type_of t'; |
31218 | 29 |
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
|
30 |
val p = Print_Mode.with_modes modes (fn () => |
28227 | 31 |
Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk, |
32 |
Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) (); |
|
33 |
in Pretty.writeln p end; |
|
34 |
||
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
33522
diff
changeset
|
35 |
val opt_modes = |
46949 | 36 |
Scan.optional (@{keyword "("} |-- Parse.!!! (Scan.repeat1 Parse.xname --| @{keyword ")"})) []; |
28227 | 37 |
|
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
33522
diff
changeset
|
38 |
val _ = |
46961
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
wenzelm
parents:
46949
diff
changeset
|
39 |
Outer_Syntax.improper_command @{command_spec "value"} "evaluate and print term" |
56927 | 40 |
(opt_modes -- Parse.term |
41 |
>> (fn (modes, t) => Toplevel.keep (value_cmd modes t))); |
|
28227 | 42 |
|
56926 | 43 |
val _ = Context.>> (Context.map_theory |
44 |
(Thy_Output.antiquotation @{binding value} |
|
56927 | 45 |
(Term_Style.parse -- Args.term) |
46 |
(fn {source, context, ...} => fn (style, t) => Thy_Output.output context |
|
43612 | 47 |
(Thy_Output.maybe_pretty_source Thy_Output.pretty_term context source |
56927 | 48 |
[style (value context t)])))); |
43612 | 49 |
|
28227 | 50 |
end; |