bsc-leon-vatthauer/slides/code-examples/reverse.agda

36 lines
1 KiB
Agda
Raw Normal View History

2024-01-03 19:51:04 +01:00
{-# OPTIONS --guardedness #-}
open import Codata.Musical.Notation
open import Data.Nat
module reverse where
data Delay (A : Set) : Set where
now : A Delay A
later : (Delay A) Delay A
2024-01-12 17:36:59 +01:00
run_for_steps : {A : Set} Delay A Delay A
run now x for n steps = now x
run later x for zero steps = later x
2024-01-22 17:57:12 +01:00
run later x for suc n steps = run x for n steps
2024-01-12 17:36:59 +01:00
never : {A : Set} Delay A
never = later ( never)
data Colist (A : Set) : Set where
[] : Colist A
_∷_ : A (Colist A) Colist A
2024-01-03 19:51:04 +01:00
reverse : {A : Set} Colist A Delay (Colist A)
2024-01-22 17:57:12 +01:00
reverse {A} l = reverseAcc l []
2024-01-03 19:51:04 +01:00
where
reverseAcc : Colist A Colist A Delay (Colist A)
2024-01-11 13:42:27 +01:00
reverseAcc [] a = now a
reverseAcc (x xs) a = later ( reverseAcc ( xs) (x ( a)))
2024-01-03 19:51:04 +01:00
fin-colist : Colist
fin-colist = 1 (2 (3 []))
2024-01-23 13:25:18 +01:00
ones : Colist
ones = 1 ones
2024-01-03 19:51:04 +01:00
-- run reverse fin-colist for 5 steps
2024-01-23 13:25:18 +01:00
-- run reverse ones for 1000 steps