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