Proposal: NF newtype

Edward Z. Yang ezyang at mit.edu
Sat May 2 01:24:09 UTC 2015


I just realized that the thread of this conversation was not fully
finished.

For Show/Read, there are two flex points:

(1) Should `show (mkNF 2)` output "mkNF 2" or "UnsafeNF 2"?
    I have a preference for the former but then we need to handwrite
    the instance.  We could also change the definition of NF to just
    name its constructor NF and export a helper function unsafeMkNF

(2) Should `read "mkNF 2"` execute rnf on the result of the inner
    Read instance?  If the answer is no, we can use the default; if
    the answer is yes, we want an instance like:

        instance (NFData a, Read a) => Read (NF a) where
            readsPrec = parens . prec 10 $ do
                            Ident "makeNF" <- lexP
                            m <- step readPrec
                            return (m `deepseq` UnsafeNF m)

I lean towards a safe by default API, with an unsafeReadNF, but
maybe if default Read instances are not lazy, we should be OK
(e.g.  (read $ show (repeat 1)) :: [Int] hangs; it isn't identity).

Edward

Excerpts from Roman Cheplyaka's message of 2015-04-22 13:28:10 -0700:
> On 22/04/15 21:54, Edward Z. Yang wrote:
> > But it is an interesting question whether or not 'UnsafeNF' should be
> > used, since the value read in is known to be in normal form.
> 
> Is it?
> 
> newtype X = X Int
>   deriving Show
> instance Read X where
>   readsPrec n = map (first $ X . trace "eval") . readsPrec n
> 
> > length (read "[1,2,3]" :: [X])
> 3
> > read "[1,2,3]" :: [X]
> [X eval
> 1,X eval
> 2,X eval
> 3]
> 
> Or did you mean something else?
> 
> > Excerpts from Henning Thielemann's message of 2015-04-22 19:49:56 +0100:
> >>
> >> On Wed, 22 Apr 2015, Dan Burton wrote:
> >>
> >>> A hand-written read makes more sense to me in this case:
> >>> read = makeNF . read
> >>> show = show . getNF
> >>
> >> Show and Read instances should process Strings representing Haskell code, 
> >> and I guess, Haskell code with the same type as the represented value. 
> >> Thus the NF should be part of the formatted value.
> > _______________________________________________
> > Libraries mailing list
> > Libraries at haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> > 


More information about the Libraries mailing list