I have a very simple snippet:
{-# LANGUAGE LinearTypes #-}
module Lib where
data Peer st = Peer { data :: String } deriving Show
data Idle
data Busy
sendToPeer :: Peer Idle %1-> Int -> IO (Peer Busy)
sendToPeer c n = case c of Peer d -> pure $ Peer d
I am on resolver: ghc-9.0.1
.
From the documentation:
A function f is linear if: when its result is consumed exactly once, then its argument is consumed exactly once. Intuitively, it means that in every branch of the definition of f, its argument x must be used exactly once. Which can be done by
- Returning x unmodified
- Passing x to a linear function
- Pattern-matching on x and using each argument exactly once in the same fashion.
- Calling it as a function and using the result exactly once in the same fashion.
and my function sendToPeer
does exactly this - pattern-matches on c
and its argument d
is used once - in Peer d
which is linear:
By default, all fields in algebraic data types are linear (even if -XLinearTypes is not turned on).
but I got an error:
• Couldn't match type ‘'Many’ with ‘'One’
arising from multiplicity of ‘c’
• In an equation for ‘sendToPeer’:
sendToPeer c n = case c of { Peer d -> pure $ Peer d }
|
11 | sendToPeer c n =
| ^
Without pure
:
sendToPeer :: Peer Idle %1-> Int -> Peer Busy
sendToPeer c n = case c of Peer d -> Peer d
but the error is the same.
pure
is not linear? – Starlikepure
and not do returnPeer Busy
, notIO (Peer Busy)
- the error is same – Kirstiekirstin