src/HOL/ex/Codegenerator.thy
author haftmann
Mon Sep 25 17:04:19 2006 +0200 (2006-09-25)
changeset 20702 8b79d853eabb
parent 20597 65fe827aa595
child 20713 823967ef47f1
permissions -rw-r--r--
added examples for variable name handling
     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 "~~/src/HOL/ex/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   one :: nat
    21   "one = 1"
    22   n :: nat
    23   "n = 42"
    24 
    25 subsection {* pairs *}
    26 
    27 definition
    28   swap :: "'a * 'b \<Rightarrow> 'b * 'a"
    29   "swap p = (let (x, y) = p in (y, x))"
    30   appl :: "('a \<Rightarrow> 'b) * 'a \<Rightarrow> 'b"
    31   "appl p = (let (f, x) = p in f x)"
    32 
    33 subsection {* integers *}
    34 
    35 definition
    36   k :: "int"
    37   "k = -42"
    38 
    39 consts
    40   fac :: "int => int"
    41 
    42 recdef fac "measure nat"
    43   "fac j = (if j <= 0 then 1 else j * (fac (j - 1)))"
    44 
    45 subsection {* sums *}
    46 
    47 subsection {* options *}
    48 
    49 subsection {* lists *}
    50 
    51 definition
    52   ps :: "nat list"
    53   "ps = [2, 3, 5, 7, 11]"
    54   qs :: "nat list"
    55   "qs == rev ps"
    56 
    57 subsection {* mutual datatypes *}
    58 
    59 datatype mut1 = Tip | Top mut2
    60   and mut2 = Tip | Top mut1
    61 
    62 consts
    63   mut1 :: "mut1 \<Rightarrow> mut1"
    64   mut2 :: "mut2 \<Rightarrow> mut2"
    65 
    66 primrec
    67   "mut1 mut1.Tip = mut1.Tip"
    68   "mut1 (mut1.Top x) = mut1.Top (mut2 x)"
    69   "mut2 mut2.Tip = mut2.Tip"
    70   "mut2 (mut2.Top x) = mut2.Top (mut1 x)"
    71 
    72 subsection {* records *}
    73 
    74 subsection {* equalities *}
    75 
    76 subsection {* strings *}
    77 
    78 definition
    79   "mystring = ''my home is my castle''"
    80 
    81 subsection {* heavy usage of names *}
    82 
    83 definition
    84   f :: nat
    85   "f = 2"
    86   g :: nat
    87   "g = f"
    88   h :: nat
    89   "h = g"
    90 
    91 code_constname
    92   f "Mymod.f"
    93   g "Mymod.A.f"
    94   h "Mymod.A.B.f"
    95 
    96 definition
    97   "apply_tower = (\<lambda>x. x (\<lambda>x. x (\<lambda>x. x)))"
    98 
    99 definition
   100   "keywords fun datatype class instance funa classa =
   101     Suc fun + datatype * class mod instance - funa - classa"
   102 
   103 hide (open) const keywords
   104 
   105 definition
   106   "shadow keywords = keywords @ [Codegenerator.keywords 0 0 0 0 0 0]"
   107 
   108 code_gen xor
   109 code_gen one
   110 code_gen
   111   Pair fst snd Let split swap
   112 code_gen "0::int" "1::int"
   113   (SML) (Haskell)
   114 code_gen n
   115   (SML) (Haskell)
   116 code_gen fac
   117   (SML) (Haskell)
   118 code_gen
   119   "op + :: nat \<Rightarrow> nat \<Rightarrow> nat"
   120   "op - :: nat \<Rightarrow> nat \<Rightarrow> nat"
   121   "op * :: nat \<Rightarrow> nat \<Rightarrow> nat"
   122   "op < :: nat \<Rightarrow> nat \<Rightarrow> bool"
   123   "op <= :: nat \<Rightarrow> nat \<Rightarrow> bool"
   124 code_gen
   125   appl
   126 code_gen
   127   k
   128   "op + :: int \<Rightarrow> int \<Rightarrow> int"
   129   "op - :: int \<Rightarrow> int \<Rightarrow> int"
   130   "op * :: int \<Rightarrow> int \<Rightarrow> int"
   131   "op < :: int \<Rightarrow> int \<Rightarrow> bool"
   132   "op <= :: int \<Rightarrow> int \<Rightarrow> bool"
   133   fac
   134   "op div :: int \<Rightarrow> int \<Rightarrow> int"
   135   "op mod :: int \<Rightarrow> int \<Rightarrow> int"  
   136   (SML) (Haskell)
   137 code_gen
   138   Inl Inr
   139 code_gen
   140   None Some
   141 code_gen
   142   hd tl "op @" ps qs
   143 code_gen
   144   mut1 mut2
   145 code_gen
   146   remove1
   147   null
   148   replicate
   149   rotate1
   150   rotate
   151   splice
   152 code_gen
   153   remdups
   154   "distinct"
   155 code_gen
   156   foo1 foo3
   157 code_gen
   158   "OperationalEquality.eq :: bool \<Rightarrow> bool \<Rightarrow> bool"
   159   "OperationalEquality.eq :: nat \<Rightarrow> nat \<Rightarrow> bool"
   160   "OperationalEquality.eq :: int \<Rightarrow> int \<Rightarrow> bool"
   161   "OperationalEquality.eq :: ('a\<Colon>eq) * ('b\<Colon>eq) \<Rightarrow> 'a * 'b \<Rightarrow> bool"
   162   "OperationalEquality.eq :: ('a\<Colon>eq) + ('b\<Colon>eq) \<Rightarrow> 'a + 'b \<Rightarrow> bool"
   163   "OperationalEquality.eq :: ('a\<Colon>eq) option \<Rightarrow> 'a option \<Rightarrow> bool"
   164   "OperationalEquality.eq :: ('a\<Colon>eq) list \<Rightarrow> 'a list \<Rightarrow> bool"
   165   "OperationalEquality.eq :: mut1 \<Rightarrow> mut1 \<Rightarrow> bool"
   166   "OperationalEquality.eq :: mut2 \<Rightarrow> mut2 \<Rightarrow> bool"
   167   "OperationalEquality.eq :: ('a\<Colon>eq) point_scheme \<Rightarrow> 'a point_scheme \<Rightarrow> bool"
   168 code_gen
   169   mystring
   170 code_gen
   171   f g h
   172 code_gen
   173   apply_tower Codegenerator.keywords shadow
   174 
   175 code_gen (SML -)
   176 
   177 end