{-
	Copyright (C) 2011-2015 Dr. Alistair Ward

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
-}
{- |
 [@AUTHOR@]	Dr. Alistair Ward

 [@DESCRIPTION@]	Defines the classes of /Pi/-algorithm which have been implemented.
-}

module Factory.Math.Pi(
-- * Type-classes
	Algorithmic(..),
-- * Types
-- ** Data-types
	Category(..)
) where

import qualified	Data.Default
import qualified	Factory.Math.Precision	as Math.Precision

{- |
	* Defines the methods expected of a /Pi/-algorithm.

	* Most of the implementations naturally return a 'Rational', but the spigot-algorithms naturally produce a @[Int]@;
	though representing /Pi/ as a big integer with the decimal point removed is clearly incorrect.

	* Since representing /Pi/ as either a 'Rational' or promoted to an 'Integer', is inconvenient, an alternative decimal 'String'-representation is provided.
-}
class Algorithmic algorithm where
	openR	:: algorithm -> Math.Precision.DecimalDigits -> Rational	-- ^ Returns the value of /Pi/ as a 'Rational'.

	openI	:: algorithm -> Math.Precision.DecimalDigits -> Integer	-- ^ Returns the value of /Pi/, promoted by the required precision to form an integer.
	openI algorithm
_ DecimalDigits
1	= Integer
3
	openI algorithm
algorithm DecimalDigits
decimalDigits
		| DecimalDigits
decimalDigits DecimalDigits -> DecimalDigits -> Bool
forall a. Ord a => a -> a -> Bool
<= DecimalDigits
0	= [Char] -> Integer
forall a. HasCallStack => [Char] -> a
error ([Char] -> Integer) -> [Char] -> Integer
forall a b. (a -> b) -> a -> b
$ [Char]
"Factory.Math.Pi.openI:\tinsufficient decimalDigits=" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ DecimalDigits -> [Char]
forall a. Show a => a -> [Char]
show DecimalDigits
decimalDigits
		| Bool
otherwise		= Rational -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
round (Rational -> Integer)
-> (DecimalDigits -> Rational) -> DecimalDigits -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> DecimalDigits -> Rational
forall n. Num n => n -> DecimalDigits -> n
Math.Precision.promote (algorithm -> DecimalDigits -> Rational
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Rational
openR algorithm
algorithm DecimalDigits
decimalDigits) (DecimalDigits -> Integer) -> DecimalDigits -> Integer
forall a b. (a -> b) -> a -> b
$ DecimalDigits -> DecimalDigits
forall a. Enum a => a -> a
pred DecimalDigits
decimalDigits

	openS	:: algorithm -> Math.Precision.DecimalDigits -> String	-- ^ Returns the value of /Pi/ as a decimal 'String'.
	openS algorithm
_ DecimalDigits
1	= [Char]
"3"
	openS algorithm
algorithm DecimalDigits
decimalDigits
		| DecimalDigits
decimalDigits DecimalDigits -> DecimalDigits -> Bool
forall a. Ord a => a -> a -> Bool
<= DecimalDigits
0	= [Char]
""
		| DecimalDigits
decimalDigits DecimalDigits -> DecimalDigits -> Bool
forall a. Ord a => a -> a -> Bool
<= DecimalDigits
16	= DecimalDigits -> [Char] -> [Char]
forall a. DecimalDigits -> [a] -> [a]
take (DecimalDigits -> DecimalDigits
forall a. Enum a => a -> a
succ DecimalDigits
decimalDigits) ([Char] -> [Char]) -> [Char] -> [Char]
forall a b. (a -> b) -> a -> b
$ Double -> [Char]
forall a. Show a => a -> [Char]
show (Double
forall a. Floating a => a
pi :: Double)
		| Bool
otherwise		= [Char]
"3." [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char] -> [Char]
forall a. [a] -> [a]
tail (Integer -> [Char]
forall a. Show a => a -> [Char]
show (Integer -> [Char]) -> Integer -> [Char]
forall a b. (a -> b) -> a -> b
$ algorithm -> DecimalDigits -> Integer
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Integer
openI algorithm
algorithm DecimalDigits
decimalDigits)	-- Insert a decimal point.

