Skip to content

Instantly share code, notes, and snippets.

@oisdk
Last active February 12, 2020 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oisdk/dc41f750ac51a3102860493047a3e76b to your computer and use it in GitHub Desktop.
Save oisdk/dc41f750ac51a3102860493047a3e76b to your computer and use it in GitHub Desktop.
{-# LANGUAGE DataKinds, GADTs, KindSignatures, PolyKinds, RankNTypes, ScopedTypeVariables, TypeOperators #-}
import Data.Kind
import Data.Coerce
data Nat = Z | S Nat
data Vec (a :: Type) (n :: Nat) where
Nil :: Vec a Z
(:-) :: a -> Vec a n -> Vec a (S n)
newtype (:.:) (f :: b -> Type) (g :: a -> b) (x :: a) = Comp { unComp :: f (g x) }
foldlVec :: forall a b n. (forall m. a -> b m -> b (S m)) -> b Z -> Vec a n -> b n
foldlVec f b Nil = b
foldlVec f b (x :- xs) = unComp (foldlVec (c f) (Comp (f x b)) xs)
where
c :: (a -> b (S m) -> b (S (S m))) -> (a -> (b :.: S) m -> (b :.: S) (S m))
c = coerce
{-# INLINE c #-}
newtype VecCont a b n = VecCont { runVecCont :: Vec b n -> Vec (a,b) n }
revZip :: Vec a n -> Vec b n -> Vec (a,b) n
revZip = runVecCont .
foldlVec
(\x k -> VecCont (\(y :- ys) -> (x,y) :- runVecCont k ys))
(VecCont (const Nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment