
The comments in HughesPJ say that empty is an identity for <> and $$, but the implementation doesn't satisfy either of these laws. Here are the implementations:
p $$ q = Above p False q
p <> q = Beside p False q
and definitions for empty and isEmpty:
empty = Empty
isEmpty Empty = True isEmpty _ = False
It's an easy fix, just adding these two lines before the current definitions of $$:
Empty $$ q = q p $$ Empty = p
And these before the current definition of <>:
Empty <> q = q p <> Empty = p
- Conal

Conal Elliott wrote:
The comments in HughesPJ say that empty is an identity for <> and $$, but the implementation doesn't satisfy either of these laws. Here are the implementations:
I think, the "observable" results of <> and $$ satisfy these laws. Can you construct an example were these laws can be observed to be not fulfilled (for a reduced doc)?
p $$ q = Above p False q
p <> q = Beside p False q
Above and Beside are not observable and (I think) your proposed fix is not necessary. Christian

Can you construct an example [...]
Certainly. Here's the example that caught my attention (in my GHC 6.5 STABLE): Prelude Text.PrettyPrint> isEmpty empty True Prelude Text.PrettyPrint> isEmpty (empty<>empty) False Similarly, Prelude Text.PrettyPrint> isEmpty (empty$$empty) False Cheers, - Conal -----Original Message----- From: Christian Maeder [mailto:maeder@tzi.de] Sent: Thursday, November 24, 2005 1:12 AM To: Conal Elliott Cc: libraries@haskell.org Subject: Re: lawbreakers in Text.PrettyPrint.HughesPJ Conal Elliott wrote:
The comments in HughesPJ say that empty is an identity for <> and $$, but the implementation doesn't satisfy either of these laws. Here are the implementations:
I think, the "observable" results of <> and $$ satisfy these laws. Can you construct an example were these laws can be observed to be not fulfilled (for a reduced doc)?
p $$ q = Above p False q
p <> q = Beside p False q
Above and Beside are not observable and (I think) your proposed fix is not necessary. Christian

Or the combinators (<>, $$, etc) might take care to build their results in reduced form, which seems to be the strategy that "nest" takes. BTW, there's an invariant comment (about Doc, I think) that says "An empty document is always represented by @Empty@." If the combinators aren't going to ensure that's true, then probably the comment should be removed or fixed. - Conal -----Original Message----- From: Christian Maeder [mailto:maeder@tzi.de] Sent: Thursday, November 24, 2005 8:56 AM To: Conal Elliott Cc: libraries@haskell.org Subject: Re: lawbreakers in Text.PrettyPrint.HughesPJ Conal Elliott wrote:
Can you construct an example [...]
Prelude Text.PrettyPrint> isEmpty (empty<>empty) False
Indeed, isEmpty seems to be the problem that needs a fix: isEmpty d = case reduceDoc d of Empty -> True _ -> False Christian

Conal Elliott wrote:
Or the combinators (<>, $$, etc) might take care to build their results in reduced form, which seems to be the strategy that "nest" takes.
maybe that is possible, too (as now an extra call of reduceDoc for isEmpty is done). Fortunately isEmpty is rarely used, and if it is used the original input is usually thrown away.
BTW, there's an invariant comment (about Doc, I think) that says "An empty document is always represented by @Empty@." If the combinators aren't going to ensure that's true, then probably the comment should be removed or fixed.
The comment only seems to apply to RDoc that don't have Above or Beside (at least on the top-level). Christian
participants (2)
-
Christian Maeder
-
Conal Elliott