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!
===In the presence of <hask>seq</hask>=== This is the more interesting setting, given that in Haskell there is no way to restrict the use of <hask>seq</hask>, so in any given program we must be prepared for the possibility that the <hask>g</hask> appearing in the <hask>foldr</hask>/<hask>build</hask>- or the <hask>destroy</hask>/<hask>unfoldr</hask>-rule is defined using <hask>seq</hask>. Unsurprisingly, it is also the setting in which more can go wrong than above. ====<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. ====<hask>destroy</hask>/<hask>unfoldr</hask>==== Contrary to the situation without <hask>seq</hask>, now also the <hask>destroy</hask>/<hask>unfoldr</hask>-rule may decrease the definedness of a program. This is witnessed by the following instance: <haskell> g = \x y -> seq x 0 p = undefined e = 0 </haskell> Here the left-hand side of the rule (<hask>destroy g (unfoldr p e)</hask>) yields <hask>0</hask>, while the right-hand side (<hask>g p e</hask>) yields <hask>undefined</hask>. Conditions for semantic approximation in either direction can be given as follows. If <hask>p β β₯</hask> and <hask>(p β₯)</hask> β {<hask>β₯</hask>, <hask>Just β₯</hask>}, then: <haskell> destroy g (unfoldr p e) β g p e </haskell> If <hask>p</hask> is strict and total and never returns <hask>Just β₯</hask>, then: <haskell> destroy g (unfoldr p e) β g p e </haskell> Of course, conditions for semantic equivalence can be obtained by combining the two laws above.
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