src/HOL/Library/Eval.thy
author wenzelm
Tue, 02 Sep 2008 14:10:45 +0200
changeset 28083 103d9282a946
parent 28054 2b84d34c5d02
child 28084 a05ca48ef263
permissions -rw-r--r--
explicit type Name.binding for higher-specification elements;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/Library/Eval.thy
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     2
    ID:         $Id$
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     3
    Author:     Florian Haftmann, TU Muenchen
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     4
*)
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     5
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     6
header {* A simple term evaluation mechanism *}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     7
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     8
theory Eval
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
     9
imports
27368
9f90ac19e32b established Plain theory and image
haftmann
parents: 27103
diff changeset
    10
  Plain
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    11
  RType
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
    12
  Code_Index (* this theory is just imported for a term_of setup *)
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    13
begin
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    14
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    15
subsection {* Term representation *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    16
26587
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
    17
subsubsection {* Terms and class @{text term_of} *}
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    18
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    19
datatype "term" = dummy_term
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    20
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    21
definition
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    22
  Const :: "message_string \<Rightarrow> rtype \<Rightarrow> term"
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    23
where
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    24
  "Const _ _ = dummy_term"
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    25
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    26
definition
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    27
  App :: "term \<Rightarrow> term \<Rightarrow> term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    28
where
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    29
  "App _ _ = dummy_term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    30
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    31
code_datatype Const App
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    32
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    33
class term_of = rtype +
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    34
  fixes term_of :: "'a \<Rightarrow> term"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    35
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    36
lemma term_of_anything: "term_of x \<equiv> t"
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    37
  by (rule eq_reflection) (cases "term_of x", cases t, simp)
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    38
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    39
ML {*
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    40
structure Eval =
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    41
struct
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    42
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    43
fun mk_term f g (Const (c, ty)) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    44
      @{term Const} $ Message_String.mk c $ g ty
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    45
  | mk_term f g (t1 $ t2) =
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    46
      @{term App} $ mk_term f g t1 $ mk_term f g t2
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26242
diff changeset
    47
  | mk_term f g (Free v) = f v
26587
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
    48
  | mk_term f g (Bound i) = Bound i
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
    49
  | mk_term f g (Abs (v, _, t)) = Abs (v, @{typ term}, mk_term f g t);
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
    50
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    51
fun mk_term_of ty t = Const (@{const_name term_of}, ty --> @{typ term}) $ t;
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    52
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    53
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    54
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    55
26587
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
    56
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
    57
subsubsection {* @{text term_of} instances *}
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
    58
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    59
setup {*
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    60
let
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    61
  fun add_term_of_def ty vs tyco thy =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    62
    let
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    63
      val lhs = Const (@{const_name term_of}, ty --> @{typ term})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    64
        $ Free ("x", ty);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    65
      val rhs = @{term "undefined \<Colon> term"};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    66
      val eq = HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, rhs));
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    67
    in
25536
01753a944433 improved
haftmann
parents: 25502
diff changeset
    68
      thy
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    69
      |> TheoryTarget.instantiation ([tyco], vs, @{sort term_of})
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    70
      |> `(fn lthy => Syntax.check_term lthy eq)
28083
103d9282a946 explicit type Name.binding for higher-specification elements;
wenzelm
parents: 28054
diff changeset
    71
      |-> (fn eq => Specification.definition (NONE, ((Name.no_binding, []), eq)))
25569
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    72
      |> snd
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    73
      |> Class.prove_instantiation_instance (K (Class.intro_classes_tac []))
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    74
      |> LocalTheory.exit
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
    75
      |> ProofContext.theory_of
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    76
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    77
  fun interpretator (tyco, (raw_vs, _)) thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    78
    let
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    79
      val has_inst = can (Sorts.mg_domain (Sign.classes_of thy) tyco) @{sort term_of};
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    80
      val constrain_sort =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    81
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    82
      val vs = (map o apsnd) constrain_sort raw_vs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    83
      val ty = Type (tyco, map TFree vs);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    84
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    85
      thy
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    86
      |> RType.perhaps_add_def tyco
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    87
      |> not has_inst ? add_term_of_def ty vs tyco
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    88
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    89
in
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
    90
  Code.type_interpretation interpretator
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    91
end
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    92
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    93
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    94
setup {*
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    95
let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    96
  fun mk_term_of_eq ty vs tyco (c, tys) =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    97
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    98
      val t = list_comb (Const (c, tys ---> ty),
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
    99
        map Free (Name.names Name.context "a" tys));
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   100
    in (map_aterms (fn Free (v, ty) => Var ((v, 0), ty) | t => t) t, Eval.mk_term
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   101
      (fn (v, ty) => Eval.mk_term_of ty (Var ((v, 0), ty)))
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
   102
      (RType.mk (fn (v, sort) => RType.rtype (TFree (v, sort)))) t)
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   103
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   104
  fun prove_term_of_eq ty eq thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   105
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   106
      val cty = Thm.ctyp_of thy ty;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   107
      val (arg, rhs) = pairself (Thm.cterm_of thy) eq;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   108
      val thm = @{thm term_of_anything}
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   109
        |> Drule.instantiate' [SOME cty] [SOME arg, SOME rhs]
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   110
        |> Thm.varifyT;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   111
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   112
      thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   113
      |> Code.add_func thm
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   114
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   115
  fun interpretator (tyco, (raw_vs, raw_cs)) thy =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   116
    let
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   117
      val constrain_sort =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   118
        curry (Sorts.inter_sort (Sign.classes_of thy)) @{sort term_of};
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   119
      val vs = (map o apsnd) constrain_sort raw_vs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   120
      val cs = (map o apsnd o map o map_atyps)
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   121
        (fn TFree (v, sort) => TFree (v, constrain_sort sort)) raw_cs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   122
      val ty = Type (tyco, map TFree vs);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   123
      val eqs = map (mk_term_of_eq ty vs tyco) cs;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   124
      val const = AxClass.param_of_inst thy (@{const_name term_of}, tyco);
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   125
    in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   126
      thy
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   127
      |> Code.del_funcs const
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   128
      |> fold (prove_term_of_eq ty) eqs
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   129
    end;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   130
in
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   131
  Code.type_interpretation interpretator
25569
c597835d5de4 dropped Instance.instantiate
haftmann
parents: 25559
diff changeset
   132
end
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   133
*}
23062
d88d2087436d evaluation for integers
haftmann
parents: 23020
diff changeset
   134
26587
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   135
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   136
subsubsection {* Code generator setup *}
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   137
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   138
lemmas [code func del] = term.recs term.cases term.size
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   139
lemma [code func, code func del]: "(t1\<Colon>term) = t2 \<longleftrightarrow> t1 = t2" ..
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   140
26168
3bd9ac4e0b97 added theory for reflected types
haftmann
parents: 26037
diff changeset
   141
lemma [code func, code func del]: "(term_of \<Colon> rtype \<Rightarrow> term) = term_of" ..
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   142
lemma [code func, code func del]: "(term_of \<Colon> term \<Rightarrow> term) = term_of" ..
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   143
lemma [code func, code func del]: "(term_of \<Colon> index \<Rightarrow> term) = term_of" ..
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   144
lemma [code func, code func del]: "(term_of \<Colon> message_string \<Rightarrow> term) = term_of" ..
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   145
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   146
code_type "term"
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   147
  (SML "Term.term")
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   148
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   149
code_const Const and App
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   150
  (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)")
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   151
26037
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   152
code_const "term_of \<Colon> index \<Rightarrow> term"
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   153
  (SML "HOLogic.mk'_number/ HOLogic.indexT")
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   154
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   155
code_const "term_of \<Colon> message_string \<Rightarrow> term"
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   156
  (SML "Message'_String.mk")
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   157
8bf9e480a7b8 suppport for messages and indices
haftmann
parents: 26032
diff changeset
   158
26587
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   159
subsubsection {* Syntax *}
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   160
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   161
print_translation {*
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   162
let
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   163
  val term = Const ("<TERM>", dummyT);
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   164
  fun tr1' [_, _] = term;
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   165
  fun tr2' [] = term;
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   166
in
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   167
  [(@{const_syntax Const}, tr1'),
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   168
    (@{const_syntax App}, tr1'),
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   169
    (@{const_syntax dummy_term}, tr2')]
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   170
end
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   171
*}
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   172
setup {*
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   173
  Sign.declare_const [] ("rterm_of", @{typ "'a \<Rightarrow> 'b"}, NoSyn)
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   174
  #> snd
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   175
*}
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   176
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   177
notation (output)
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   178
  rterm_of ("\<guillemotleft>_\<guillemotright>")
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   179
27681
8cedebf55539 dropped locale (open)
haftmann
parents: 27368
diff changeset
   180
locale rterm_syntax =
26587
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   181
  fixes rterm_of_syntax :: "'a \<Rightarrow> 'b" ("\<guillemotleft>_\<guillemotright>")
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   182
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   183
parse_translation {*
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   184
let
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   185
  fun rterm_of_tr [t] = Lexicon.const @{const_name rterm_of} $ t
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   186
    | rterm_of_tr ts = raise TERM ("rterm_of_tr", ts);
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   187
in
26591
74b3c93f2428 avoid control symbols in document (\<^fixed>);
wenzelm
parents: 26587
diff changeset
   188
  [(Syntax.fixedN ^ "rterm_of_syntax", rterm_of_tr)]
26587
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   189
end
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   190
*}
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   191
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   192
setup {*
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   193
let
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   194
  val subst_rterm_of = Eval.mk_term
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   195
    (fn (v, _) => error ("illegal free variable in term quotation: " ^ quote v))
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   196
    (RType.mk (fn (v, sort) => RType.rtype (TFree (v, sort))));
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   197
  fun subst_rterm_of' (Const (@{const_name rterm_of}, _), [t]) = subst_rterm_of t
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   198
    | subst_rterm_of' (Const (@{const_name rterm_of}, _), _) =
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   199
        error ("illegal number of arguments for " ^ quote @{const_name rterm_of})
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   200
    | subst_rterm_of' (t, ts) = list_comb (t, map (subst_rterm_of' o strip_comb) ts);
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   201
  fun subst_rterm_of'' t = 
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   202
    let
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   203
      val t' = subst_rterm_of' (strip_comb t);
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   204
    in if t aconv t'
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   205
      then NONE
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   206
      else SOME t'
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   207
    end;
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   208
  fun check_rterm_of ts ctxt =
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   209
    let
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   210
      val ts' = map subst_rterm_of'' ts;
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   211
    in if exists is_some ts'
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   212
      then SOME (map2 the_default ts ts', ctxt)
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   213
      else NONE
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   214
    end;
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   215
in
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   216
  Context.theory_map (Syntax.add_term_check 0 "rterm_of" check_rterm_of)
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   217
end;
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   218
*}
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   219
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   220
hide const dummy_term
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   221
hide (open) const Const App
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   222
hide (open) const term_of
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   223
58fb6e033c00 rudimentary user-syntax for terms
haftmann
parents: 26267
diff changeset
   224
25763
474f8ba9dfa9 improved evaluation mechanism
haftmann
parents: 25691
diff changeset
   225
subsection {* Evaluation setup *}
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   226
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   227
ML {*
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   228
signature EVAL =
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   229
sig
26267
ba710daf77a7 added combinator for interpretation of construction of datatype
haftmann
parents: 26242
diff changeset
   230
  val mk_term: ((string * typ) -> term) -> (typ -> term) -> term -> term
24587
4f2cbf6e563f multi-functional value keyword
haftmann
parents: 24508
diff changeset
   231
  val eval_ref: (unit -> term) option ref
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   232
  val eval_term: theory -> term -> term
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   233
  val evaluate: Proof.context -> term -> unit
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   234
  val evaluate': string -> Proof.context -> term -> unit
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   235
  val evaluate_cmd: string option -> string -> Toplevel.state -> unit
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   236
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   237
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   238
structure Eval : EVAL =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   239
struct
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   240
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   241
open Eval;
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   242
24587
4f2cbf6e563f multi-functional value keyword
haftmann
parents: 24508
diff changeset
   243
val eval_ref = ref (NONE : (unit -> term) option);
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   244
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   245
fun eval_term thy t =
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   246
  t 
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   247
  |> Eval.mk_term_of (fastype_of t)
28054
2b84d34c5d02 restructured and split code serializer module
haftmann
parents: 27681
diff changeset
   248
  |> (fn t => Code_ML.eval_term ("Eval.eval_ref", eval_ref) thy t [])
26020
ffe1a032d24b proper term_of functions
haftmann
parents: 26011
diff changeset
   249
  |> Code.postprocess_term thy;
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   250
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   251
val evaluators = [
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   252
  ("code", eval_term),
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   253
  ("SML", Codegen.eval_term),
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   254
  ("normal_form", Nbe.norm_term)
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   255
];
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   256
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   257
fun gen_evaluate evaluators ctxt t =
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   258
  let
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   259
    val thy = ProofContext.theory_of ctxt;
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   260
    val (evls, evl) = split_last evaluators;
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   261
    val t' = case get_first (fn f => try (f thy) t) evls
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   262
     of SOME t' => t'
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   263
      | NONE => evl thy t;
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   264
    val ty' = Term.type_of t';
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24916
diff changeset
   265
    val p = Pretty.block [Pretty.quote (Syntax.pretty_term ctxt t'),
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   266
      Pretty.fbrk, Pretty.str "::", Pretty.brk 1,
24920
2a45e400fdad generic Syntax.pretty/string_of operations;
wenzelm
parents: 24916
diff changeset
   267
      Pretty.quote (Syntax.pretty_typ ctxt ty')];
24280
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   268
  in Pretty.writeln p end;
c9867bdf2424 updated code generator setup
haftmann
parents: 24219
diff changeset
   269
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   270
val evaluate = gen_evaluate (map snd evaluators);
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   271
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   272
fun evaluate' name = gen_evaluate
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   273
  [(the o AList.lookup (op =) evaluators) name];
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   274
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   275
fun evaluate_cmd some_name raw_t state =
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   276
  let
22804
haftmann
parents: 22665
diff changeset
   277
    val ctxt = Toplevel.context_of state;
24508
c8b82fec6447 replaced ProofContext.read_term/prop by general Syntax.read_term/prop;
wenzelm
parents: 24423
diff changeset
   278
    val t = Syntax.read_term ctxt raw_t;
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   279
  in case some_name
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   280
   of NONE => evaluate ctxt t
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   281
    | SOME name => evaluate' name ctxt t
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   282
  end;
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   283
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   284
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   285
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   286
22804
haftmann
parents: 22665
diff changeset
   287
ML {*
haftmann
parents: 22665
diff changeset
   288
  OuterSyntax.improper_command "value" "read, evaluate and print term" OuterKeyword.diag
24835
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   289
    (Scan.option (OuterParse.$$$ "(" |-- OuterParse.name --| OuterParse.$$$ ")")
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   290
    -- OuterParse.term
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   291
      >> (fn (some_name, t) => Toplevel.no_timing o Toplevel.keep
8c26128f8997 clarified relationship of code generator conversions and evaluations
haftmann
parents: 24659
diff changeset
   292
           (Eval.evaluate_cmd some_name t)));
22804
haftmann
parents: 22665
diff changeset
   293
*}
haftmann
parents: 22665
diff changeset
   294
26032
377b7aa0b5b5 <TERM> syntax
haftmann
parents: 26020
diff changeset
   295
end