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