Skip to content

Instantly share code, notes, and snippets.

@monadplus
Created January 29, 2020 22:05
Show Gist options
  • Save monadplus/655960971b485ec87c5c6ea20a2ad4ac to your computer and use it in GitHub Desktop.
Save monadplus/655960971b485ec87c5c6ea20a2ad4ac to your computer and use it in GitHub Desktop.
-- | a. Fix that for me - write a list that allows me to hold any functions as
-- long as the input of one lines up with the output of the next.
data TypeAlignedList a b where
TNil :: TypeAlignedList a a
TCons :: (a -> r) -> TypeAlignedList r b -> TypeAlignedList a b
-- >>> f = TCons (+2) (TCons show TNil); f :: TypeAlignedList Int String
-- | b. Which types are existential?
-- r
-- | c. Write a function to append type-aligned lists. This is almost certainly
-- not as difficult as you'd initially think.
composeTALs :: TypeAlignedList b c -> TypeAlignedList a b -> TypeAlignedList a c
composeTALs bc TNil = bc -- a ~ b => TypeAlignedList b c ~ TypeAlignedList a c
composeTALs bc (TCons ax txb) = TCons ax (composeTALs bc txb)
evalTAL :: TypeAlignedList a b -> (a -> b)
evalTAL TNil = id
evalTAL (TCons ax txb) = evalTAL txb . ax
-- >>> f = (TCons show TNil); f :: TypeAlignedList Int String
-- >>> g = (TCons read TNil); g :: TypeAlignedList String Int
-- >>> gof = composeTALs g f
-- (evalTAL gof) 1234
-- 1234
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment