src/HOL/ex/Codegenerator.thy
author haftmann
Tue Aug 08 08:19:15 2006 +0200 (2006-08-08)
changeset 20351 c7658e811ffb
parent 20187 af47971ea304
child 20383 58f65fc90cf4
permissions -rw-r--r--
added more examples
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@20351
     8
imports Main "~~/src/HOL/ex/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
  one :: nat
haftmann@19281
    21
  "one = 1"
haftmann@19281
    22
  n :: nat
haftmann@19281
    23
  "n = 42"
haftmann@19281
    24
haftmann@19281
    25
subsection {* pairs *}
haftmann@19281
    26
haftmann@19281
    27
definition
haftmann@19281
    28
  swap :: "'a * 'b \<Rightarrow> 'b * 'a"
haftmann@19281
    29
  "swap p = (let (x, y) = p in (y, x))"
haftmann@19281
    30
  swapp :: "'a * 'b \<Rightarrow> 'c * 'd \<Rightarrow> ('a * 'c) * ('b * 'd)"
haftmann@19281
    31
  "swapp = (\<lambda>(x, y) (z, w). ((x, z), (y, w)))"
haftmann@19281
    32
  appl :: "('a \<Rightarrow> 'b) * 'a \<Rightarrow> 'b"
haftmann@19281
    33
  "appl p = (let (f, x) = p in f x)"
haftmann@19281
    34
haftmann@20187
    35
subsection {* integers *}
haftmann@20187
    36
haftmann@19281
    37
definition
haftmann@19281
    38
  k :: "int"
haftmann@20351
    39
  "k = -42"
haftmann@19281
    40
haftmann@19281
    41
consts
haftmann@19281
    42
  fac :: "int => int"
haftmann@19281
    43
haftmann@19281
    44
recdef fac "measure nat"
haftmann@19281
    45
  "fac j = (if j <= 0 then 1 else j * (fac (j - 1)))"
haftmann@19281
    46
haftmann@19281
    47
subsection {* sums *}
haftmann@19281
    48
haftmann@19281
    49
subsection {* options *}
haftmann@19281
    50
haftmann@19281
    51
subsection {* lists *}
haftmann@19281
    52
haftmann@19281
    53
definition
haftmann@19281
    54
  ps :: "nat list"
haftmann@19281
    55
  "ps = [2, 3, 5, 7, 11]"
haftmann@19281
    56
  qs :: "nat list"
haftmann@19281
    57
  "qs == rev ps"
haftmann@19281
    58
haftmann@19281
    59
subsection {* mutual datatypes *}
haftmann@19281
    60
haftmann@19281
    61
datatype mut1 = Tip | Top mut2
haftmann@19281
    62
  and mut2 = Tip | Top mut1
haftmann@19281
    63
haftmann@19281
    64
consts
haftmann@19281
    65
  mut1 :: "mut1 \<Rightarrow> mut1"
haftmann@19281
    66
  mut2 :: "mut2 \<Rightarrow> mut2"
haftmann@19281
    67
haftmann@19281
    68
primrec
haftmann@19281
    69
  "mut1 mut1.Tip = mut1.Tip"
haftmann@19281
    70
  "mut1 (mut1.Top x) = mut1.Top (mut2 x)"
haftmann@19281
    71
  "mut2 mut2.Tip = mut2.Tip"
haftmann@19281
    72
  "mut2 (mut2.Top x) = mut2.Top (mut1 x)"
haftmann@19281
    73
haftmann@20351
    74
subsection {* records *}
haftmann@19281
    75
haftmann@19281
    76
subsection {* equalities *}
haftmann@19281
    77
haftmann@19789
    78
subsection {* heavy usage of names *}
haftmann@19281
    79
haftmann@19281
    80
definition
haftmann@19281
    81
  f :: nat
haftmann@19281
    82
  "f = 2"
haftmann@19281
    83
  g :: nat
haftmann@19281
    84
  "g = f"
haftmann@19281
    85
  h :: nat
haftmann@19281
    86
  "h = g"
haftmann@19281
    87
haftmann@19281
    88
code_alias
haftmann@19281
    89
  "Codegenerator.f" "Mymod.f"
