[Haskell] Per-type function namespaces (was: Data.Set whishes)
tpledger at ihug.co.nz
Sat Mar 6 10:40:04 EST 2004
Conor McBride wrote:
>There's a catch, of course. When you write
> r <| e
>you give the typechecker no clue as to the type of r: it just
>has to infer the type of r and hope it's a datatype.
This is reminiscent of an issue I encountered a year or so ago, when
designing a language. The main example I used was a function for
calculating the magnitude of a two dimensional vector. It would be nice
to simplify this
magnitude v = sqrt ((v <| x)*(v <| x) + (v <| y)*(v <| y))
magnitude v = v <| sqrt (x*x + y*y)
but how was the compiler to know that x and y are fields of v, but sqrt
and (+) and (*) aren't?
What I ended up doing was making a rule that if the record expression
(the part to the left of the <|) could not be statically tracked to
something which revealed the field names, then the right hand side (of
the <|) must contain exactly one free variable. This static tracking
took place before type inference, so it was in line with Simon PJ's
preference for keeping scope and type inference separate.
I also used some syntactic sugar for explicit record narrowing, so the
final version of the magnitude function was
magnitude v = v(.x, .y) <| sqrt (x*x + y*y)
which was quite similar to Cayenne's "open ... use ... in ..." feature.
More information about the Haskell