Skip to content

Commit b0867ff

Browse files
committed
better pretty printing for prime fields
1 parent fc82039 commit b0867ff

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/Data/Field/Galois/Prime.hs

+9-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import GHC.Natural (naturalToInteger)
1818
import GHC.TypeNats (natVal)
1919
import Protolude as P hiding (Semiring, natVal, rem)
2020
import Test.QuickCheck (Arbitrary (..), choose)
21-
import Text.PrettyPrint.Leijen.Text (Pretty (..))
21+
import Text.PrettyPrint.Leijen.Text (Pretty (..), text)
2222

2323
-------------------------------------------------------------------------------
2424
-- Data types
@@ -103,7 +103,14 @@ instance (KnownNat p) => Integral (Prime p) where
103103

104104
-- Prime fields are pretty.
105105
instance (KnownNat p) => Pretty (Prime p) where
106-
pretty (P x) = pretty $ naturalToInteger $ unMod x
106+
pretty (f :: Prime p) =
107+
let p = toInteger (natVal $ Proxy @p)
108+
cutoff = p `div` 2
109+
x = fromP f
110+
in if x < cutoff
111+
then pretty x
112+
else text "-" <> pretty (abs (x - p))
113+
107114

108115
-- Prime fields are random.
109116
instance (KnownNat p) => Random (Prime p) where

0 commit comments

Comments
 (0)