src/HOL/ex/Codegenerator.thy
author haftmann
Tue May 09 10:11:47 2006 +0200 (2006-05-09)
changeset 19604 02f5fbdd5c54
parent 19281 b411f25fff25
child 19789 c08c9f9ea9a5
permissions -rw-r--r--
removed 1::int
     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
     9 begin
    10 
    11 subsection {* booleans *}
    12 
    13 definition
    14   xor :: "bool \<Rightarrow> bool \<Rightarrow> bool"
    15   "xor p q = ((p | q) & \<not> (p & q))"
    16 
    17 code_generate xor
    18 
    19 subsection {* natural numbers *}
    20 
    21 definition
    22   one :: nat
    23   "one = 1"
    24   n :: nat
    25   "n = 42"
    26 
    27 code_generate
    28   "0::nat" "one" n
    29   "op + :: nat \<Rightarrow> nat \<Rightarrow> nat"
    30   "op - :: nat \<Rightarrow> nat \<Rightarrow> nat"
    31   "op * :: nat \<Rightarrow> nat \<Rightarrow> nat"
    32   "op < :: nat \<Rightarrow> nat \<Rightarrow> bool"
    33   "op <= :: nat \<Rightarrow> nat \<Rightarrow> bool"
    34 
    35 subsection {* pairs *}
    36 
    37 definition
    38   swap :: "'a * 'b \<Rightarrow> 'b * 'a"
    39   "swap p = (let (x, y) = p in (y, x))"
    40   swapp :: "'a * 'b \<Rightarrow> 'c * 'd \<Rightarrow> ('a * 'c) * ('b * 'd)"
    41   "swapp = (\<lambda>(x, y) (z, w). ((x, z), (y, w)))"
    42   appl :: "('a \<Rightarrow> 'b) * 'a \<Rightarrow> 'b"
    43   "appl p = (let (f, x) = p in f x)"
    44 
    45 code_generate Pair fst snd Let split swap swapp appl
    46 
    47 definition
    48   k :: "int"
    49   "k = 42"
    50 
    51 consts
    52   fac :: "int => int"
    53 
    54 recdef fac "measure nat"
    55   "fac j = (if j <= 0 then 1 else j * (fac (j - 1)))"
    56 
    57 code_generate
    58   "0::int" k
    59   "op + :: int \<Rightarrow> int \<Rightarrow> int"
    60   "op - :: int \<Rightarrow> int \<Rightarrow> int"
    61   "op * :: int \<Rightarrow> int \<Rightarrow> int"
    62   "op < :: int \<Rightarrow> int \<Rightarrow> bool"
    63   "op <= :: int \<Rightarrow> int \<Rightarrow> bool"
    64   fac
    65 
    66 subsection {* sums *}
    67 
    68 code_generate Inl Inr
    69 
    70 subsection {* options *}
    71 
    72 code_generate None Some
    73 
    74 subsection {* lists *}
    75 
    76 definition
    77   ps :: "nat list"
    78   "ps = [2, 3, 5, 7, 11]"
    79   qs :: "nat list"
    80   "qs == rev ps"
    81 
    82 code_generate hd tl "op @" ps qs
    83 
    84 subsection {* mutual datatypes *}
    85 
    86 datatype mut1 = Tip | Top mut2
    87   and mut2 = Tip | Top mut1
    88 
    89 consts
    90   mut1 :: "mut1 \<Rightarrow> mut1"
    91   mut2 :: "mut2 \<Rightarrow> mut2"
    92 
    93 primrec
    94   "mut1 mut1.Tip = mut1.Tip"
    95   "mut1 (mut1.Top x) = mut1.Top (mut2 x)"
    96   "mut2 mut2.Tip = mut2.Tip"
    97   "mut2 (mut2.Top x) = mut2.Top (mut1 x)"
    98 
    99 code_generate mut1 mut2
   100 
   101 subsection {* equalities *}
   102 
   103 code_generate
   104   "op = :: bool \<Rightarrow> bool \<Rightarrow> bool"
   105   "op = :: nat \<Rightarrow> nat \<Rightarrow> bool"
   106   "op = :: int \<Rightarrow> int \<Rightarrow> bool"
   107   "op = :: 'a * 'b \<Rightarrow> 'a * 'b \<Rightarrow> bool"
   108   "op = :: 'a + 'b \<Rightarrow> 'a + 'b \<Rightarrow> bool"
   109   "op = :: 'a option \<Rightarrow> 'a option \<Rightarrow> bool"
   110   "op = :: 'a list \<Rightarrow> 'a list \<Rightarrow> bool"
   111   "op = :: mut1 \<Rightarrow> mut1 \<Rightarrow> bool"
   112   "op = :: mut2 \<Rightarrow> mut2 \<Rightarrow> bool"
   113 
   114 subsection {* heavy use of names *}
   115 
   116 definition
   117   f :: nat
   118   "f = 2"
   119   g :: nat
   120   "g = f"
   121   h :: nat
   122   "h = g"
   123 
   124 code_alias
   125   "Codegenerator.f" "Mymod.f"
   126   "Codegenerator.g" "Mymod.A.f"
   127   "Codegenerator.h" "Mymod.A.B.f"
   128 
   129 code_generate f g h
   130 
   131 code_serialize ml (-)
   132 
   133 end