enforcing strictness on arbitrary subexpressions

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

enforcing strictness on arbitrary subexpressions

Matthias Fischmann


hei,

I want to force evaluation on an arbitrary expression.  What I have
found is only strict datatypes (the '!' thing), but not a strictness
idiom for arbitrary subexpressions.  (I want this for debugging
mostly, so if there is a better way to unsafePerformIO something in
one piece I would be happy with that, too.)

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
veryLongString = "..."

data StrictThingy = StrictThingy ! String

main :: IO ()
main = do
       hPutStr stdout veryLongString  -- lazy
       veryLongString `seq` hPutStr stdout veryLongString  -- still lazy?
       (StrictThingy veryLongString) `seq` hPutStr stdout veryLongString  -- strict (or at least i hope it is)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Problem with the last line is that it doesn't work for arbitrary
types, and it's kind of ugly.  What I would like to have is something
like:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
hPutStr stdout ! veryLongString
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

but I guess it's not that easy?

Sorry for asking without consulting the list archives, but I promise
I'll add it to the faq in the wiki once i understood.  (-:

I am using ghci and ghc, latest ubuntu package, but I am happy to
switch to new versions.

thanks,
matthias

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: enforcing strictness on arbitrary subexpressions

Udo Stenzel
Matthias Fischmann wrote:
> I want to force evaluation on an arbitrary expression.
> [...]

> main :: IO ()
> main = do
>        hPutStr stdout veryLongString  -- lazy
>        veryLongString `seq` hPutStr stdout veryLongString  -- still lazy?
>        (StrictThingy veryLongString) `seq` hPutStr stdout veryLongString  -- strict (or at least i hope it is)

The last line is actually equivalent to the second.  Strict data
constructors are defined in term of seq, and seq only forces the
evaluation of the outermost constructor.  So after seq'ing a string, it
is either empty or has at least one element, but that element and the
whole tail are still unevaluated.  You have to recurse into the string
to evaluate everything:

> hPutStr stdout $ foldr seq veryLongString veryLongString

There is no primitive to do this for arbitrary data types, but the
DeepSeq type class comes close.  You can find DeepSeq and some more
hints on strict evaluation at
<http://users.aber.ac.uk/afc/stricthaskell.html>.


Udo.
--
Today is the tomorrow you worried about yesterday.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: enforcing strictness on arbitrary subexpressions

Matthias Fischmann


Thanks Udo, that helped a lot.

I added the link to haskell.org.  I couldn't find a proper place to
put it, so I added a page 'By topic' (still quite empty) linked from
the main page.  (I hope that wasn't against any rules I missed; if
anybody objects please feel free to remove it.)

cheers,
matthias



On Thu, Feb 16, 2006 at 01:10:14PM +0100, Udo Stenzel wrote:

> To: Matthias Fischmann <[hidden email]>
> Cc: [hidden email]
> From: Udo Stenzel <[hidden email]>
> Date: Thu, 16 Feb 2006 13:10:14 +0100
> Subject: Re: [Haskell-cafe] enforcing strictness on arbitrary subexpressions
>
> Matthias Fischmann wrote:
> > I want to force evaluation on an arbitrary expression.
> > [...]
>
> > main :: IO ()
> > main = do
> >        hPutStr stdout veryLongString  -- lazy
> >        veryLongString `seq` hPutStr stdout veryLongString  -- still lazy?
> >        (StrictThingy veryLongString) `seq` hPutStr stdout veryLongString  -- strict (or at least i hope it is)
>
> The last line is actually equivalent to the second.  Strict data
> constructors are defined in term of seq, and seq only forces the
> evaluation of the outermost constructor.  So after seq'ing a string, it
> is either empty or has at least one element, but that element and the
> whole tail are still unevaluated.  You have to recurse into the string
> to evaluate everything:
>
> > hPutStr stdout $ foldr seq veryLongString veryLongString
>
> There is no primitive to do this for arbitrary data types, but the
> DeepSeq type class comes close.  You can find DeepSeq and some more
> hints on strict evaluation at
> <http://users.aber.ac.uk/afc/stricthaskell.html>.
>
>
> Udo.
> --
> Today is the tomorrow you worried about yesterday.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

signature.asc (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: enforcing strictness on arbitrary subexpressions

Nils Anders Danielsson
In reply to this post by Udo Stenzel
On Thu, 16 Feb 2006, Udo Stenzel <[hidden email]> wrote:

>> hPutStr stdout $ foldr seq veryLongString veryLongString
>
> There is no primitive to do this for arbitrary data types, but the
> DeepSeq type class comes close.  You can find DeepSeq and some more
> hints on strict evaluation at
> <http://users.aber.ac.uk/afc/stricthaskell.html>.

You can also use Control.Parallel.Strategies:

Prelude Control.Parallel.Strategies> take 2 (repeat 'x')
"xx"
Prelude Control.Parallel.Strategies> take 2 (repeat 'x' `using` rnf)
"

(No more output.)

--
/NAD

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe