Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using acid-state - safeCopy of a function

Tags:

haskell

acid

Problem when there is a data type :

MyData = One Int | Two (Int -> Int) | Three | Four

the problem is that when i say

$(deriveSafeCopy 0 'base ''MyData)

i got the following error:

No instance for (SafeCopy (Int -> Int) )
   arising from the use of `getSafePut`
...

So I realize that SafeCopy may not be meant for functions...

Am i in trouble? Since i cant change the model of MyData.... Is there someway to do this?

like image 469
Illiax Avatar asked Feb 20 '23 14:02

Illiax


1 Answers

Indeed, since the point of SafeCopy is serialisation, you can't use functions in your data; GHC does not support the serialisation of functions, and it would be problematic to do so for various reasons; functions can close over ephemeral data like operating system handles and the like, so it would be very difficult to serialise and deserialise functions reliably.

You'll have to model it some other way; for instance, if there's only a few possible behaviours you need from the Int -> Int function, you could model it as a data type. For instance, if the only functions you need are \x -> x `div` k (for an arbitrary constant k) and \x -> x + 1, you could write:

data IntToInt
    = DivideBy Int
    | PlusOne

which can be an instance of SafeCopy. But if you rely on unrestricted functions, then I'm afraid you'll have to change your design in a more fundamental manner.

like image 176
ehird Avatar answered Mar 06 '23 02:03

ehird