[Haskell-beginners] I need advice on design
Brent Yorgey
byorgey at seas.upenn.edu
Mon Dec 28 12:43:08 EST 2009
On Mon, Dec 28, 2009 at 05:14:53PM +0000, Amy de Buitléir wrote:
> I'm building a library of components for artificial neural networks.
> I'm used to object-oriented languages, so I'm struggling a bit to
> figure out how to do a good design in a functional programming
> language like Haskell.
>
> Q1: I've come up with two designs, and would appreciate any advice on
> improvements and what approach to take.
>
> ===== Design #1 =====
> class Neuron n where
> activate :: [Double] -> n -> Double
> train :: [Double] -> Double -> n -> n
Looks reasonable.
> The disadvantage of this approach is that I need to define and name
> each instance of neuron before I can use it. I'd rather create a
> neuron on-the-fly by calling a general-purpose constructor and telling
> it what functions to use for activation and training.
Indeed, if you want to create new types of neurons on-the-fly you
should use your second design.
> ===== Design #2 =====
> data Neuron =
> Neuron {
> weights :: [Double],
> activate :: [Double] -> Double,
> train :: [Double] -> Double -> Neuron
> }
Mostly makes sense. Note that this is really making explicit what the
type class mechanism is doing -- a type class instance corresponds to
a "dictionary", a record of type class methods, which is implcitly
passed around. Here you are just explicitly declaring a dictionary
type for neurons.
The one thing that confuses me is why you included "weights" in the
explicit dictionary but not in the Neuron type class. I would think
you'd want it in both or neither, I don't see any reason you'd need it
in one but not the other.
> Q2: I thought there might be some way to define a function type, but
> the following doesn't work. Is there something along these lines that
> would work?
>
> type activationFunction = [Double] -> Double
Type names must start with an uppercase letter.
type ActivationFunction = [Double] -> Double
should work just fine.
-Brent
More information about the Beginners
mailing list