20 ms·
> I wish i could apply f x y instead of lift $ f <$> x <*> pure y. In "Applicative Programming with Effects"[0], the authors use such a notation, and even leav
by siraben 4y ago
> I wish i could apply f x y instead of lift $ f <$> x <*> pure y.
In "Applicative Programming with Effects"[0], the authors use such a notation, and even leave it as an exercise to implement it in Haskell using MultiParamTypeClasses:
> Given Haskell extended with multi-parameter type classes, enthusiasts for overloading may replace ‘⟦’ and ‘⟧’ by identifiers i[ and ]i with the right behaviour.
So they have code that looks like
eval :: Exp v -> Env v -> Int
eval (Var x) = fetch x
eval (Val i) = ⟦ i ⟧
eval (Add p q) = ⟦ (+) (eval p) (eval q) ⟧
> Yes so why doesn’t some GHC extension do this?
It's possible to derive the functor instance automatically if the data type is built from products, sums and arrows. However even for Applicatives you can't do this in general because there can be many lawful Applicative instances for the same type, for instance[1] for lists.
[0] http://strictlypositive.org/IdiomLite.pdf http://strictlypositive.org/IdiomLite.pdf
[1] https://hackage.haskell.org/package/base-4.16.1.0/docs/Control-Applicative.html#t:ZipList https://hackage.haskell.org/package/base-4.16.1.0/docs/Contr...
- Iceland_jack 4y agoIt is possible to derive Applicative. The next release of GHC is able to derive Applicative for product types: `deriving Applicative via Generically1 F`. If you have a sum type you can derive Applicative as well, but as you mention there are often multiple options so this requires user assistance: `deriving Applicative via Idiomatically F '[LeftBias Id]`. Here is the library that allows that: https://hackage.haskell.org/package/idiomatic https://hackage.haskell.org/package/idiomatic