src/HOL/ex/Codegenerator.thy
author haftmann
Fri Nov 03 14:22:41 2006 +0100 (2006-11-03)
changeset 21155 95142d816793
parent 21125 9b7d35ca1eef
child 21191 c00161fbf990
permissions -rw-r--r--
added particular test for partially applied case constants
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@21155
     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@21092
    30
  snd_three :: "'a * 'b * 'c => 'b"
haftmann@21092
    31
  "snd_three a = id (\<lambda>(a, b, c). b) a"
haftmann@19281
    32
haftmann@20936
    33
lemma [code]:
haftmann@20936
    34
  "swap (x, y) = (y, x)"
haftmann@20936
    35
  unfolding swap_def Let_def by auto
haftmann@20936
    36
haftmann@20936
    37
lemma [code]:
haftmann@20936
    38
  "appl (f, x) = f x"
haftmann@20936
    39
  unfolding appl_def Let_def by auto
haftmann@20936
    40
haftmann@20187
    41
subsection {* integers *}
haftmann@20187
    42
haftmann@19281
    43
definition
haftmann@19281
    44
  k :: "int"
haftmann@20351
    45
  "k = -42"
haftmann@19281
    46
haftmann@20968
    47
function
haftmann@20968
    48
  fac :: "int => int" where
haftmann@20968
    49
  "fac j = (if j <= 0 then 1 else j * (fac (j - 1)))"
haftmann@20968
    50
  by pat_completeness auto
haftmann@20968
    51
termination by (auto_term "measure nat")
haftmann@19281
    52
haftmann@20968
    53
declare fac.simps [code]
haftmann@19281
    54
haftmann@19281
    55
subsection {* sums *}
haftmann@19281
    56
haftmann@19281
    57
subsection {* options *}
haftmann@19281
    58
haftmann@19281
    59
subsection {* lists *}
haftmann@19281
    60
haftmann@19281
    61
definition
haftmann@19281
    62
  ps :: "nat list"
haftmann@19281
    63
  "ps = [2, 3, 5, 7, 11]"
haftmann@19281
    64
  qs :: "nat list"
haftmann@19281
    65
  "qs == rev ps"
haftmann@19281
    66
haftmann@19281
    67
subsection {* mutual datatypes *}
haftmann@19281
    68
haftmann@19281
    69
datatype mut1 = Tip | Top mut2
haftmann@19281
    70
  and mut2 = Tip | Top mut1
haftmann@19281
    71
haftmann@19281
    72
consts
haftmann@19281
    73
  mut1 :: "mut1 \<Rightarrow> mut1"
haftmann@19281
    74
  mut2 :: "mut2 \<Rightarrow> mut2"
haftmann@19281
    75
haftmann@19281
    76
primrec
haftmann@19281
    77
  "mut1 mut1.Tip = mut1.Tip"
haftmann@19281
    78
  "mut1 (mut1.Top x) = mut1.Top (mut2 x)"
haftmann@19281
    79
  "mut2 mut2.Tip = mut2.Tip"
haftmann@19281
    80
  "mut2 (mut2.Top x) = mut2.Top (mut1 x)"
haftmann@19281
    81
haftmann@20351
    82
subsection {* records *}
haftmann@19281
    83
haftmann@19281
    84
subsection {* equalities *}
haftmann@19281
    85
haftmann@20702
    86
subsection {* strings *}
haftmann@20702
    87
haftmann@20702
    88
definition
haftmann@20702
    89
  "mystring = ''my home is my castle''"
haftmann@20702
    90
haftmann@20968
    91
subsection {* nested lets and such *}
haftmann@20968
    92
haftmann@20968
    93
definition
haftmann@20968
    94
  "abs_let x = (let (y, z) = x in (\<lambda>u. case u of () \<Rightarrow> (y + y)))"
haftmann@20968
    95
haftmann@20968
    96
definition
haftmann@20968
    97
  "nested_let x = (let (y, z) = x in let w = y z in w * w)"
haftmann@20968
    98
haftmann@20968
    99
definition
haftmann@20968
   100
  "case_let x = (let (y, z) = x in case y of () => z)"
haftmann@20968
   101
haftmann@21155
   102
definition
haftmann@21155
   103
  "base_case f = f list_case"
haftmann@21155
   104
haftmann@19789
   105
subsection {* heavy usage of names *}
haftmann@19281
   106
haftmann@19281
   107
definition
haftmann@19281
   108
  f :: nat
haftmann@19281
   109
  "f = 2"
haftmann@19281
   110
  g :: nat
haftmann@19281
   111
  "g = f"
haftmann@19281
   112
  h :: nat
haftmann@19281
   113
  "h = g"
haftmann@19281
   114
haftmann@20383
   115
code_constname
haftmann@20936
   116
  f "MymodA.f"
haftmann@20936
   117
  g "MymodB.f"
haftmann@20936
   118
  h "MymodC.f"
