src/HOL/ex/Codegenerator.thy
changeset 21404 eb85850d3eb7
parent 21319 cf814e36f788
child 21420 8b15e5e66813
equal deleted inserted replaced
21403:dd58f13a8eb4 21404:eb85850d3eb7
     9 begin
     9 begin
    10 
    10 
    11 subsection {* booleans *}
    11 subsection {* booleans *}
    12 
    12 
    13 definition
    13 definition
    14   xor :: "bool \<Rightarrow> bool \<Rightarrow> bool"
    14   xor :: "bool \<Rightarrow> bool \<Rightarrow> bool" where
    15   "xor p q = ((p | q) & \<not> (p & q))"
    15   "xor p q = ((p | q) & \<not> (p & q))"
    16 
    16 
    17 subsection {* natural numbers *}
    17 subsection {* natural numbers *}
    18 
    18 
    19 definition
    19 definition
    20   n :: nat
    20   n :: nat where
    21   "n = 42"
    21   "n = 42"
    22 
    22 
    23 subsection {* pairs *}
    23 subsection {* pairs *}
    24 
    24 
    25 definition
    25 definition
    26   swap :: "'a * 'b \<Rightarrow> 'b * 'a"
    26   swap :: "'a * 'b \<Rightarrow> 'b * 'a" where
    27   "swap p = (let (x, y) = p in (y, x))"
    27   "swap p = (let (x, y) = p in (y, x))"
    28   appl :: "('a \<Rightarrow> 'b) * 'a \<Rightarrow> 'b"
    28 
       
    29 definition
       
    30   appl :: "('a \<Rightarrow> 'b) * 'a \<Rightarrow> 'b" where
    29   "appl p = (let (f, x) = p in f x)"
    31   "appl p = (let (f, x) = p in f x)"
    30   snd_three :: "'a * 'b * 'c => 'b"
    32 
       
    33 definition
       
    34   snd_three :: "'a * 'b * 'c => 'b" where
    31   "snd_three a = id (\<lambda>(a, b, c). b) a"
    35   "snd_three a = id (\<lambda>(a, b, c). b) a"
    32 
    36 
    33 lemma [code]:
    37 lemma [code]:
    34   "swap (x, y) = (y, x)"
    38   "swap (x, y) = (y, x)"
    35   unfolding swap_def Let_def by auto
    39   unfolding swap_def Let_def by auto
    39   unfolding appl_def Let_def by auto
    43   unfolding appl_def Let_def by auto
    40 
    44 
    41 subsection {* integers *}
    45 subsection {* integers *}
    42 
    46 
    43 definition
    47 definition
    44   k :: "int"
    48   k :: "int" where
    45   "k = -42"
    49   "k = -42"
    46 
    50 
    47 function
    51 function
    48   fac :: "int => int" where
    52   fac :: "int => int" where
    49   "fac j = (if j <= 0 then 1 else j * (fac (j - 1)))"
    53   "fac j = (if j <= 0 then 1 else j * (fac (j - 1)))"
    57 subsection {* options *}
    61 subsection {* options *}
    58 
    62 
    59 subsection {* lists *}
    63 subsection {* lists *}
    60 
    64 
    61 definition
    65 definition
    62   ps :: "nat list"
    66   ps :: "nat list" where
    63   "ps = [2, 3, 5, 7, 11]"
    67   "ps = [2, 3, 5, 7, 11]"
    64   qs :: "nat list"
    68 
       
    69 definition
       
    70   qs :: "nat list" where
    65   "qs == rev ps"
    71   "qs == rev ps"
    66 
    72 
    67 subsection {* mutual datatypes *}
    73 subsection {* mutual datatypes *}
    68 
    74 
    69 datatype mut1 = Tip | Top mut2
    75 datatype mut1 = Tip | Top mut2