Skip to content

Instantly share code, notes, and snippets.

@kindaro
Last active September 16, 2022 07:19
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 kindaro/95ddafe160bbd2f62681f9eebb5450da to your computer and use it in GitHub Desktop.
Save kindaro/95ddafe160bbd2f62681f9eebb5450da to your computer and use it in GitHub Desktop.
-- Inspired by <https://duplode.github.io/posts/traversable-a-remix.html>.
module WeirdFish where
import Control.Monad
import Control.Monad.Free
import Data.Functor.Classes
type Nice functor = (Functor functor, Show1 functor)
data WeirdFish α = ∀ functor. Nice functor ⇒ WeirdFish {weirdFish ∷ functor α}
instance Show1 WeirdFish where
liftShowsPrec showsPrec showList precedence WeirdFish {..}
= liftShowsPrec showsPrec showList precedence weirdFish
instance Functor WeirdFish where
fmap function WeirdFish {..} = WeirdFish { weirdFish = fmap function weirdFish}
irradiate ∷ ∀ α β functor. Nice functor ⇒ (α → functor β) → α → Free WeirdFish β
irradiate action x = liftF WeirdFish {weirdFish = action x}
-- Examples.
f ∷ α → [α]
f x = [x, x]
g ∷ (Integral α, Num β) ⇒ α → Maybe β
g x = Just (fromIntegral x + 1)
h ∷ (Integral α, Num β) ⇒ α → Free WeirdFish β
h = irradiate f >=> irradiate g
-- h 1 ≡ Free [ Free (Just (Pure 2)) , Free (Just (Pure 2)) ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment