src/HOL/Corec_Examples/Tests/TLList_Friends.thy
author Manuel Eberl <manuel@pruvisto.org>
Tue, 22 Apr 2025 15:41:34 +0200
changeset 82536 e0892dfd1b27
parent 66453 cc19f7ca2ed6
permissions -rw-r--r--
removed possible problematic dependency in HOL-Library.Multiset
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
     1
(*  Title:      HOL/Corec_Examples/Tests/TLList_Friends.thy
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
     2
    Author:     Aymeric Bouzy, Ecole polytechnique
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
     3
    Author:     Jasmin Blanchette, Inria, LORIA, MPII
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
     4
    Copyright   2015, 2016
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
     5
62726
5b2a7caa855b tuned examples
blanchet
parents: 62696
diff changeset
     6
Friendly functions on terminated lists.
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
     7
*)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
     8
62726
5b2a7caa855b tuned examples
blanchet
parents: 62696
diff changeset
     9
section \<open>Friendly Functions on Terminated Lists\<close>
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    10
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    11
theory TLList_Friends
66453
cc19f7ca2ed6 session-qualified theory imports: isabelle imports -U -i -d '~~/src/Benchmarks' -a;
wenzelm
parents: 62726
diff changeset
    12
imports "HOL-Library.BNF_Corec"
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    13
begin
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    14
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    15
codatatype ('a, 'b) tllist =
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    16
  TLNil (trm: 'b)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    17
| TLCons (tlhd: 'a) (tltl: "('a, 'b) tllist")
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    18
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    19
corec pls :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    20
  "pls s s' = TLCons (tlhd s + tlhd s') (pls (tltl s) (tltl s'))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    21
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    22
friend_of_corec pls where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    23
  "pls s s' = TLCons (tlhd s + tlhd s') (pls (tltl s) (tltl s'))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    24
  sorry
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    25
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    26
corec prd :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    27
  "prd xs ys = TLCons (tlhd xs * tlhd ys) (pls (prd xs (tltl ys)) (prd (tltl xs) ys))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    28
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    29
friend_of_corec prd where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    30
  "prd xs ys = TLCons (tlhd xs * tlhd ys) (pls (prd xs (tltl ys)) (prd (tltl xs) ys))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    31
  sorry
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    32
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    33
corec onetwo :: "(nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    34
  "onetwo = TLCons 1 (TLCons 2 onetwo)"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    35
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    36
corec Exp :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    37
  "Exp xs = TLCons (2 ^^ tlhd xs) (prd (tltl xs) (Exp xs))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    38
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    39
friend_of_corec Exp where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    40
  "Exp xs = TLCons (2 ^^ tlhd xs) (prd (tltl xs) (Exp xs))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    41
  sorry
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    42
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    43
corec prd2 :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    44
  "prd2 xs ys = TLCons (tlhd xs * tlhd ys) (pls (prd xs (tltl ys)) (prd2 (tltl xs) ys))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    45
62726
5b2a7caa855b tuned examples
blanchet
parents: 62696
diff changeset
    46
text \<open>Outer if:\<close>
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    47
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    48
corec takeUntilOdd :: "(int, int) tllist \<Rightarrow> (int, int) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    49
  "takeUntilOdd xs =
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    50
     (if is_TLNil xs then TLNil (trm xs)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    51
      else if tlhd xs mod 2 = 0 then TLCons (tlhd xs) (takeUntilOdd (tltl xs))
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    52
      else TLNil (tlhd xs))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    53
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    54
friend_of_corec takeUntilOdd where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    55
  "takeUntilOdd xs =
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    56
     (if is_TLNil xs then TLNil (trm xs)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    57
      else if tlhd xs mod 2 = 0 then TLCons (tlhd xs) (takeUntilOdd (tltl xs))
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    58
      else TLNil (tlhd xs))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    59
      sorry
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    60
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    61
corec takeUntilEven :: "(int, int) tllist \<Rightarrow> (int, int) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    62
  "takeUntilEven xs =
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    63
     (case xs of
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    64
        TLNil y \<Rightarrow> TLNil y
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    65
      | TLCons y ys \<Rightarrow>
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    66
        if y mod 2 = 1 then TLCons y (takeUntilEven ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    67
        else TLNil y)"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    68
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    69
friend_of_corec takeUntilEven where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    70
  "takeUntilEven xs =
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    71
     (case xs of
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    72
        TLNil y \<Rightarrow> TLNil y
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    73
      | TLCons y ys \<Rightarrow>
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    74
        if y mod 2 = 1 then TLCons y (takeUntilEven ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    75
        else TLNil y)"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    76
        sorry
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    77
62726
5b2a7caa855b tuned examples
blanchet
parents: 62696
diff changeset
    78
text \<open>If inside the constructor:\<close>
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    79
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    80
corec prd3 :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    81
  "prd3 xs ys = TLCons (tlhd xs * tlhd ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    82
     (if undefined then pls (prd xs (tltl ys)) (prd (tltl xs) ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    83
      else prd3 (tltl xs) (tltl ys))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    84
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    85
friend_of_corec prd3 where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    86
  "prd3 xs ys = TLCons (tlhd xs * tlhd ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    87
     (if undefined then pls (prd xs (tltl ys)) (prd (tltl xs) ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    88
      else prd3 (tltl xs) (tltl ys))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    89
 sorry
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    90
62726
5b2a7caa855b tuned examples
blanchet
parents: 62696
diff changeset
    91
text \<open>If inside the inner friendly operation:\<close>
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    92
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    93
corec prd4 :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    94
  "prd4 xs ys = TLCons (tlhd xs * tlhd ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    95
     (pls (if undefined then prd xs (tltl ys) else xs) (prd (tltl xs) ys))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    96
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    97
friend_of_corec prd4 :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    98
  "prd4 xs ys = TLCons (tlhd xs * tlhd ys)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
    99
     (pls (if undefined then prd xs (tltl ys) else xs) (prd (tltl xs) ys))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   100
  sorry
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   101
62726
5b2a7caa855b tuned examples
blanchet
parents: 62696
diff changeset
   102
text \<open>Lots of if's:\<close>
62696
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   103
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   104
corec iffy :: "(nat, 'b) tllist \<Rightarrow> (nat, 'b) tllist" where
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   105
  "iffy xs =
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   106
   (if undefined (0::nat) then
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   107
      TLNil undefined
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   108
    else
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   109
      Exp (if undefined (1::nat) then
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   110
             Exp undefined
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   111
           else
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   112
             TLCons undefined
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   113
               (if undefined (2::nat) then
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   114
                  Exp (if undefined (3::nat) then
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   115
                         Exp (if undefined (4::nat) then
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   116
                                iffy (tltl xs)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   117
                              else if undefined (5::nat) then
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   118
                                iffy xs
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   119
                              else
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   120
                                xs)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   121
                       else
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   122
                         xs)
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   123
                else
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   124
                  undefined)))"
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   125
7325d8573fb8 added 'corec' examples and tests
blanchet
parents:
diff changeset
   126
end