src/HOL/Library/Pure_term.thy
author wenzelm
Wed, 19 Dec 2007 16:52:26 +0100
changeset 25709 43a1f08c5a29
parent 25666 f46ed5b333fd
permissions -rw-r--r--
simultaneous use_thys;
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/Pure_term.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
25666
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
     6
header {* Partial reflection of the Pure term algebra in HOL *}
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     7
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
     8
theory Pure_term
24994
c385c4eabb3b consolidated naming conventions for code generator theories
haftmann
parents: 23854
diff changeset
     9
imports Code_Message
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    10
begin
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    11
25666
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    12
subsection {* Definitions and syntax *}
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    13
24994
c385c4eabb3b consolidated naming conventions for code generator theories
haftmann
parents: 23854
diff changeset
    14
types vname = message_string;
c385c4eabb3b consolidated naming conventions for code generator theories
haftmann
parents: 23854
diff changeset
    15
types "class" = message_string;
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    16
types sort = "class list"
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    17
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    18
datatype "typ" =
25666
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    19
    Type message_string "typ list"
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    20
  | TFree vname sort  
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    21
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    22
abbreviation
25666
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    23
  Fun :: "typ \<Rightarrow> typ \<Rightarrow> typ" where
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    24
  "Fun ty1 ty2 \<equiv> Type (STR ''fun'') [ty1, ty2]"
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    25
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    26
locale (open) pure_term_syntax =
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    27
  fixes pure_term_Type :: "message_string \<Rightarrow> typ list \<Rightarrow> typ" (infixl "{\<struct>}" 120)
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    28
    and pure_term_TFree :: "vname \<Rightarrow> sort \<Rightarrow> typ" ("\<lbrace>_\<Colon>_\<rbrace>" [118, 118] 117)
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    29
    and pure_term_Fun :: "typ \<Rightarrow> typ \<Rightarrow> typ" (infixr "\<rightarrow>" 114)
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    30
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    31
parse_translation {*
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    32
let
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    33
  fun Type_tr [tyco, tys] = Lexicon.const @{const_syntax Type} $ tyco $ tys
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    34
    | Type_tr ts = raise TERM ("Type_tr", ts);
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    35
  fun TFree_tr [v, sort] = Lexicon.const @{const_syntax TFree} $ v $ sort
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    36
    | TFree_tr ts = raise TERM ("TFree_tr", ts);
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    37
  fun Fun_tr [ty1, ty2] = Lexicon.const @{const_syntax Fun} $ ty1 $ ty2
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    38
    | Fun_tr ts = raise TERM("Fun_tr", ts);
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    39
in [
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    40
  ("\\<^fixed>pure_term_Type", Type_tr),
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    41
  ("\\<^fixed>pure_term_TFree", TFree_tr),
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    42
  ("\\<^fixed>pure_term_Fun", Fun_tr)
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    43
] end
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    44
*}
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    45
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    46
notation (output)
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    47
  Type (infixl "{\<struct>}" 120)
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    48
and
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    49
  TFree ("\<lbrace>_\<Colon>_\<rbrace>" [118, 118] 117)
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    50
and
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    51
  Fun (infixr "\<rightarrow>" 114)
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    52
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    53
datatype "term" =
24994
c385c4eabb3b consolidated naming conventions for code generator theories
haftmann
parents: 23854
diff changeset
    54
    Const message_string "typ" (infix "\<Colon>\<subseteq>" 112)
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    55
  | Fix   vname "typ" (infix ":\<epsilon>" 112)
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    56
  | App   "term" "term" (infixl "\<bullet>" 110)
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    57
  | Abs   "vname \<times> typ" "term" (infixr "\<mapsto>" 111)
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    58
  | Bnd   nat
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    59
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    60
abbreviation
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    61
  Apps :: "term \<Rightarrow> term list \<Rightarrow> term" (infixl "{\<bullet>}" 110) where
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    62
  "t {\<bullet>} ts \<equiv> foldl (op \<bullet>) t ts"
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    63
abbreviation
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    64
  Abss :: "(vname \<times> typ) list \<Rightarrow> term \<Rightarrow> term" (infixr "{\<mapsto>}" 111) where
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    65
  "vs {\<mapsto>} t \<equiv> foldr (op \<mapsto>) vs t"
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    66
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    67
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    68
subsection {* ML interface *}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    69
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    70
ML {*
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    71
structure Pure_term =
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    72
struct
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    73
24994
c385c4eabb3b consolidated naming conventions for code generator theories
haftmann
parents: 23854
diff changeset
    74
val mk_sort = HOLogic.mk_list @{typ class} o map Message_String.mk;
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    75
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    76
fun mk_typ f (Type (tyco, tys)) =
24994
c385c4eabb3b consolidated naming conventions for code generator theories
haftmann
parents: 23854
diff changeset
    77
      @{term Type} $ Message_String.mk tyco
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    78
        $ HOLogic.mk_list @{typ typ} (map (mk_typ f) tys)
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    79
  | mk_typ f (TFree v) =
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    80
      f v;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    81
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    82
fun mk_term f g (Const (c, ty)) =
24994
c385c4eabb3b consolidated naming conventions for code generator theories
haftmann
parents: 23854
diff changeset
    83
      @{term Const} $ Message_String.mk c $ g ty
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    84
  | mk_term f g (t1 $ t2) =
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    85
      @{term App} $ mk_term f g t1 $ mk_term f g t2
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    86
  | mk_term f g (Free v) = f v;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    87
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    88
end;
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    89
*}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    90
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    91
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    92
subsection {* Code generator setup *}
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
    93
22553
haftmann
parents: 22525
diff changeset
    94
lemma [code func]:
25666
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    95
  includes pure_term_syntax
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
    96
  shows "tyco1 {\<struct>} tys1 = tyco2 {\<struct>} tys2 \<longleftrightarrow> tyco1 = tyco2
22553
haftmann
parents: 22525
diff changeset
    97
     \<and> list_all2 (op =) tys1 tys2"
haftmann
parents: 22525
diff changeset
    98
  by (auto simp add: list_all2_eq [symmetric])
haftmann
parents: 22525
diff changeset
    99
23063
haftmann
parents: 22845
diff changeset
   100
code_datatype Const App Fix
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   101
22845
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22804
diff changeset
   102
lemmas [code func del] = term.recs term.cases term.size
5f9138bcb3d7 changed code generator invocation syntax
haftmann
parents: 22804
diff changeset
   103
lemma [code func, code func del]: "(t1\<Colon>term) = t2 \<longleftrightarrow> t1 = t2" ..
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   104
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   105
code_type "typ" and "term"
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   106
  (SML "Term.typ" and "Term.term")
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   107
25666
f46ed5b333fd improved term syntax
haftmann
parents: 24994
diff changeset
   108
code_const Type and TFree
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   109
  (SML "Term.Type/ (_, _)" and "Term.TFree/ (_, _)")
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   110
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   111
code_const Const and App and Fix
23063
haftmann
parents: 22845
diff changeset
   112
  (SML "Term.Const/ (_, _)" and "Term.$/ (_, _)" and "Term.Free/ (_, _)")
22804
haftmann
parents: 22665
diff changeset
   113
 
22525
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   114
code_reserved SML Term
d929a900584c cleaned up Library/ and ex/
haftmann
parents:
diff changeset
   115
22665
cf152ff55d16 tuned document (headers, sections, spacing);
wenzelm
parents: 22553
diff changeset
   116
end