-- | Categorises the various algorithms.
data Category agm bbp borwein ramanujan spigot
	= AGM agm		-- ^ Algorithms based on the /Arithmetic-geometric Mean/.
	| BBP bbp		-- ^ <https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula>.
	| Borwein borwein	-- ^ <https://en.wikipedia.org/wiki/Borwein%27s_algorithm>.
	| Ramanujan ramanujan	-- ^ <http://www.pi314.net/eng/ramanujan.php>.
	| Spigot spigot		-- ^ Algorithms from which the digits of /Pi/ slowly drip, one by one.
	deriving (Category agm bbp borwein ramanujan spigot
-> Category agm bbp borwein ramanujan spigot -> Bool
(Category agm bbp borwein ramanujan spigot
 -> Category agm bbp borwein ramanujan spigot -> Bool)
-> (Category agm bbp borwein ramanujan spigot
    -> Category agm bbp borwein ramanujan spigot -> Bool)
-> Eq (Category agm bbp borwein ramanujan spigot)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall agm bbp borwein ramanujan spigot.
(Eq agm, Eq bbp, Eq borwein, Eq ramanujan, Eq spigot) =>
Category agm bbp borwein ramanujan spigot
-> Category agm bbp borwein ramanujan spigot -> Bool
/= :: Category agm bbp borwein ramanujan spigot
-> Category agm bbp borwein ramanujan spigot -> Bool
$c/= :: forall agm bbp borwein ramanujan spigot.
(Eq agm, Eq bbp, Eq borwein, Eq ramanujan, Eq spigot) =>
Category agm bbp borwein ramanujan spigot
-> Category agm bbp borwein ramanujan spigot -> Bool
== :: Category agm bbp borwein ramanujan spigot
-> Category agm bbp borwein ramanujan spigot -> Bool
$c== :: forall agm bbp borwein ramanujan spigot.
(Eq agm, Eq bbp, Eq borwein, Eq ramanujan, Eq spigot) =>
Category agm bbp borwein ramanujan spigot
-> Category agm bbp borwein ramanujan spigot -> Bool
Eq, ReadPrec [Category agm bbp borwein ramanujan spigot]
ReadPrec (Category agm bbp borwein ramanujan spigot)
DecimalDigits -> ReadS (Category agm bbp borwein ramanujan spigot)
ReadS [Category agm bbp borwein ramanujan spigot]
(DecimalDigits
 -> ReadS (Category agm bbp borwein ramanujan spigot))
-> ReadS [Category agm bbp borwein ramanujan spigot]
-> ReadPrec (Category agm bbp borwein ramanujan spigot)
-> ReadPrec [Category agm bbp borwein ramanujan spigot]
-> Read (Category agm bbp borwein ramanujan spigot)
forall a.
(DecimalDigits -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
ReadPrec [Category agm bbp borwein ramanujan spigot]
forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
ReadPrec (Category agm bbp borwein ramanujan spigot)
forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
DecimalDigits -> ReadS (Category agm bbp borwein ramanujan spigot)
forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
ReadS [Category agm bbp borwein ramanujan spigot]
readListPrec :: ReadPrec [Category agm bbp borwein ramanujan spigot]
$creadListPrec :: forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
ReadPrec [Category agm bbp borwein ramanujan spigot]
readPrec :: ReadPrec (Category agm bbp borwein ramanujan spigot)
$creadPrec :: forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
ReadPrec (Category agm bbp borwein ramanujan spigot)
readList :: ReadS [Category agm bbp borwein ramanujan spigot]
$creadList :: forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
ReadS [Category agm bbp borwein ramanujan spigot]
readsPrec :: DecimalDigits -> ReadS (Category agm bbp borwein ramanujan spigot)
$creadsPrec :: forall agm bbp borwein ramanujan spigot.
(Read agm, Read bbp, Read borwein, Read ramanujan, Read spigot) =>
DecimalDigits -> ReadS (Category agm bbp borwein ramanujan spigot)
Read, DecimalDigits
-> Category agm bbp borwein ramanujan spigot -> [Char] -> [Char]
[Category agm bbp borwein ramanujan spigot] -> [Char] -> [Char]
Category agm bbp borwein ramanujan spigot -> [Char]
(DecimalDigits
 -> Category agm bbp borwein ramanujan spigot -> [Char] -> [Char])
-> (Category agm bbp borwein ramanujan spigot -> [Char])
-> ([Category agm bbp borwein ramanujan spigot]
    -> [Char] -> [Char])
-> Show (Category agm bbp borwein ramanujan spigot)
forall a.
(DecimalDigits -> a -> [Char] -> [Char])
-> (a -> [Char]) -> ([a] -> [Char] -> [Char]) -> Show a
forall agm bbp borwein ramanujan spigot.
(Show agm, Show bbp, Show borwein, Show ramanujan, Show spigot) =>
DecimalDigits
-> Category agm bbp borwein ramanujan spigot -> [Char] -> [Char]
forall agm bbp borwein ramanujan spigot.
(Show agm, Show bbp, Show borwein, Show ramanujan, Show spigot) =>
[Category agm bbp borwein ramanujan spigot] -> [Char] -> [Char]
forall agm bbp borwein ramanujan spigot.
(Show agm, Show bbp, Show borwein, Show ramanujan, Show spigot) =>
Category agm bbp borwein ramanujan spigot -> [Char]
showList :: [Category agm bbp borwein ramanujan spigot] -> [Char] -> [Char]
$cshowList :: forall agm bbp borwein ramanujan spigot.
(Show agm, Show bbp, Show borwein, Show ramanujan, Show spigot) =>
[Category agm bbp borwein ramanujan spigot] -> [Char] -> [Char]
show :: Category agm bbp borwein ramanujan spigot -> [Char]
$cshow :: forall agm bbp borwein ramanujan spigot.
(Show agm, Show bbp, Show borwein, Show ramanujan, Show spigot) =>
Category agm bbp borwein ramanujan spigot -> [Char]
showsPrec :: DecimalDigits
-> Category agm bbp borwein ramanujan spigot -> [Char] -> [Char]
$cshowsPrec :: forall agm bbp borwein ramanujan spigot.
(Show agm, Show bbp, Show borwein, Show ramanujan, Show spigot) =>
DecimalDigits
-> Category agm bbp borwein ramanujan spigot -> [Char] -> [Char]
Show)

instance Data.Default.Default bbp => Data.Default.Default (Category agm bbp borwein ramanujan spigot)	where
	def :: Category agm bbp borwein ramanujan spigot
def	= bbp -> Category agm bbp borwein ramanujan spigot
forall agm bbp borwein ramanujan spigot.
bbp -> Category agm bbp borwein ramanujan spigot
BBP bbp
forall a. Default a => a
Data.Default.def

instance (
	Algorithmic agm,
	Algorithmic bbp,
	Algorithmic borwein,
	Algorithmic ramanujan,
	Algorithmic spigot
 ) => Algorithmic (Category agm bbp borwein ramanujan spigot)	where
	openR :: Category agm bbp borwein ramanujan spigot
-> DecimalDigits -> Rational
openR Category agm bbp borwein ramanujan spigot
algorithm DecimalDigits
decimalDigits
		| DecimalDigits
decimalDigits DecimalDigits -> DecimalDigits -> Bool
forall a. Ord a => a -> a -> Bool
<= DecimalDigits
0	= [Char] -> Rational
forall a. HasCallStack => [Char] -> a
error ([Char] -> Rational) -> [Char] -> Rational
forall a b. (a -> b) -> a -> b
$ [Char]
"Factory.Math.Pi.openR:\tinsufficient decimalDigits=" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ DecimalDigits -> [Char]
forall a. Show a => a -> [Char]
show DecimalDigits
decimalDigits
		| DecimalDigits
decimalDigits DecimalDigits -> DecimalDigits -> Bool
forall a. Ord a => a -> a -> Bool
<= DecimalDigits
16	= DecimalDigits -> Double -> Rational
forall operand.
RealFrac operand =>
DecimalDigits -> operand -> Rational
Math.Precision.simplify (DecimalDigits -> DecimalDigits
forall a. Enum a => a -> a
pred DecimalDigits
decimalDigits) (Double
forall a. Floating a => a
pi :: Double)
		| Bool
otherwise		= (
			case Category agm bbp borwein ramanujan spigot
algorithm of
				AGM agm
agm			-> agm -> DecimalDigits -> Rational
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Rational
openR agm
agm
				BBP bbp
bbp			-> bbp -> DecimalDigits -> Rational
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Rational
openR bbp
bbp
				Borwein borwein
borwein		-> borwein -> DecimalDigits -> Rational
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Rational
openR borwein
borwein
				Ramanujan ramanujan
ramanujan	-> ramanujan -> DecimalDigits -> Rational
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Rational
openR ramanujan
ramanujan
				Spigot spigot
spigot		-> spigot -> DecimalDigits -> Rational
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Rational
openR spigot
spigot
		) DecimalDigits
decimalDigits

	openI :: Category agm bbp borwein ramanujan spigot
-> DecimalDigits -> Integer
openI Category agm bbp borwein ramanujan spigot
_ DecimalDigits
1				= Integer
3
	openI (Spigot spigot
spigot) DecimalDigits
decimalDigits	= spigot -> DecimalDigits -> Integer
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Integer
openI spigot
spigot DecimalDigits
decimalDigits
	openI Category agm bbp borwein ramanujan spigot
algorithm DecimalDigits
decimalDigits
		| DecimalDigits
decimalDigits DecimalDigits -> DecimalDigits -> Bool
forall a. Ord a => a -> a -> Bool
<= DecimalDigits
0	= [Char] -> Integer
forall a. HasCallStack => [Char] -> a
error ([Char] -> Integer) -> [Char] -> Integer
forall a b. (a -> b) -> a -> b
$ [Char]
"Factory.Math.Pi.openI:\tinsufficient decimalDigits=" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ DecimalDigits -> [Char]
forall a. Show a => a -> [Char]
show DecimalDigits
decimalDigits
		| Bool
otherwise		= Rational -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
round (Rational -> Integer)
-> (DecimalDigits -> Rational) -> DecimalDigits -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rational -> DecimalDigits -> Rational
forall n. Num n => n -> DecimalDigits -> n
Math.Precision.promote (Category agm bbp borwein ramanujan spigot
-> DecimalDigits -> Rational
forall algorithm.
Algorithmic algorithm =>
algorithm -> DecimalDigits -> Rational
openR Category agm bbp borwein ramanujan spigot
algorithm DecimalDigits
decimalDigits) (DecimalDigits -> Integer) -> DecimalDigits -> Integer
forall a b. (a -> b) -> a -> b
$ DecimalDigits -> DecimalDigits
forall a. Enum a => a -> a
pred DecimalDigits
decimalDigits