0
votes

I am new to Haskell and I am using the llvm-general-ure-3.5.1.0 library:

https://hackage.haskell.org/package/llvm-general-pure-3.5.1.0/docs/

There is a basic block data type, Named and name data type:

data BasicBlock = BasicBlock Name [Named Instruction] (Named Terminator)
  deriving (Eq, Read, Show, Typeable, Data)

data Named a 
  = Name := a
  | Do a

data Name 
    = Name String -- ^ a string name 
    | UnName Word -- ^ a number for a nameless thing

The problem I am having is pattern matching against Named Instruction.

My code looks like this:

executeInstruction :: Named Instruction -> Memory -> Memory
executeInstruction inst mem = 
  case inst of
    Add nsw nuw op0 op1 meta -> undefined

This gives me an error message that I expect, my types don't match of Named Instruction vs Instruction.

So I want to strip off the Named and have just an Instruction left.

stripN :: Named Instruction -> Instruction
stripN (Name n inst) = inst

This gives this error:

Constructor `Name' should have 1 argument, but has been given 2 In the pattern: Name n inst

I understand the error. But I don't see how to get just an Instruction from a Named Instruction.

Thanks,

1

1 Answers

2
votes

Try something like this:

executeInstruction :: Named Instruction -> Memory -> Memory
executeInstruction (name := inst) mem = 
     undefined
executeInstruction (Do inst) mem = 
     undefined

Above, inst :: Instruction, so you can also pattern match on that.

If you plan to do the exact same thing in both branches, you might want to define a projection, first:

getInst :: Named Instruction -> Instruction
getInst (_ := i) = i
getInst (Do i)   = i

executeInstruction :: Named Instruction -> Memory -> Memory
executeInstruction namedInst mem = case getInst namedInst of
   Add nsw nuw op0 op1 meta -> undefined
   ...