[Haskell-cafe] Re: Current thinking on CompositionAsDot issue in haskell prime?

wren ng thornton wren at freegeek.org
Fri Oct 29 19:54:24 EDT 2010


On 10/29/10 11:18 AM, Daniel Peebles wrote:
> Speaking of MagicHash, is it really necessary to take an operator with
> "potential" like (#) just to keep primitive symbols separate from the rest?
> At least from my 2010 Haskell learner perspective, it seems odd to create a
> whole language extension/lexical change just for that purpose.

I'm sort of torn on this issue. On the one hand (#) has great potential 
as an operator, on the other hand I've found that having something like 
-XMagicHash (or TeX's \makeatletter and \makeatother) can be really 
helpful when you want to expose some guts but also want to keep folks 
from using them accidentally.

For example, I use a lot of newtypes to enforce various well-formedness 
constraints (a la weak-sigma types in dependently typed languages). 
Thus, I'll often define things like

     netwype Foo = Foo# Bar

and then have a smart constructor (toFoo :: Bar -> Maybe Foo) which 
verifies whatever invariant.

Sometimes we may be forced to export Foo# in case other modules need to 
look inside in order to create new class instances or the like, but we 
don't want people using it in general since that'd violate the semantics 
of the type Foo. And Foo# is much nicer to type than UnsafeFoo[1]. Since 
it uses the MagicHash syntax, we can ensure that noone will use it 
blithely since they must first enable an extension to eliminate the 
syntax error.

So you can make some nice lemonade from those lemons, but I'm not sure 
whether the lemonade is really worth eliminating every other citrus 
fruit (most of which are quite tasty).


[1] In addition to toFoo, I'll often export a smart constructor 
(unsafeToFoo :: Bar -> Foo) which will still test the invariants but 
throws an error instead of returning Nothing. The primary use of 
unsafeToFoo is to get consistent error messages when someone's going to 
be throwing the Maybe away anyways. Conversely, allowing them to use 
Foo# gives the same type as unsafeToFoo, but allows circumventing the 
invariant checking as a performance optimization.

-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list