this post was submitted on 23 Jul 2023
1 points (100.0% liked)

Haskell

5 readers
1 users here now

**The Haskell programming language community.** Daily news and info about all things Haskell related: practical stuff, theory, types, libraries, jobs, patches, releases, events and conferences and more... ### Links - Get Started with Haskell

founded 2 years ago
 

I finally found a decent definition of free alternatives:

newtype Free f a = Free { alts :: [Free' f a] } deriving Functor
instance Applicative (Free f) where
  pure = Free . pure . Pure
  Free ps <*> q0 = asum $ map (`apL` q0) ps where
    apL (Pure f) q = f <$> q
    apL (Free' x p) q = Free $ pure $ Free' x $ flip <$> p <*> q
instance Alternative (Free f) where
  empty = Free empty
  Free ps <|> Free qs = Free (ps <|> qs)
data Free' f a = Pure a | forall x. Free' (f x) (Free f (x -> a))
deriving instance Functor (Free' f)

#haskell

top 1 comments
sorted by: hot top controversial new old
[–] jaror@kbin.social 1 points 2 years ago

Exercise: why did I not write an instance Applicative (Free' f) and used deriving (Functor, Applicative, Alternative) via Compose [] (Free' f) to get the instances for Free?