-- |
-- Module    : Statistics.Internal
-- Copyright : (c) 2009 Bryan O'Sullivan
-- License   : BSD3
--
-- Maintainer  : bos@serpentine.com
-- Stability   : experimental
-- Portability : portable
--
-- 
module Statistics.Internal (
    -- * Default definitions for Show
    defaultShow1
  , defaultShow2
  , defaultShow3
    -- * Default definitions for Read
  , defaultReadPrecM1
  , defaultReadPrecM2
  , defaultReadPrecM3
    -- * Reexports
  , Show(..)
  , Read(..)
  ) where

import Control.Applicative
import Control.Monad
import Text.Read


----------------------------------------------------------------
-- Default show implementations
----------------------------------------------------------------

defaultShow1 :: (Show a) => String -> a -> Int -> ShowS
defaultShow1 :: String -> a -> Int -> ShowS
defaultShow1 String
con a
a Int
n
  = Bool -> ShowS -> ShowS
showParen (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
11)
  ( String -> ShowS
showString String
con
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
a
  )

defaultShow2 :: (Show a, Show b) => String -> a -> b -> Int -> ShowS
defaultShow2 :: String -> a -> b -> Int -> ShowS
defaultShow2 String
con a
a b
b Int
n
  = Bool -> ShowS -> ShowS
showParen (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
11)
  ( String -> ShowS
showString String
con
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
a
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> b -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 b
b
  )

defaultShow3 :: (Show a, Show b, Show c)
             => String -> a -> b -> c -> Int -> ShowS
defaultShow3 :: String -> a -> b -> c -> Int -> ShowS
defaultShow3 String
con a
a b
b c
c Int
n
  = Bool -> ShowS -> ShowS
showParen (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
11)
  ( String -> ShowS
showString String
con
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
a
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> b -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 b
b
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' '
  ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> c -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 c
c
  )

----------------------------------------------------------------
-- Default read implementations
----------------------------------------------------------------

defaultReadPrecM1 :: (Read a) => String -> (a -> Maybe r) -> ReadPrec r
defaultReadPrecM1 :: String -> (a -> Maybe r) -> ReadPrec r
defaultReadPrecM1 String
con a -> Maybe r
f = ReadPrec r -> ReadPrec r
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec r -> ReadPrec r) -> ReadPrec r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec r -> ReadPrec r
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec r -> ReadPrec r) -> ReadPrec r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ do
  String -> ReadPrec ()
expect String
con
  a
a <- ReadPrec a
forall a. Read a => ReadPrec a
readPrec
  ReadPrec r -> (r -> ReadPrec r) -> Maybe r -> ReadPrec r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ReadPrec r
forall (f :: * -> *) a. Alternative f => f a
empty r -> ReadPrec r
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe r -> ReadPrec r) -> Maybe r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ a -> Maybe r
f a
a

defaultReadPrecM2 :: (Read a, Read b) => String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 :: String -> (a -> b -> Maybe r) -> ReadPrec r
defaultReadPrecM2 String
con a -> b -> Maybe r
f = ReadPrec r -> ReadPrec r
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec r -> ReadPrec r) -> ReadPrec r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec r -> ReadPrec r
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec r -> ReadPrec r) -> ReadPrec r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ do
  String -> ReadPrec ()
expect String
con
  a
a <- ReadPrec a
forall a. Read a => ReadPrec a
readPrec
  b
b <- ReadPrec b
forall a. Read a => ReadPrec a
readPrec
  ReadPrec r -> (r -> ReadPrec r) -> Maybe r -> ReadPrec r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ReadPrec r
forall (f :: * -> *) a. Alternative f => f a
empty r -> ReadPrec r
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe r -> ReadPrec r) -> Maybe r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ a -> b -> Maybe r
f a
a b
b

defaultReadPrecM3 :: (Read a, Read b, Read c)
                 => String -> (a -> b -> c -> Maybe r) -> ReadPrec r
defaultReadPrecM3 :: String -> (a -> b -> c -> Maybe r) -> ReadPrec r
defaultReadPrecM3 String
con a -> b -> c -> Maybe r
f = ReadPrec r -> ReadPrec r
forall a. ReadPrec a -> ReadPrec a
parens (ReadPrec r -> ReadPrec r) -> ReadPrec r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ Int -> ReadPrec r -> ReadPrec r
forall a. Int -> ReadPrec a -> ReadPrec a
prec Int
10 (ReadPrec r -> ReadPrec r) -> ReadPrec r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ do
  String -> ReadPrec ()
expect String
con
  a
a <- ReadPrec a
forall a. Read a => ReadPrec a
readPrec
  b
b <- ReadPrec b
forall a. Read a => ReadPrec a
readPrec
  c
c <- ReadPrec c
forall a. Read a => ReadPrec a
readPrec
  ReadPrec r -> (r -> ReadPrec r) -> Maybe r -> ReadPrec r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe ReadPrec r
forall (f :: * -> *) a. Alternative f => f a
empty r -> ReadPrec r
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe r -> ReadPrec r) -> Maybe r -> ReadPrec r
forall a b. (a -> b) -> a -> b
$ a -> b -> c -> Maybe r
f a
a b
b c
c

expect :: String -> ReadPrec ()
expect :: String -> ReadPrec ()
expect String
str = do
  Ident String
s <- ReadPrec Lexeme
lexP
  Bool -> ReadPrec ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (String
s String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
str)