Let’s assume we have the following data type:
data Cmd = Cmd0 Opcode | Cmd1 Opcode Arg | Cmd2 OPcode Arg Arg
data Opcode = NOP | INC | ADD | MUL deriving (Enum)
data Arg = W32 Int | W16 Int | W8 Int
The idea is to have an Opcode type that produces sequential opcode numbers. Is there any way to specify a constraints for Cmd values, say: Cmd0 is only has NOP opcode, Cmd1 is only INC,
Cmd2 is only ADD or MUL values. I tried to use GATDs, but they operates by types, not values.
Or vise versa is there any way to generate a sequentual opcodes for every value of Cmd without
declaring fromEnum method for every value manually or without using TH ?
You could use separate
OpCodetypes:Now sometimes you might want to treat all the Opcodes as a single type, say to put them in a list. For this purpose you could use a type class for Opcode types and use existential types:
I suspect you wouldn’t be able to do anything with
Opcodehere, since the classOpcodeClhas no methods. You could add useful methods toOpcodeCl, which convert to or fromInts, for example.