haftmann@19281
    90
  "Codegenerator.g" "Mymod.A.f"
haftmann@19281
    91
  "Codegenerator.h" "Mymod.A.B.f"
haftmann@19281
    92
haftmann@20351
    93
code_generate (ml, haskell)
haftmann@20351
    94
  n one "0::int" "0::nat" "1::int" "1::nat"
haftmann@20351
    95
code_generate (ml, haskell)
haftmann@20351
    96
  fac
haftmann@20351
    97
code_generate (ml, haskell)
haftmann@20351
    98
  xor
haftmann@20351
    99
code_generate (ml, haskell)
haftmann@20351
   100
  "op + :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   101
  "op - :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   102
  "op * :: nat \<Rightarrow> nat \<Rightarrow> nat"
haftmann@20351
   103
  "op < :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20351
   104
  "op <= :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20351
   105
code_generate (ml, haskell)
haftmann@20351
   106
  Pair fst snd Let split swap swapp appl
haftmann@20351
   107
code_generate (ml, haskell)
haftmann@20351
   108
  k
haftmann@20351
   109
  "op + :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20351
   110
  "op - :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20351
   111
  "op * :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20351
   112
  "op < :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20351
   113
  "op <= :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20351
   114
  fac
haftmann@20351
   115
  "op div :: int \<Rightarrow> int \<Rightarrow> int"
haftmann@20351
   116
  "op mod :: int \<Rightarrow> int \<Rightarrow> int"  
haftmann@20351
   117
code_generate (ml, haskell)
haftmann@20351
   118
  Inl Inr
haftmann@20351
   119
code_generate (ml, haskell)
haftmann@20351
   120
  None Some
haftmann@20351
   121
code_generate (ml, haskell)
haftmann@20351
   122
  hd tl "op @" ps qs
haftmann@20351
   123
code_generate (ml, haskell)
haftmann@20351
   124
  mut1 mut2
haftmann@20351
   125
code_generate (ml, haskell)
haftmann@20351
   126
  "op @" filter concat foldl foldr hd tl
haftmann@20351
   127
  last butlast list_all2
haftmann@20351
   128
  map 
haftmann@20351
   129
  nth 
haftmann@20351
   130
  list_update
haftmann@20351
   131
  take
haftmann@20351
   132
  drop
haftmann@20351
   133
  takeWhile
haftmann@20351
   134
  dropWhile
haftmann@20351
   135
  rev
haftmann@20351
   136
  zip
haftmann@20351
   137
  upt
haftmann@20351
   138
  remdups
haftmann@20351
   139
  remove1
haftmann@20351
   140
  null
haftmann@20351
   141
  "distinct"
haftmann@20351
   142
  replicate
haftmann@20351
   143
  rotate1
haftmann@20351
   144
  rotate
haftmann@20351
   145
  splice
haftmann@20351
   146
code_generate (ml, haskell)
haftmann@20351
   147
  foo1 foo3
haftmann@20351
   148
code_generate (ml, haskell)
haftmann@20351
   149
  "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"
haftmann@20351
   150
  "op = :: nat \<Rightarrow> nat \<Rightarrow> bool"
haftmann@20351
   151
  "op = :: int \<Rightarrow> int \<Rightarrow> bool"
haftmann@20351
   152
  "op = :: 'a * 'b \<Rightarrow> 'a * 'b \<Rightarrow> bool"
haftmann@20351
   153
  "op = :: 'a + 'b \<Rightarrow> 'a + 'b \<Rightarrow> bool"
haftmann@20351
   154
  "op = :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool"
haftmann@20351
   155
  "op = :: 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
haftmann@20351
   156
  "op = :: mut1 \<Rightarrow> mut1 \<Rightarrow> bool"
haftmann@20351
   157
  "op = :: mut2 \<Rightarrow> mut2 \<Rightarrow> bool"
haftmann@20351
   158
code_generate (ml, haskell)
haftmann@20351
   159
  f g h
haftmann@19281
   160
haftmann@19281
   161
code_serialize ml (-)
haftmann@19281
   162
haftmann@19281
   163
end