I am just interested to know how, in a purely functional language, you can connect with an API without introducing side effects?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Purely functional languages, such as Haskell, support calling functions in foreign languages via “foreign function interfaces”.
The question that arises is how to encode the types and behavior of the foreign language function into the purely functional language (e.g Haskell).
There are two cases to consider:
Pure functions
Functions in the foreign language without side effects can be directly embedded without semantic issue. An example is
sin :: CDouble -> CDoublein C.Impure functions
Impure functions have side effects. Often they modify state on the foreign language side. Such functions must be called in a dependency/sequential order, in order to sequence the side effects correctly.
To embed this in a purely functional language you can pass a token to and from the foreign function, representing the state. Each time you call the foreign function, you get back a new token, and the old one is thrown away. So
the side effect is captured as a pure function that modifies (by association) the
statevariable. Passing these back and forth ensures safety.To hide the plumbing of passing the state token around you can use a monad.
This approach is very common for interfacing with stateful foreign APIs from Haskell, for example. A product example: the mersenne-twister binding., which uses the
MTGentoken as evidence that the library has been initialized.