tuura / scenco-core

Collection of encoding algorithms for conditional graphs
Other
0 stars 1 forks source link

Getting opcodes from c++ function #7

Closed allegroCoder closed 8 years ago

allegroCoder commented 8 years ago

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 :: Int -> Int -> IO [[Int]]
getOpcodes nPO bitLength = undefined

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

snowleopard commented 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.

allegroCoder commented 8 years ago

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
snowleopard commented 8 years ago

What's the error message?

allegroCoder commented 8 years ago
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
snowleopard commented 8 years ago

You forgot return? That is: return $ convert bitC.

snowleopard commented 8 years ago

And the convert function should be pure. No need to use IO.