Last active
September 12, 2018 22:31
-
-
Save masaeedu/a2cef07687b516fc6dd0ced71cccc118 to your computer and use it in GitHub Desktop.
Folding type level lists using type level monoids
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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