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