Skip to content

Instantly share code, notes, and snippets.

@masaeedu
Last active September 12, 2018 22:31
Show Gist options
  • Select an option

  • Save masaeedu/a2cef07687b516fc6dd0ced71cccc118 to your computer and use it in GitHub Desktop.

Select an option

Save masaeedu/a2cef07687b516fc6dd0ced71cccc118 to your computer and use it in GitHub Desktop.
Folding type level lists using type level monoids
{-# LANGUAGE PolyKinds, DataKinds, TypeFamilies, TypeOperators, EmptyDataDecls, GADTs #-}
import Data.Proxy
import Data.Void
import Unsafe.Coerce
type family Foldr (append :: a -> b -> b) (zero :: b) fa :: b where
Foldr append zero '[] = zero;
Foldr append zero (x ': xs) = append x (Foldr append zero xs)
data HList xs where
HNil :: HList '[]
(:::) :: a -> HList as -> HList (a ': as)
infixr 6 :::
choose :: Int -> d -> HList l -> Foldr Either d l
choose n d HNil = d
choose 0 _ (t ::: _) = Left t
choose n d (_ ::: ts) = Right $ choose (n - 1) d ts
-- choose 1 () (1 ::: "foo" ::: HNil) == Right (Left "")
-- choose 1 () HNil == ()
-- choose 1 () ("bar ::: HNil) == Right 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment