I am working with a Haskell libraby that discribes digital circuits (Lava), its function inputs and outputs are of the type signal ( Signal Bool , Signal Int) , as far as i know there is not a function that converts from a Signal Int to Int, I know there are several arithmetic operations that we can use with the type Signal Int, but not all arithmetic operations are possible. I wrote this function that is suppose to convert from Signal Int to Int ( just for the values that I need).
signalInt2int :: Signal Int -> Int
signalInt2int x = case x of
0 -> 0
1 -> 1
15 -> 15
_ -> 1000
Just for trying I only wrote these 4 possibilities, the problem is whenever I call this function no matter what the input is, the output is always 1000. I make sure to use an input of the type Signal Int. Here is what I get.
Can anyone point out where the problem is? I will be grateful for your help.
Due to the way integer literals work in Haskell, your code is making comparisons x == fromInteger 0
, x == fromInteger 1
etc. So it depends on the way ==
and fromInteger
are implemented for Signal Int
.
And if you check sources, it turns out fromInteger
always creates a new Ref
and won't be equal to any existing Signal
s.
So the question remains, how to do what you want. Pattern-matching should work, but instead of comparing with literals you'll need to go inside the actual structure of Signal
. If I got it correct (without testing) something like this should work (returning Maybe Int
because not all Signal Int
signals are constant):
signalInt2int :: Signal Int -> Maybe Int
signalInt2int (Signal (Symbol ref)) = case deref ref of
Int i -> Just i
_ -> None