2021-05-20 13:55:22 +03:00
|
|
|
|||
|
|
|
|
|||
|
|
|
|
||| foldr is the unique solution to the equation:
|
|
|
|
|||
|
|
|
|
||| h f e [] = e
|
|
|
|
||| h f e (x :: xs) = x `h` (foldr f e xs)
|
|
|
|
|||
|
|
|
|
||| (This fact is called 'the universal property of foldr'.)
|
|
|
|
|||
|
|
|
|
||| Since the prelude defines foldr tail-recursively, this fact isn't immediate
|
|
|
|
||| and we need some lemmata to prove it.
|
|
|
|
module Data.Vect.Properties.Foldr
|
|
|
|
|
|
|
|
import Data.Vect
|
|
|
|
import Data.Vect.Elem
|
|
|
|
import Data.Fin
|
2021-05-24 10:48:00 +03:00
|
|
|
import Data.Nat
|
|
|
|
import Data.Nat.Order
|
2021-05-20 13:55:22 +03:00
|
|
|
|
|
|
|
import Syntax.PreorderReasoning
|
2021-05-24 10:48:00 +03:00
|
|
|
import Syntax.PreorderReasoning.Generic
|
|
|
|
|
2021-07-09 11:06:27 +03:00
|
|
|
import Control.Order
|
2021-05-20 13:55:22 +03:00
|
|
|
|
2021-06-01 17:05:04 +03:00
|
|
|
||| Sum implemented with foldr
|
|
|
|
public export
|
2021-10-17 01:32:16 +03:00
|
|
|
sumR : Num a => Foldable f => f a -> a
|
2021-06-01 17:05:04 +03:00
|
|
|
sumR = foldr (+) 0
|
|
|
|
|
|
|
|
%transform "sumR/sum" sumR = sum
|
|
|
|
|
2021-05-20 13:55:22 +03:00
|
|
|
||| A function H : forall n. Vect n A -> B preserving the structure of vectors over A
|
|
|
|
public export
|
|
|
|
record VectHomomorphismProperty {0 A, B : Type} (F : A -> B -> B) (E : B) (H : forall n . Vect n A -> B) where
|
|
|
|
constructor ShowVectHomomorphismProperty
|
|
|
|
nil : H [] = E
|
|
|
|
cons : {0 n : Nat} -> (x : A) -> (xs : Vect n A) -> H (x :: xs) = x `F` (H xs)
|
|
|
|
|
|
|
|
||| There is an extensionally unique function preserving the vector structure
|
|
|
|
export
|
|
|
|
nilConsInitiality :
|
|
|
|
(f : a -> b -> b) -> (e : b)
|
|
|
|
-> (h1, h2 : forall n . Vect n a -> b)
|
|
|
|
-> (prf1 : VectHomomorphismProperty f e h1)
|
|
|
|
-> (prf2 : VectHomomorphismProperty f e h2)
|
|
|
|
-> (xs : Vect n a) -> h1 xs = h2 xs
|
|
|
|
nilConsInitiality f e h1 h2 prf1 prf2 [] = Calc $
|
|
|
|
|~ h1 []
|
|
|
|
~~ e ...(prf1.nil)
|
|
|
|
~~ h2 [] ...(sym prf2.nil)
|
|
|
|
|
|
|
|
nilConsInitiality f e h1 h2 prf1 prf2 (x :: xs) = Calc $
|
|
|
|
|~ h1 (x :: xs)
|
|
|
|
~~ (x `f` (h1 xs)) ...(prf1.cons _ _)
|
|
|
|
~~ (x `f` (h2 xs)) ...(cong (x `f`) $ nilConsInitiality f e h1 h2 prf1 prf2 xs)
|
|
|
|
~~ h2 (x :: xs) ...(sym $ prf2.cons _ _)
|
|
|
|
|
|
|
|
||| extensionality is a congruence with respect to Data.Vect.foldrImpl
|
|
|
|
foldrImplExtensional :
|
|
|
|
(f : a -> b -> b) -> (e : b)
|
|
|
|
-> (go1, go2 : b -> b)
|
|
|
|
-> ((y : b) -> go1 y = go2 y)
|
|
|
|
-> (xs : Vect n a)
|
|
|
|
-> foldrImpl f e go1 xs = foldrImpl f e go2 xs
|
|
|
|
foldrImplExtensional f e go1 go2 ext [] = ext e
|
|
|
|
foldrImplExtensional f e go1 go2 ext (x :: xs) =
|
|
|
|
foldrImplExtensional f e _ _
|
|
|
|
(\y => ext (f x y))
|
|
|
|
xs
|
|
|
|
|
|
|
|
||| foldrImpl f e x : (b -> -) -> - is natural
|
|
|
|
foldrImplNaturality : (f : a -> b -> b) -> (e : b) -> (xs : Vect n a) -> (go1, go2 : b -> b)
|
|
|
|
-> foldrImpl f e (go1 . go2) xs = go1 (foldrImpl f e go2 xs)
|
|
|
|
foldrImplNaturality f e [] go1 go2 = Refl
|
|
|
|
foldrImplNaturality f e (x :: xs) go1 go2 = foldrImplNaturality f e xs go1 (go2 . (f x))
|
|
|
|
|
|
|
|
||| Our tail-recursive foldr preserves the vector structure
|
|
|
|
export
|
|
|
|
foldrVectHomomorphism : VectHomomorphismProperty f e (foldr f e)
|
|
|
|
foldrVectHomomorphism = ShowVectHomomorphismProperty
|
|
|
|
{ nil = Refl
|
|
|
|
, cons = \x, xs => Calc $
|
|
|
|
|~ foldr f e (x :: xs)
|
|
|
|
~~ foldrImpl f e (id . (f x)) xs ...(Refl)
|
|
|
|
~~ foldrImpl f e ((f x) . id) xs ...(foldrImplExtensional f e _ _ (\y => Refl) xs)
|
|
|
|
~~ f x (foldrImpl f e id xs) ...(foldrImplNaturality f e xs (f x) _)
|
|
|
|
~~ f x (foldr f e xs) ...(Refl)
|
|
|
|
}
|
|
|
|
|
|
|
|
||| foldr is the unique function preserving the vector structure
|
|
|
|
export
|
|
|
|
foldrUniqueness : (h : forall n . Vect n a -> b) -> VectHomomorphismProperty f e h -> (xs : Vect n a) -> h xs = foldr f e xs
|
|
|
|
foldrUniqueness {f} h prf xs = irrelevantEq $
|
|
|
|
nilConsInitiality f e h (foldr f e) prf foldrVectHomomorphism xs
|
2021-05-24 10:48:00 +03:00
|
|
|
|
|
|
|
|
|
|
|
||| Each summand is `LTE` the sum
|
|
|
|
export
|
2021-06-01 17:05:04 +03:00
|
|
|
sumIsGTEtoParts : {x : Nat} -> (xs : Vect n Nat) -> (x `Elem` xs) -> sumR xs `GTE` x
|
2021-05-24 10:48:00 +03:00
|
|
|
sumIsGTEtoParts (x :: xs) Here
|
|
|
|
= CalcWith $
|
|
|
|
|~ x
|
|
|
|
~~ x + 0 ...(sym $ plusZeroRightNeutral _)
|
2021-06-01 17:05:04 +03:00
|
|
|
<~ x + (sumR xs) ...(plusLteMonotoneLeft x 0 _ LTEZero)
|
|
|
|
~~ sumR (x :: xs) ...(sym $ (foldrVectHomomorphism {f = plus} {e = 0}).cons _ _)
|
2021-05-24 10:48:00 +03:00
|
|
|
|
|
|
|
sumIsGTEtoParts {x} (y :: xs) (There later)
|
|
|
|
= CalcWith $
|
|
|
|
|~ x
|
2021-06-01 17:05:04 +03:00
|
|
|
<~ sumR xs ...(sumIsGTEtoParts {x} xs later)
|
|
|
|
~~ 0 + sumR xs ...(Refl)
|
|
|
|
<~ y + (sumR xs) ...(plusLteMonotoneRight (sumR xs) 0 y LTEZero)
|
|
|
|
~~ sumR (y :: xs) ...(sym $ (foldrVectHomomorphism {f = plus} {e = 0}).cons _ _)
|
2021-05-24 10:48:00 +03:00
|
|
|
|
2021-06-01 17:05:04 +03:00
|
|
|
||| `sumR : Vect n Nat -> Nat` is monotone
|
2021-05-24 10:48:00 +03:00
|
|
|
export
|
|
|
|
sumMonotone : {n : Nat} -> (xs, ys : Vect n Nat)
|
|
|
|
-> (prf : (i : Fin n) -> index i xs `LTE` index i ys)
|
2021-06-01 17:05:04 +03:00
|
|
|
-> (sumR xs `LTE` sumR ys)
|
2021-05-24 10:48:00 +03:00
|
|
|
sumMonotone [] [] prf = LTEZero
|
|
|
|
sumMonotone (x :: xs) (y :: ys) prf =
|
|
|
|
let prf' = sumMonotone xs ys (\i => prf (FS i))
|
|
|
|
in CalcWith $
|
2021-06-01 17:05:04 +03:00
|
|
|
|~ sumR (x :: xs)
|
|
|
|
~~ x + sumR xs ...((foldrVectHomomorphism {f = plus} {e = 0}).cons x xs)
|
|
|
|
<~ y + sumR ys ...(plusLteMonotone (prf 0) prf')
|
|
|
|
~~ sumR (y :: ys) ...(sym $ (foldrVectHomomorphism {f = plus} {e = 0}).cons y ys)
|