Closed allegroCoder closed 8 years ago
So, the first step is to try to get only one bit in Haskell land. Can you do that? Try to implement the following function by FFI-calling get_bit
:
getOpcodeBit :: Int -> Int -> IO Int
Then try to change it so that it returns IO (Bit Bool)
instead, after doing the conversion from integer representation used in C++ to our representation in https://github.com/tuura/scenco/blob/master/Code.hs.
For what regards getOpcodeBit
(it works):
getOpcodeBit :: Int -> Int -> IO Int
getOpcodeBit partialOrder bitPosition = do
get_bit partialOrder bitPosition
I cannot compile the second function though:
getOpcodeBit :: Int -> Int -> IO (Bit Bool)
getOpcodeBit partialOrder bitPosition = do
bitC <- get_bit partialOrder bitPosition
convert bitC
convert :: Int -> IO (Bit Bool)
convert x | x == 0 = known False
| x == 1 = known True
| x == 2 = Nothing
What's the error message?
Encode.hs:97:23:
Couldn't match type ‘Maybe Code.BoolWithUnknowns’
with ‘IO (Bit Bool)’
Expected type: IO (Bit Bool)
Actual type: Bit Code.BoolWithUnknowns
In the expression: known False
In an equation for ‘convert’:
convert x
| x == 0 = known False
| x == 1 = known True
| x == 2 = unused
Encode.hs:98:23:
Couldn't match type ‘Maybe Code.BoolWithUnknowns’
with ‘IO (Bit Bool)’
Expected type: IO (Bit Bool)
Actual type: Bit Code.BoolWithUnknowns
In the expression: known True
In an equation for ‘convert’:
convert x
| x == 0 = known False
| x == 1 = known True
| x == 2 = unused
Encode.hs:99:23:
Couldn't match type ‘Maybe Code.BoolWithUnknowns’
with ‘IO (Bit Bool)’
Expected type: IO (Bit Bool)
Actual type: Bit Code.BoolWithUnknowns
In the expression: unused
In an equation for ‘convert’:
convert x
| x == 0 = known False
| x == 1 = known True
| x == 2 = unused
You forgot return
? That is: return $ convert bitC
.
And the convert
function should be pure. No need to use IO
.
I assume to have all the opcodes available inside a matrix, in the c++ side of the memory. I have a function:
get_bit(int row, int col)
, which returns the particular bit of the opcode in the form of an integer.I am not quite sure what would be the best way to get all of the opcodes in the Haskell side of the code. My plan is to have a function, something like:
getOpcodes
is supposed to take in input the number of partial orders and the bit length; and return the whole list of list of opcodes. @snowleopard could you help me writing this function. I have read some of the material I found online, and watched again some of the lectures in edx, but still cannot find a solution