Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Correctness of short cut fusion
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====<hask>foldr</hask>/<hask>build</hask>==== In the presence of <hask>seq</hask>, the <hask>foldr</hask>/<hask>build</hask>-rule is not necessarily a semantic equivalence. The instance <haskell> g = seq c = undefined n = 0 </haskell> shows, via similar "evaluations" as above, that the right-hand side (<hask>g c n</hask>) can be strictly less defined than the left-hand side (<hask>foldr c n (build g)</hask>). The converse cannot happen, because the following always holds: <haskell> foldr c n (build g) β g c n </haskell> Moreover, semantic equivalence can again be recovered by putting restrictions on the involved functions. On the consumption side, if <hask>(c β₯ β₯) β β₯</hask> and <hask>n β β₯</hask>, then even in the presence of <hask>seq</hask>: <haskell> foldr c n (build g) = g c n </haskell> On the production side, <hask>seq</hask> can be used safely as long as it is never used to force anything whose type <hask>build</hask> expects to be polymorphic. In particular, the function passed to build must not force either of its arguments, and must not force anything constructed using them. For example, in <haskell> f x = build (\c n -> x `seq` (x `c` n)) </haskell> The usual equivalence holds, regardless of <hask>c</hask> and <hask>n</hask>: <haskell> fold c n (f x) = x `seq` (x `c` n) </haskell> For a more interesting example, we can define <haskell> hyloList f q c n = case f q of Nothing -> n Just (x,q') -> x `c` hyloList f q' c n unfoldr f q = build (hyloList f q) </haskell> Note that if <hask>f</hask> or <hask>q</hask> uses <hask>seq</hask>, then that will appear in the argument to <hask>build</hask>, but that is still safe because <hask>f</hask> and <hask>q</hask> have no way to get their hands on <hask>c</hask>, <hask>n</hask>, or anything built from them.
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width