src/HOL/Tools/value.ML
author haftmann
Fri, 04 Jul 2014 20:18:47 +0200
changeset 57512 cc97b347b301
parent 56975 1f3e60572081
child 58100 f54a8a4134d3
permissions -rw-r--r--
reduced name variants for assoc and commute on plus and mult
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
     1
(*  Title:      HOL/Tools/value.ML
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
     3
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
     4
Evaluation using nbe or SML.
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
     5
*)
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
     6
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
     7
signature VALUE =
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
     8
sig
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
     9
  val value: Proof.context -> term -> term
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    10
  val value_cmd: string list -> string -> Toplevel.state -> unit
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    11
end;
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    12
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    13
structure Value : VALUE =
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    14
struct
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    15
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    16
fun value ctxt t =
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    17
  if null (Term.add_frees t [])
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    18
  then case try (Code_Evaluation.dynamic_value_strict ctxt) t of
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    19
    SOME t' => t'
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    20
  | NONE => Nbe.dynamic_value ctxt t
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    21
  else Nbe.dynamic_value ctxt t;
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    22
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    23
fun value_cmd modes raw_t state =
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    24
  let
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    25
    val ctxt = Toplevel.context_of state;
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    26
    val t = Syntax.read_term ctxt raw_t;
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    27
    val t' = value ctxt t;
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    28
    val ty' = Term.type_of t';
31218
fa54c1e614df fixed typo
haftmann
parents: 29288
diff changeset
    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
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    31
      Pretty.block [Pretty.quote (Syntax.pretty_term ctxt' t'), Pretty.fbrk,
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    32
        Pretty.str "::", Pretty.brk 1, Pretty.quote (Syntax.pretty_typ ctxt' ty')]) ();
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    33
  in Pretty.writeln p end;
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    34
36960
01594f816e3a prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents: 33522
diff changeset
    35
val opt_modes =
46949
94aa7b81bcf6 prefer formally checked @{keyword} parser;
wenzelm
parents: 43619
diff changeset
    36
  Scan.optional (@{keyword "("} |-- Parse.!!! (Scan.repeat1 Parse.xname --| @{keyword ")"})) [];
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    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
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    40
    (opt_modes -- Parse.term
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    41
      >> (fn (modes, t) => Toplevel.keep (value_cmd modes t)));
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    42
56926
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
    43
val _ = Context.>> (Context.map_theory
aaea99edc040 modernized setups
haftmann
parents: 56925
diff changeset
    44
  (Thy_Output.antiquotation @{binding value}
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    45
    (Term_Style.parse -- Args.term)
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    46
    (fn {source, context, ...} => fn (style, t) => Thy_Output.output context
43612
c32144b8baba adding a value antiquotation
bulwahn
parents: 42361
diff changeset
    47
      (Thy_Output.maybe_pretty_source Thy_Output.pretty_term context source
56927
4044a7d1720f hardcoded nbe and sml into value command
haftmann
parents: 56926
diff changeset
    48
        [style (value context t)]))));
43612
c32144b8baba adding a value antiquotation
bulwahn
parents: 42361
diff changeset
    49
28227
77221ee0f7b9 generic value command
haftmann
parents:
diff changeset
    50
end;