Multiplicity polymorphic Consumable
Nobody has claimed this yet.
- Dominant language
- Haskell
- Stars
- 359
- Forks
- 45
- PR merge metrics
- No merged PRs in 30d
Description
We have a some typeclasses which is only useful with linear types, namely Consumable, Dupable or Movable. Hence, when writing multiplicity polymorphic functions, we should only require those constraints when the values are used linearly; otherwise non-linear use suffers from unnecessary constraints.
Describe the solution you'd like
I suggest we add wrapper typeclasses with an extra multiplicity parameter; implement a catch-all instance when multiplicity is Many, and refer to the original typeclass when multiplicity is One. Example:
class Consumable' r a where
consume' :: FUN r a ()
instance Consumable' Many a where
consume' _ = ()
instance Consumable a => Consumable' One a where
consume' = consume
lseq' :: Consumable' r a => FUN r a (FUN r b b)
lseq' a b = seqUnit (consume' a) b
Using these, we can easily implement multiplicity polymorphic functions:
lefts :: Consumable' r b => FUN r [Either a b] [a]
lefts [] = []
lefts (Left a : xs) = a : lefts xs
lefts (Right b : xs) = lseq' b (lefts xs)
Which means that I can write multiplicity-polymorphic functions where they only require a Consumable constraint when used with One.
Describe alternatives you've considered
- Instead of adding a new set of classes, we can simply add the additional parameter to existing classes. Then we can also provide a type alias replicating the current kinds. I don't have a preference between this and the solution above.
- We can bikeshed the names.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the existing Consumable, Dupable, and Movable typeclass definitions and the multiplicity-polymorphic functions that use them. Compare the proposed wrapper-typeclass approach with adding a multiplicity parameter to the existing classes. Done means the project has a decided API and the relevant constraints support non-linear and linear uses as described.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- haskell
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100