src/HOL/Corec_Examples/Tests/Stream_Friends.thy
author haftmann
Mon, 16 Jun 2025 15:25:38 +0200
changeset 82730 3b98b1b57435
parent 66453 cc19f7ca2ed6
permissions -rw-r--r--
more explicit theorem names for list quantifiers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62725
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Corec_Examples/Tests/Stream_Friends.thy
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     2
    Author:     Aymeric Bouzy, Ecole polytechnique
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, Inria, LORIA, MPII
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     4
    Copyright   2015, 2016
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     5
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     6
Friendly functions on streams.
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     7
*)
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     8
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
     9
section \<open>Friendly Functions on Streams\<close>
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    10
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    11
theory Stream_Friends
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 62732
diff changeset
    12
imports "HOL-Library.BNF_Corec"
62725
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    13
begin
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    14
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    15
codatatype 'a stream = SCons (shd: 'a) (stl: "'a stream")
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    16
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    17
corec pls :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    18
  "pls s s' = SCons (shd s + shd s') (pls (stl s) (stl s'))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    19
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    20
friend_of_corec pls :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    21
  "pls s s' = SCons (shd s + shd s') (pls (stl s) (stl s'))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    22
  sorry
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    23
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    24
corec onetwo :: "nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    25
  "onetwo = SCons 1 (SCons 2 onetwo)"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    26
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    27
corec prd :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    28
  "prd xs ys = SCons (shd xs * shd ys) (pls (prd xs (stl ys)) (prd (stl xs) ys))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    29
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    30
friend_of_corec prd  where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    31
  "prd xs ys = SCons (shd xs * shd ys) (pls (prd xs (stl ys)) (prd (stl xs) ys))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    32
  sorry
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    33
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    34
corec Exp :: "nat stream \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    35
  "Exp xs = SCons (2 ^^ shd xs) (prd (stl xs) (Exp xs))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    36
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    37
friend_of_corec Exp where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    38
  "Exp xs = SCons (2 ^^ shd xs) (prd (stl xs) (Exp xs))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    39
  sorry
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    40
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    41
corec prd2 :: "nat stream \<Rightarrow> nat stream \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    42
  "prd2 xs ys = SCons (shd xs * shd ys) (pls (prd xs (stl ys)) (prd2 (stl xs) ys))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    43
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    44
fun sthe_default :: "'a stream \<Rightarrow> nat \<Rightarrow> 'a stream option \<Rightarrow> 'a stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    45
  "sthe_default s _ None = s"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    46
| "sthe_default _ _ (Some t) = t"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    47
62730
blanchet
parents: 62725
diff changeset
    48
(* FIXME:
62725
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    49
friend_of_corec sthe_default where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    50
  "sthe_default s n opt = SCons (shd (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t)) (stl (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    51
  sorry
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    52
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    53
corec funky0 :: "nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    54
  "funky0 n = SCons 0 (sthe_default undefined n (map_option funky0 undefined))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    55
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    56
consts funky0' :: "nat stream \<Rightarrow> nat stream"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    57
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    58
friend_of_corec funky0' :: "nat stream \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    59
  "funky0' ns = SCons 0 (sthe_default undefined (shd ns) (map_option funky0' undefined))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    60
  sorry
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    61
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    62
corec funky :: "nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    63
  "funky n = SCons 0 (sthe_default (funky (n + 1)) n (map_option funky (Some (n + 2))))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    64
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    65
corec funky' :: "nat \<Rightarrow> nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    66
  "funky' m n = SCons 0 (sthe_default (funky' (m - 1) (n + 1)) m (map_option (%(x, y). funky' (Suc x) (Suc y)) (Some (m - 2, n + 2))))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    67
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    68
corec funky'' :: "nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    69
  "funky'' n = SCons 0 (sthe_default (funky'' (n + 1)) n (Some (funky'' (n + 2))))" 
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    70
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    71
corec phunky0 :: "nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    72
  "phunky0 n = sthe_default undefined n undefined"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    73
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    74
fun lthe_default :: "'a stream \<Rightarrow> 'a stream option \<Rightarrow> 'a stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    75
  "lthe_default s None = s"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    76
| "lthe_default _ (Some t) = t"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    77
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    78
friend_of_corec lthe_default where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    79
  "lthe_default s opt = SCons (shd (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t)) (stl (case opt of None \<Rightarrow> s | Some t \<Rightarrow> t))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    80
  sorry
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    81
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    82
corec phunky_debug :: "'a \<Rightarrow> 'a stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    83
  "phunky_debug n = lthe_default (SCons n (lthe_default undefined (map_option phunky_debug undefined))) undefined"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    84
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    85
corec phunky1 :: "nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    86
  "phunky1 n = sthe_default (SCons 0 (sthe_default (phunky1 (n + 1)) n (map_option phunky1 (Some (n + 2))))) n undefined"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    87
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    88
corec phunky2 :: "nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    89
  "phunky2 n = sthe_default (SCons 0 (sthe_default (phunky2 (n + 1)) n (map_option phunky2 (Some (n + 2))))) n
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    90
     (map_option (%m. SCons m (sthe_default (phunky2 (n + 1)) n (map_option phunky2 (Some (n + 2))))) (Some n))"
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    91
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    92
corec phunky3 :: "nat \<Rightarrow> nat stream" where
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    93
  "phunky3 n = sthe_default (SCons 0 (sthe_default (phunky3 (n + 1)) n (map_option phunky3 (Some (n + 3))))) n
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    94
     (Some (SCons n (sthe_default (phunky3 (n + 1)) n (map_option phunky3 (Some (n + 3))))))"
62732
bf31fd0231ba commented out for now
blanchet
parents: 62730
diff changeset
    95
*)
62725
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    96
5ab1746186c7 new 'corec' example
blanchet
parents:
diff changeset
    97
end