haftmann@19281
   119
haftmann@20702
   120
definition
haftmann@20702
   121
  "apply_tower = (\<lambda>x. x (\<lambda>x. x (\<lambda>x. x)))"
haftmann@20702
   122
haftmann@20702
   123
definition
haftmann@20702
   124
  "keywords fun datatype class instance funa classa =
haftmann@20702
   125
    Suc fun + datatype * class mod instance - funa - classa"
haftmann@20702
   126
haftmann@20702
   127
hide (open) const keywords
haftmann@20702
   128
haftmann@20702
   129
definition
haftmann@20702
   130
  "shadow keywords = keywords @ [Codegenerator.keywords 0 0 0 0 0 0]"
haftmann@20702
   131
haftmann@20713
   132
code_gen
haftmann@20713
   133
  xor
haftmann@20713
   134
code_gen
haftmann@20713
   135
  "0::nat" "1::nat"
haftmann@20702
   136
code_gen
haftmann@21092
   137
  Pair fst snd Let split swap snd_three
haftmann@20453
   138
code_gen
haftmann@20351
   139
  "op + :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   140
  "op - :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   141
  "op * :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   142
  "op < :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20351
   143
  "op <= :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20453
   144
code_gen
haftmann@20597
   145
  appl
haftmann@20453
   146
code_gen
haftmann@20351
   147
  Inl Inr
haftmann@20453
   148
code_gen
haftmann@20351
   149
  None Some
haftmann@20453
   150
code_gen
haftmann@20351
   151
  hd tl "op @" ps qs
haftmann@20453
   152
code_gen
haftmann@20351
   153
  mut1 mut2
haftmann@20453
   154
code_gen
haftmann@20351
   155
  remove1
haftmann@20351
   156
  null
haftmann@20351
   157
  replicate
haftmann@20351
   158
  rotate1
haftmann@20351
   159
  rotate
haftmann@20351
   160
  splice
haftmann@20597
   161
code_gen
haftmann@20597
   162
  remdups
haftmann@20597
   163
  "distinct"
haftmann@21155
   164
  filter
haftmann@20453
   165
code_gen
haftmann@20351
   166
  foo1 foo3
haftmann@20453
   167
code_gen
haftmann@20936
   168
  mystring
haftmann@20936
   169
code_gen
haftmann@20936
   170
  f g h
haftmann@20936
   171
code_gen
haftmann@21155
   172
  apply_tower Codegenerator.keywords shadow base_case
haftmann@20968
   173
code_gen
haftmann@20968
   174
  abs_let nested_let case_let
haftmann@20936
   175
code_gen "0::int" "1::int"
haftmann@20936
   176
  (SML) (Haskell)
haftmann@20936
   177
code_gen n
haftmann@20936
   178
  (SML) (Haskell)
haftmann@20936
   179
code_gen fac
haftmann@20936
   180
  (SML) (Haskell)
haftmann@20936
   181
code_gen
haftmann@20936
   182
  k
haftmann@20936
   183
  "op + :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   184
  "op - :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   185
  "op * :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   186
  "op < :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20936
   187
  "op <= :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20936
   188
  fac
haftmann@20936
   189
  "op div :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20936
   190
  "op mod :: int \<Rightarrow> int \<Rightarrow> int"  
haftmann@20936
   191
  (SML) (Haskell)
haftmann@20936
   192
code_gen
haftmann@21046
   193
  "Code_Generator.eq :: bool \<Rightarrow> bool \<Rightarrow> bool"
haftmann@21046
   194
  "Code_Generator.eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@21046
   195
  "Code_Generator.eq :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@21046
   196
  "Code_Generator.eq :: ('a\<Colon>eq) * ('b\<Colon>eq) \<Rightarrow> 'a * 'b \<Rightarrow> bool"
haftmann@21046
   197
  "Code_Generator.eq :: ('a\<Colon>eq) + ('b\<Colon>eq) \<Rightarrow> 'a + 'b \<Rightarrow> bool"
haftmann@21046
   198
  "Code_Generator.eq :: ('a\<Colon>eq) option \<Rightarrow> 'a option \<Rightarrow> bool"
haftmann@21046
   199
  "Code_Generator.eq :: ('a\<Colon>eq) list \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@21046
   200
  "Code_Generator.eq :: mut1 \<Rightarrow> mut1 \<Rightarrow> bool"
haftmann@21046
   201
  "Code_Generator.eq :: mut2 \<Rightarrow> mut2 \<Rightarrow> bool"
haftmann@21046
   202
  "Code_Generator.eq :: ('a\<Colon>eq) point_scheme \<Rightarrow> 'a point_scheme \<Rightarrow> bool"
haftmann@19281
   203
haftmann@21125
   204
code_gen (SML *)
haftmann@21080
   205
code_gen (Haskell -)
haftmann@19281
   206
haftmann@19281
   207
end