Linear types are not supported in FFI [GHC57396]
If the extension LinearTypes
is enabled, then programmers can write expressions which have the linear
function type a %1 > b
for functions which use their argument exactly once.
When calling external functions via GHC’s FFI functionality, however, then it is not allowed to use a linear function type.
More information about this specific restriction can be found in the discussion in the GHC issue tracker.
Examples
Linear function type was used in foreign function call
In this example the programmer tries to use a C function with the help of GHC’s FFI system. The type given to the imported function, however, is not allowed. Only nonlinear function types can be given to foreign functions.
messages/GHC57396/linearffi/before/LinearFFI.hs:6:1: error: [GHC57396]
• Unacceptable argument type in foreign declaration:
Linear types are not supported in FFI declarations, see #18472
• When checking declaration:
foreign import ccall safe "exp" c_exp :: Double %1 > Double

6  foreign import ccall "exp" c_exp :: Double %1 > Double
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LinearFFI.hs
Before
{# LANGUAGE LinearTypes #}
module LinearFFI where
import Foreign.Ptr
foreign import ccall "exp" c_exp :: Double %1 > Double
After
{# LANGUAGE LinearTypes #}
module LinearFFI where
import Foreign.Ptr
foreign import ccall "exp" c_exp :: Double > Double