src/HOL/ex/Computations.thy
author haftmann
Mon, 06 Feb 2017 20:56:32 +0100
changeset 64988 93aaff2b0ae0
parent 64987 1985502518ce
child 64989 40c36a4aee1f
permissions -rw-r--r--
computations and partiality
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64959
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     1
(*  Title:      HOL/ex/Computations.thy
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     2
    Author:     Florian Haftmann, TU Muenchen
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     3
*)
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     4
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     5
section \<open>Simple example for computations generated by the code generator\<close>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     6
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     7
theory Computations
64987
haftmann
parents: 64959
diff changeset
     8
  imports "../Nat" "../Fun_Def" "../Num" "../Code_Numeral"
64959
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
     9
begin
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    10
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    11
fun even :: "nat \<Rightarrow> bool"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    12
  where "even 0 \<longleftrightarrow> True"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    13
      | "even (Suc 0) \<longleftrightarrow> False"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    14
      | "even (Suc (Suc n)) \<longleftrightarrow> even n"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    15
  
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    16
fun fib :: "nat \<Rightarrow> nat"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    17
  where "fib 0 = 0"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    18
      | "fib (Suc 0) = Suc 0"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    19
      | "fib (Suc (Suc n)) = fib (Suc n) + fib n"
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    20
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    21
declare [[ML_source_trace]]
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    22
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    23
ML \<open>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    24
local 
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    25
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    26
fun int_of_nat @{code "0 :: nat"} = 0
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    27
  | int_of_nat (@{code Suc} n) = int_of_nat n + 1;
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    28
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    29
in
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    30
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    31
val comp_nat = @{computation "0 :: nat" Suc
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    32
  "plus :: nat \<Rightarrow>_" "times :: nat \<Rightarrow> _" fib :: nat}
64988
93aaff2b0ae0 computations and partiality
haftmann
parents: 64987
diff changeset
    33
  (fn post => post o HOLogic.mk_nat o int_of_nat o the);
64959
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    34
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    35
val comp_numeral = @{computation "0 :: nat" "1 :: nat" "2 :: nat" "3 :: nat" :: nat}
64988
93aaff2b0ae0 computations and partiality
haftmann
parents: 64987
diff changeset
    36
  (fn post => post o HOLogic.mk_nat o int_of_nat o the);
64959
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    37
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    38
val comp_bool = @{computation True False HOL.conj HOL.disj HOL.implies
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    39
  HOL.iff even "less_eq :: nat \<Rightarrow> _" "less :: nat \<Rightarrow> _" "HOL.eq :: nat \<Rightarrow> _" :: bool }
64988
93aaff2b0ae0 computations and partiality
haftmann
parents: 64987
diff changeset
    40
  (K the);
64959
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    41
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    42
end
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    43
\<close>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    44
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    45
declare [[ML_source_trace = false]]
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    46
  
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    47
ML_val \<open>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    48
  comp_nat @{context} @{term "fib (Suc (Suc (Suc 0)) * Suc (Suc (Suc 0))) + Suc 0"}
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    49
  |> Syntax.string_of_term @{context}
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    50
  |> writeln
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    51
\<close>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    52
  
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    53
ML_val \<open>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    54
  comp_bool @{context} @{term "fib (Suc (Suc (Suc 0)) * Suc (Suc (Suc 0))) + Suc 0 < fib (Suc (Suc 0))"}
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    55
\<close>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    56
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    57
ML_val \<open>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    58
  comp_numeral @{context} @{term "Suc 42 + 7"}
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    59
  |> Syntax.string_of_term @{context}
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    60
  |> writeln
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    61
\<close>
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    62
9ca021bd718d ML antiquotation for generated computations
haftmann
parents:
diff changeset
    63
end