src/HOL/ex/Codegenerator.thy
author haftmann
Tue Oct 10 13:59:16 2006 +0200 (2006-10-10)
changeset 20952 070d176a8e2d
parent 20936 dc5dc0e55938
child 20968 5294baa98468
permissions -rw-r--r--
stripped pointless head
haftmann@19281
     1
(*  ID:         $Id$
haftmann@19281
     2
    Author:     Florian Haftmann, TU Muenchen
haftmann@19281
     3
*)
haftmann@19281
     4
haftmann@20187
     5
header {* Test and Examples for code generator *}
haftmann@19281
     6
haftmann@19281
     7
theory Codegenerator
haftmann@20952
     8
imports Main Records
haftmann@19281
     9
begin
haftmann@19281
    10
haftmann@19281
    11
subsection {* booleans *}
haftmann@19281
    12
haftmann@19281
    13
definition
haftmann@19281
    14
  xor :: "bool \<Rightarrow> bool \<Rightarrow> bool"
haftmann@19281
    15
  "xor p q = ((p | q) & \<not> (p & q))"
haftmann@19281
    16
haftmann@19281
    17
subsection {* natural numbers *}
haftmann@19281
    18
haftmann@19281
    19
definition
haftmann@19281
    20
  n :: nat
haftmann@19281
    21
  "n = 42"
haftmann@19281
    22
haftmann@19281
    23
subsection {* pairs *}
haftmann@19281
    24
haftmann@19281
    25
definition
haftmann@19281
    26
  swap :: "'a * 'b \<Rightarrow> 'b * 'a"
haftmann@19281
    27
  "swap p = (let (x, y) = p in (y, x))"
haftmann@19281
    28
  appl :: "('a \<Rightarrow> 'b) * 'a \<Rightarrow> 'b"
haftmann@19281
    29
  "appl p = (let (f, x) = p in f x)"
haftmann@19281
    30
haftmann@20936
    31
lemma [code]:
haftmann@20936
    32
  "swap (x, y) = (y, x)"
haftmann@20936
    33
  unfolding swap_def Let_def by auto
haftmann@20936
    34
haftmann@20936
    35
lemma [code]:
haftmann@20936
    36
  "appl (f, x) = f x"
haftmann@20936
    37
  unfolding appl_def Let_def by auto
haftmann@20936
    38
haftmann@20187
    39
subsection {* integers *}
haftmann@20187
    40
haftmann@19281
    41
definition
haftmann@19281
    42
  k :: "int"
haftmann@20351
    43
  "k = -42"
haftmann@19281
    44
haftmann@19281
    45
consts
haftmann@19281
    46
  fac :: "int => int"
haftmann@19281
    47
haftmann@19281
    48
recdef fac "measure nat"
haftmann@19281
    49
  "fac j = (if j <= 0 then 1 else j * (fac (j - 1)))"
haftmann@19281
    50
haftmann@19281
    51
subsection {* sums *}
haftmann@19281
    52
haftmann@19281
    53
subsection {* options *}
haftmann@19281
    54
haftmann@19281
    55
subsection {* lists *}
haftmann@19281
    56
haftmann@19281
    57
definition
haftmann@19281
    58
  ps :: "nat list"
haftmann@19281
    59
  "ps = [2, 3, 5, 7, 11]"
haftmann@19281
    60
  qs :: "nat list"
haftmann@19281
    61
  "qs == rev ps"
haftmann@19281
    62
haftmann@19281
    63
subsection {* mutual datatypes *}
haftmann@19281
    64
haftmann@19281
    65
datatype mut1 = Tip | Top mut2
haftmann@19281
    66
  and mut2 = Tip | Top mut1
haftmann@19281
    67
haftmann@19281
    68
consts
haftmann@19281
    69
  mut1 :: "mut1 \<Rightarrow> mut1"
haftmann@19281
    70
  mut2 :: "mut2 \<Rightarrow> mut2"
haftmann@19281
    71
haftmann@19281
    72
primrec
haftmann@19281
    73
  "mut1 mut1.Tip = mut1.Tip"
haftmann@19281
    74
  "mut1 (mut1.Top x) = mut1.Top (mut2 x)"
haftmann@19281
    75
  "mut2 mut2.Tip = mut2.Tip"
haftmann@19281
    76
  "mut2 (mut2.Top x) = mut2.Top (mut1 x)"
haftmann@19281
    77
haftmann@20351
    78
subsection {* records *}
haftmann@19281
    79
haftmann@19281
    80
subsection {* equalities *}
haftmann@19281
    81
haftmann@20702
    82
subsection {* strings *}
haftmann@20702
    83
haftmann@20702
    84
definition
haftmann@20702
    85
  "mystring = ''my home is my castle''"
haftmann@20702
    86
haftmann@19789
    87
subsection {* heavy usage of names *}
haftmann@19281
    88
haftmann@19281
    89
definition
haftmann@19281
    90
  f :: nat
haftmann@19281
    91
  "f = 2"
haftmann@19281
    92
  g :: nat
