proposal: add 'unsafeCoerce'

kahl at cas.mcmaster.ca kahl at cas.mcmaster.ca
Tue Nov 21 15:41:01 EST 2006


Simon Marlow <simonmarhaskell at gmail.com> wrote:
 > 
 > >  From Section 4.2.3, from the Haskell report:
 > > 
 > > A declaration of the form
 > > 
 > > newtype cx => T u1 ... uk = N t
 > > 
 > > introduces a new type whose representation is the same as an existing  
 > > type. The type (T u1 ... uk) renames the datatype t. It differs from  a 
 > > type synonym in that it creates a distinct type that must be  explicitly 
 > > coerced to or from the original type. Also, unlike type  synonyms, 
 > > newtype may be used to define recursive types. The  constructor N in an 
 > > expression coerces a value from type t to type (T  u1 ... uk). Using N 
 > > in a pattern coerces a value from type (T u1 ...  uk) to type t. These 
 > > coercions may be implemented without execution  time overhead; newtype 
 > > does not change the underlying representation  of an object.
 > 
 > *blink*
 > 
 > I stand corrected (for the second time today, duh, maybe I should check facts 
 > before trusting my memory next time...).
 > 
 > I have no idea why the report does say that though.  Seems very odd, there's no 
 > need to mention the representation.  Indeed, the language provides no way 
 > (absent unsafeCoerce) for a programmer to determine what the representation is, 
 > so how should we interpret that paragraph?  An invisible requirement or an 
 > implementation hint?

Or as a hint that we should introduce a ``pseudo-polymorphic pseudo-function''

safeCoerce :: a -> b

which can only be used if a and b have the same representation,
as mandated by the language definition?
(Or if the compiler can derive that for the type language
 currently used...)



Wolfram



More information about the Libraries mailing list