From d83f9a85fbb4b69f9139ba4ae7c9c87730127756 Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Sun, 16 Sep 2012 15:26:35 -0400 Subject: [PATCH 1/3] names is now subMap --- src/Diagrams/TwoD/Model.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Diagrams/TwoD/Model.hs b/src/Diagrams/TwoD/Model.hs index 07081a35..f97c1b3c 100644 --- a/src/Diagrams/TwoD/Model.hs +++ b/src/Diagrams/TwoD/Model.hs @@ -89,4 +89,4 @@ showLabels d = ) <> fmap (const (Any False)) d where - SubMap m = names d + SubMap m = subMap d From bfc5bd3de6e401cc615f2dec82091fabd0f2ed2c Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Sun, 16 Sep 2012 15:32:09 -0400 Subject: [PATCH 2/3] Add a more useful custom Show instance for R2 The old derived Show instance resulted in a lot of cruft being printed out. This new instance uses &, so it should still have the property that it produces valid Haskell code which evalutes to the value that was shown. --- src/Diagrams/TwoD/Types.hs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Diagrams/TwoD/Types.hs b/src/Diagrams/TwoD/Types.hs index 21e480d6..1eca91c6 100644 --- a/src/Diagrams/TwoD/Types.hs +++ b/src/Diagrams/TwoD/Types.hs @@ -68,7 +68,14 @@ import Data.Typeable -- > foo (coords -> x :& y) = ... newtype R2 = R2 { unR2 :: (Double, Double) } - deriving (AdditiveGroup, Eq, Ord, Show, Read, Typeable, Num, Fractional) + deriving (AdditiveGroup, Eq, Ord, Read, Typeable, Num, Fractional) + +instance Show R2 where + showsPrec p (R2 (x,y)) = showParen (p >= 7) $ + showCoord x . showString " & " . showCoord y + where + showCoord x | x < 0 = showParen True (shows x) + | otherwise = shows x instance Newtype R2 (Double, Double) where pack = R2 From 8c0042bb92ec3f5859383d11c1c3dbbe79885dcf Mon Sep 17 00:00:00 2001 From: Brent Yorgey Date: Tue, 18 Sep 2012 23:43:16 -0400 Subject: [PATCH 3/3] Custom Read instance for R2 to match custom Show instance --- src/Diagrams/TwoD/Types.hs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Diagrams/TwoD/Types.hs b/src/Diagrams/TwoD/Types.hs index 1eca91c6..b27cf1bb 100644 --- a/src/Diagrams/TwoD/Types.hs +++ b/src/Diagrams/TwoD/Types.hs @@ -68,7 +68,7 @@ import Data.Typeable -- > foo (coords -> x :& y) = ... newtype R2 = R2 { unR2 :: (Double, Double) } - deriving (AdditiveGroup, Eq, Ord, Read, Typeable, Num, Fractional) + deriving (AdditiveGroup, Eq, Ord, Typeable, Num, Fractional) instance Show R2 where showsPrec p (R2 (x,y)) = showParen (p >= 7) $ @@ -77,6 +77,18 @@ instance Show R2 where showCoord x | x < 0 = showParen True (shows x) | otherwise = shows x +instance Read R2 where + readsPrec d r = readParen (d > app_prec) + (\r -> [ (R2 (x,y), r''') + | (x,r') <- readsPrec (amp_prec + 1) r + , ("&",r'') <- lex r' + , (y,r''') <- readsPrec (amp_prec + 1) r'' + ]) + r + where + app_prec = 10 + amp_prec = 7 + instance Newtype R2 (Double, Double) where pack = R2 unpack = unR2