Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Last active December 19, 2020 22:05
Show Gist options
  • Save ebresafegaga/4ffcb2840978f9846a421c6a36a83121 to your computer and use it in GitHub Desktop.
Save ebresafegaga/4ffcb2840978f9846a421c6a36a83121 to your computer and use it in GitHub Desktop.
It's all about the motive, baby
import Data.Vect
-- This is a paramorphism (aka primitive recursion) on Vectors
foldr : (motive : (k : Nat) -> Vect k a -> Type) ->
((l : Nat) -> (x : a) -> (xs : Vect l a) -> motive l xs -> motive (S l) (x :: xs)) ->
motive Z [] ->
(vec : Vect n a) ->
motive n vec
foldr {n = Z} motive step base [] = base
foldr {n = S k} motive step base (x :: xs) = step k x xs (foldr motive step base xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment