[Haskell-beginners] Pattern matching vs bit twiddling?

Patrick LeBoutillier patrick.leboutillier at gmail.com
Fri Jul 9 10:15:30 EDT 2010


Hi all,

I recently started writing a simulator for Knuth's MIX computer in
Haskell. My first attempt had the basic data types for the machine
defined like this:

type Byte = Word8
type Address = Word16
type Word = Word32
type FieldSpec = (Int,Int)
data Register = A Word | X Word | I Address | J Address
type Memory = M.Map Address Word

After hitting some kind of dead end with this setup (basically getting
frustrated/lost with all the bit-twiddling), I tried again with these
more abstract types:

data Byte = Byte Word8
data Sign = Plus | Minus
data Address = Address Byte Byte
data SignedAddress = SignedAddress Sign Address
data Word = Word Sign Byte Byte Byte Byte Byte
data FieldSpec = FieldSpec Int Int
data Memory = Memory (M.Map Address Word)

Now I find my code is much clearer (it almost writes itself...) and
practically absent of low-level bit operations (except, for example,
converting a Word into an actual Haskell Int32 to perform the actual
arithmetic). However, I'm wondering about the cost of all the
"packing/unpacking" via pattern-matching and/or accessors. Perhaps I
should not worry about this and optimize later if required? Is this
type of premature optimization (assuming the previous version was
faster...) really the root of all evil?

Note: I'm not far enough into the simulator to be able to actually
test it out and get metrics.


Patrick


Thanks,

Patrick





-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


More information about the Beginners mailing list