> I have enclosed below a test file that causes an error that puzzles
> me. Both GHC and Hugs kick it out, so at least they agree; however, I
> must admit that I don't understand it.
Yes, it is a bit confusing, and it took me a few minutes
to see what is going on.
Here's your problem:
> data (Physical indep, Physical dep) => BasicSignal indep dep =
> Pulse {start_time::indep,
> pulse_width::indep,
> amplitude::dep}
>
> pulse:: (Physical a, Physical b) => BasicSignal a b
> pulse = Pulse{start_time = toPhysical 0.0}
>
> example2:: BasicSignal Time Voltage
> example2 = pulse {start_time = (Sec 1.0),
> pulse_width = (Sec 3.0),
> amplitude = (V 2.0) }
The RHS of example2 is (by definition in the Report) equivalent to
example2:: BasicSignal Time Voltage
example2 = case pulse of
Pulse _ _ _ -> Pulse (Sec 1.0) (Sec 3.0) (V 2.0)
The trouble is that "pulse" is overloaded, and the compiler
has no clue which overloading to use. The fact that example2 has
a type signature doesn't help it, because none of the components
of "pulse" make it through to the output. So it's ambiguous.
The solution is to say:
example2 = (pulse :: BasicSignal Time Voltage) {
start_time = Sec 1.0,
pulse_width = Sec 3.0,
amplitude = V 2.0
}
Now the type signature on "pulse" tells it what overloading to use.
You may think it's silly that "pulse" needs to know what overloading
to use, even though the fields concerned (start_time in this case)
are immediately overwritten... but there's nothing to stop "pulse"
returning two quite different records based on operations from the
Physical class, so you're asking the type system tp do something it
just can't.
Incidentally, the context on your data type declaration adds nothing
useful. (We're thinking of nuking them in Std Haskell.)
Simon