src/HOL/Data_Structures/Time_Examples.thy
author nipkow
Thu, 06 Feb 2025 17:28:27 +0100
changeset 82099 168f9a5824a8
permissions -rw-r--r--
added Example theory
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
82099
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     1
(*
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     2
Author: Jonas Stahl
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     3
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     4
Nonstandard examples of the time function definition commands.
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     5
*)
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     6
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     7
theory Time_Examples
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     8
imports Define_Time_Function
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
     9
begin
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    10
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    11
fun even :: "nat \<Rightarrow> bool"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    12
  and odd :: "nat \<Rightarrow> bool" where
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    13
  "even 0 = True"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    14
| "odd 0 = False"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    15
| "even (Suc n) = odd n"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    16
| "odd (Suc n) = even n"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    17
time_fun even odd
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    18
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    19
locale locTests =
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    20
  fixes f :: "'a \<Rightarrow> 'b"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    21
   and  T_f :: "'a \<Rightarrow> nat"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    22
begin
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    23
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    24
fun simple where
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    25
  "simple a = f a"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    26
time_fun simple
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    27
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    28
fun even :: "'a list \<Rightarrow> 'b list" and odd :: "'a list \<Rightarrow> 'b list" where
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    29
  "even [] = []"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    30
| "odd [] = []"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    31
| "even (x#xs) = f x # odd xs"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    32
| "odd (x#xs) = even xs"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    33
time_fun even odd
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    34
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    35
fun locSum :: "nat list \<Rightarrow> nat" where
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    36
  "locSum [] = 0"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    37
| "locSum (x#xs) = x + locSum xs"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    38
time_fun locSum
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    39
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    40
fun map :: "'a list \<Rightarrow> 'b list" where
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    41
  "map [] = []"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    42
| "map (x#xs) = f x # map xs"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    43
time_fun map
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    44
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    45
end
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    46
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    47
definition "let_lambda a b c \<equiv> let lam = (\<lambda>a b. a + b) in lam a (lam b c)"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    48
time_fun let_lambda
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    49
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    50
partial_function (tailrec) collatz :: "nat \<Rightarrow> bool" where
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    51
  "collatz n = (if n \<le> 1 then True
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    52
                else if n mod 2 = 0 then collatz (n div 2)
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    53
                else collatz (3 * n + 1))"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    54
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    55
text \<open>This is the expected time function:\<close>
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    56
partial_function (option) T_collatz' :: "nat \<Rightarrow> nat option" where
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    57
  "T_collatz' n = (if n \<le> 1 then Some 0
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    58
                else if n mod 2 = 0 then Option.bind (T_collatz' (n div 2)) (\<lambda>k. Some (Suc k))
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    59
                else Option.bind (T_collatz' (3 * n + 1)) (\<lambda>k. Some (Suc k)))"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    60
time_fun_0 "(mod)"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    61
time_partial_function collatz
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    62
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    63
text \<open>Proof that they are the same up to 20:\<close>
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    64
lemma setIt: "P i \<Longrightarrow> \<forall>n \<in> {Suc i..j}. P n \<Longrightarrow> \<forall>n \<in> {i..j}. P n"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    65
  by (metis atLeastAtMost_iff le_antisym not_less_eq_eq)
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    66
lemma "\<forall>n \<in> {2..20}. T_collatz n = T_collatz' n"
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    67
  apply (rule setIt, simp add: T_collatz.simps T_collatz'.simps, simp)+
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    68
  by (simp add: T_collatz.simps T_collatz'.simps)
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    69
168f9a5824a8 added Example theory
nipkow
parents:
diff changeset
    70
end