haftmann@19281
    93
  "g = f"
haftmann@19281
    94
  h :: nat
haftmann@19281
    95
  "h = g"
haftmann@19281
    96
haftmann@20383
    97
code_constname
haftmann@20936
    98
  f "MymodA.f"
haftmann@20936
    99
  g "MymodB.f"
haftmann@20936
   100
  h "MymodC.f"
haftmann@19281
   101
haftmann@20702
   102
definition
haftmann@20702
   103
  "apply_tower = (\<lambda>x. x (\<lambda>x. x (\<lambda>x. x)))"
haftmann@20702
   104
haftmann@20702
   105
definition
haftmann@20702
   106
  "keywords fun datatype class instance funa classa =
haftmann@20702
   107
    Suc fun + datatype * class mod instance - funa - classa"
haftmann@20702
   108
haftmann@20702
   109
hide (open) const keywords
haftmann@20702
   110
haftmann@20702
   111
definition
haftmann@20702
   112
  "shadow keywords = keywords @ [Codegenerator.keywords 0 0 0 0 0 0]"
haftmann@20702
   113
haftmann@20713
   114
code_gen
haftmann@20713
   115
  xor
haftmann@20713
   116
code_gen
haftmann@20713
   117
  "0::nat" "1::nat"
haftmann@20702
   118
code_gen
haftmann@20702
   119
  Pair fst snd Let split swap
haftmann@20453
   120
code_gen
haftmann@20351
   121
  "op + :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   122
  "op - :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   123
  "op * :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   124
  "op < :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20351
   125
  "op <= :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20453
   126
code_gen
haftmann@20597
   127
  appl
haftmann@20453
   128
code_gen
haftmann@20351
   129
  Inl Inr
haftmann@20453
   130
code_gen
haftmann@20351
   131
  None Some
haftmann@20453
   132
code_gen
haftmann@20351
   133
  hd tl "op @" ps qs
haftmann@20453
   134
code_gen
haftmann@20351
   135
  mut1 mut2
haftmann@20453
   136
code_gen
haftmann@20351
   137
  remove1
haftmann@20351
   138
  null
haftmann@20351
   139
  replicate
haftmann@20351
   140
  rotate1
haftmann@20351
   141
  rotate
haftmann@20351
   142
  splice
haftmann@20597
   143
code_gen
haftmann@20597
   144
  remdups
haftmann@20597
   145
  "distinct"
haftmann@20453
   146
code_gen
haftmann@20351
   147
  foo1 foo3
haftmann@20453
   148
code_gen
haftmann@20936
   149
  mystring
haftmann@20936
   150
code_gen
haftmann@20936
   151
  f g h
haftmann@20936
   152
code_gen
haftmann@20936
   153
  apply_tower Codegenerator.keywords shadow
haftmann@20936
   154
code_gen "0::int" "1::int"
haftmann@20936
   155
  (SML) (Haskell)
haftmann@20936
   156
code_gen n
haftmann@20936
   157
  (SML) (Haskell)
haftmann@20936
   158
code_gen fac
haftmann@20936
   159
  (SML) (Haskell)
haftmann@20936
   160
code_gen
haftmann@20936
   161
  k
haftmann@20936
   162
  "op + :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   163
  "op - :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   164
  "op * :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   165
  "op < :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20936
   166
  "op <= :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20936
   167
  fac
haftmann@20936
   168
  "op div :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   169
  "op mod :: int \<Rightarrow> int \<Rightarrow> int"  
haftmann@20936
   170
  (SML) (Haskell)
haftmann@20936
   171
code_gen
haftmann@20597
   172
  "OperationalEquality.eq :: bool \<Rightarrow> bool \<Rightarrow> bool"
haftmann@20597
   173
  "OperationalEquality.eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20597
   174
  "OperationalEquality.eq :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20597
   175
  "OperationalEquality.eq :: ('a\<Colon>eq) * ('b\<Colon>eq) \<Rightarrow> 'a * 'b \<Rightarrow> bool"
haftmann@20597
   176
  "OperationalEquality.eq :: ('a\<Colon>eq) + ('b\<Colon>eq) \<Rightarrow> 'a + 'b \<Rightarrow> bool"
haftmann@20597
   177
  "OperationalEquality.eq :: ('a\<Colon>eq) option \<Rightarrow> 'a option \<Rightarrow> bool"
haftmann@20597
   178
  "OperationalEquality.eq :: ('a\<Colon>eq) list \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@20597
   179
  "OperationalEquality.eq :: mut1 \<Rightarrow> mut1 \<Rightarrow> bool"
haftmann@20597
   180
  "OperationalEquality.eq :: mut2 \<Rightarrow> mut2 \<Rightarrow> bool"
haftmann@20597
   181
  "OperationalEquality.eq :: ('a\<Colon>eq) point_scheme \<Rightarrow> 'a point_scheme \<Rightarrow> bool"
haftmann@19281
   182
haftmann@20702
   183
code_gen (SML -)
haftmann@19281
   184
haftmann@19281
   185
end