{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
module Crypto.Lol.FactoredDefs
(
Factored, SFactored, Fact, fType, fDec
, reifyFact, reifyFactI, intToFact
, PrimePower(..), SPrimePower, Sing(SPP), PPow, ppType, ppDec
, reifyPPow, reifyPPowI
, PrimeBin, SPrimeBin, Prime, pType, pDec
, reifyPrime, reifyPrimeI, valueP
, pToPP, sPToPP, PToPP, ppToF, sPpToF, PpToF, pToF, sPToF, PToF
, unF, sUnF, UnF, unPP, sUnPP, UnPP, primePP, PrimePP, exponentPP, ExponentPP
, fPPMul, FPPMul, fMul, FMul, type (*)
, fDivides, FDivides, Divides, fDiv, FDiv, type (/)
, fGCD, FGCD, fLCM, FLCM, Coprime
, fOddRadical, FOddRadical
, pFree, PFree
, ppsFact, valueFact, totientFact, radicalFact, oddRadicalFact, valueHatFact
, ppPPow, primePPow, exponentPPow, valuePPow, totientPPow, radicalPPow, oddRadicalPPow, valueHatPPow
, valuePrime
, valueF, totientF, radicalF, oddRadicalF, valueHatF
, transDivides, gcdDivides, lcmDivides, lcm2Divides
, pSplitTheorems, pFreeDivides
, (\\)
, valueHat
, PP, ppToPP, valuePP, totientPP, radicalPP, oddRadicalPP, valueHatPP
, module Crypto.Lol.PosBin
) where
import Crypto.Lol.PosBin
import Control.Arrow
import Data.Constraint hiding ((&&&), (***))
import Data.List hiding ((\\))
import Data.Singletons.Prelude hiding (type (*))
import Data.Singletons.TH
import Language.Haskell.TH
import Unsafe.Coerce
singletons [d|
newtype PrimeBin = P Bin deriving (Eq,Ord,Show)
newtype PrimePower = PP (PrimeBin,Pos) deriving (Eq,Show)
newtype Factored = F [PrimePower] deriving (Eq,Show)
unP :: PrimeBin -> Bin
unP (P p) = p
unPP :: PrimePower -> (PrimeBin,Pos)
unPP (PP pp) = pp
unF :: Factored -> [PrimePower]
unF (F pps) = pps
primePP :: PrimePower -> PrimeBin
primePP = fst . unPP
exponentPP :: PrimePower -> Pos
exponentPP = snd . unPP
|]
type F1 = 'F '[]
singletons [d|
fPPMul :: PrimePower -> Factored -> Factored
fMul :: Factored -> Factored -> Factored
fPPMul pp (F pps) = F (ppMul pp pps)
fMul (F pps1) (F pps2) = F (ppsMul pps1 pps2)
ppMul :: PrimePower -> [PrimePower] -> [PrimePower]
ppMul x [] = [x]
ppMul pp'@(PP (p',e')) pps@(pp@(PP (p,e)):pps') =
case compare p' p of
EQ -> PP (p, addPos e e') : pps'
LT -> pp' : pps
GT -> pp : ppMul pp' pps'
ppsMul :: [PrimePower] -> [PrimePower] -> [PrimePower]
ppsMul pps ys = foldl (flip ppMul) ys pps
|]
singletons [d|
pToPP :: PrimeBin -> PrimePower
pToPP p = PP (p, O)
ppToF :: PrimePower -> Factored
ppToF pp = F [pp]
pToF :: PrimeBin -> Factored
pToF = ppToF . pToPP
fGCD, fLCM :: Factored -> Factored -> Factored
fDivides :: Factored -> Factored -> Bool
fDiv :: Factored -> Factored -> Factored
fOddRadical :: Factored -> Factored
fGCD (F pps1) (F pps2) = F (ppsGCD pps1 pps2)
fLCM (F pps1) (F pps2) = F (ppsLCM pps1 pps2)
fDivides (F pps1) (F pps2) = ppsDivides pps1 pps2
fDiv (F pps1) (F pps2) = F (ppsDiv pps1 pps2)
fOddRadical (F pps) = F (ppsOddRad pps)
ppsGCD :: [PrimePower] -> [PrimePower] -> [PrimePower]
ppsGCD [] _ = []
ppsGCD (_:_) [] = []
ppsGCD xs@(PP (p,e) : xs') ys@(PP (p',e') : ys') =
case compare p p' of
EQ -> PP (p, min e e') : ppsGCD xs' ys'
LT -> ppsGCD xs' ys
GT -> ppsGCD xs ys'
ppsLCM :: [PrimePower] -> [PrimePower] -> [PrimePower]
ppsLCM [] ys = ys
ppsLCM xs@(_:_) [] = xs
ppsLCM xs@(pp@(PP (p,e)) : xs') ys@(pp'@(PP (p',e')) : ys') =
case compare p p' of
EQ -> PP (p, max e e') : ppsLCM xs' ys'
LT -> pp : ppsLCM xs' ys
GT -> pp' : ppsLCM xs ys'
ppsDivides :: [PrimePower] -> [PrimePower] -> Bool
ppsDivides [] _ = True
ppsDivides (_:_) [] = False
ppsDivides xs@(PP (p,e) : xs') (PP (p',e') : ys') =
if p == p' then (e <= e') && ppsDivides xs' ys'
else (p > p') && ppsDivides xs ys'
ppsDiv :: [PrimePower] -> [PrimePower] -> [PrimePower]
ppsDiv xs [] = xs
ppsDiv (pp@(PP (p,e)) : xs') ys@(PP (p',e') : ys') =
if p == p' && e' == e then ppsDiv xs' ys'
else if p == p' && e' <= e then PP (p, subPos e e') : ppsDiv xs' ys'
else if p <= p' then pp : ppsDiv xs' ys
else error "invalid call to ppsDiv"
ppsOddRad :: [PrimePower] -> [PrimePower]
ppsOddRad [] = []
ppsOddRad (PP (p, _) : xs') =
if p == P (D0 B1) then ppsOddRad xs'
else PP (p,O) : ppsOddRad xs'
|]
singletons [d|
pFree :: PrimeBin -> Factored -> Factored
pFree p (F pps) = F (go pps)
where go [] = []
go (pp@(PP (p',_)) : ps) =
if p == p' then ps
else pp : go ps
|]
type a / b = FDiv a b
type a * b = FMul a b
type Prime (p :: PrimeBin) = SingI p
type PPow (pp :: PrimePower) = SingI pp
type Fact (m :: Factored) = SingI m
reifyPrime :: Int -> (forall p . SPrimeBin p -> a) -> a
reifyPrime x _ | not $ prime x = error $ "reifyPrime: non-prime x = " ++ show x
reifyPrime x k = reifyBin x (k . SP)
reifyPrimeI :: Int -> (forall p proxy. Prime p => proxy p -> a) -> a
reifyPrimeI n k =
reifyPrime n (\(p::SPrimeBin p) -> withSingI p $ k (Proxy::Proxy p))
reifyPPow :: (Int,Int) -> (forall pp . SPrimePower pp -> a) -> a
reifyPPow (p,e) k = reifyPrime p (\sp -> reifyPos e (k . SPP . STuple2 sp))
reifyPPowI :: (Int,Int) -> (forall pp proxy. PPow pp => proxy pp -> a) -> a
reifyPPowI pp k =
reifyPPow pp (\(p::SPrimePower p) -> withSingI p $ k (Proxy::Proxy p))
reifyFact :: Int -> (forall m . SFactored m -> a) -> a
reifyFact m k = let pps = factorize m in reifyPPs pps $ k . SF
where reifyPPs :: [(Int,Int)]
-> (forall (pps :: [PrimePower]) . Sing pps -> a) -> a
reifyPPs [] c = c SNil
reifyPPs (pp:pps) c =
reifyPPow pp (\spp -> reifyPPs pps (c . SCons spp))
reifyFactI :: Int -> (forall m proxy. Fact m => proxy m -> a) -> a
reifyFactI pps k =
reifyFact pps (\(m::SFactored m) -> withSingI m $ k (Proxy::Proxy m))
type Divides m m' = (Fact m, Fact m', FDivides m m' ~ 'True)
type Coprime m m' = (FGCD m m' ~ F1)
coerceFDivs :: forall m m' . (() :- (FDivides m m' ~ 'True))
coerceFDivs = Sub $ unsafeCoerce (Dict :: Dict ())
coerceGCD :: (() :- (FGCD a a' ~ a''))
coerceGCD = Sub $ unsafeCoerce (Dict :: Dict ())
transDivides :: forall k l m .
((k `Divides` l, l `Divides` m) :- (k `Divides` m))
transDivides = Sub Dict \\ coerceFDivs @k @m
gcdDivides :: forall m1 m2 g .
((Fact m1, Fact m2, g ~ FGCD m1 m2) :-
(g `Divides` m1, g `Divides` m2))
gcdDivides =
Sub $ withSingI (sFGCD (sing :: SFactored m1) (sing :: SFactored m2))
Dict \\ coerceFDivs @g @m1
\\ coerceFDivs @g @m2
lcmDivides :: forall m1 m2 l .
((Fact m1, Fact m2, l ~ FLCM m1 m2) :-
(m1 `Divides` l, m2 `Divides` l))
lcmDivides =
Sub $ withSingI (sFLCM (sing :: SFactored m1) (sing :: SFactored m2))
Dict \\ coerceFDivs @m1 @l
\\ coerceFDivs @m2 @l
lcm2Divides :: forall m1 m2 m l .
((m1 `Divides` m, m2 `Divides` m, l ~ FLCM m1 m2) :-
(m1 `Divides` l, m2 `Divides` l, FLCM m1 m2 `Divides` m))
lcm2Divides =
Sub $ withSingI (sFLCM (sing :: SFactored m1) (sing :: SFactored m2))
Dict \\ coerceFDivs @(FLCM m1 m2) @m \\ lcmDivides @m1 @m2
pSplitTheorems :: forall p m f .
((Prime p, Fact m, f ~ PFree p m) :-
(f `Divides` m, Coprime (PToF p) f))
pSplitTheorems =
Sub $ withSingI (sPFree (sing :: SPrimeBin p) (sing :: SFactored m))
Dict \\ coerceFDivs @f @m
\\ coerceGCD @(PToF p) @f @F1
pFreeDivides :: forall p m m' .
((Prime p, m `Divides` m') :-
(PFree p m `Divides` PFree p m'))
pFreeDivides =
Sub $ withSingI (sPFree (sing :: SPrimeBin p) (sing :: SFactored m)) $
withSingI (sPFree (sing :: SPrimeBin p) (sing :: SFactored m')) $
Dict \\ coerceFDivs @(PFree p m) @(PFree p m')
type PP = (Int, Int)
ppToPP :: PrimePower -> PP
ppToPP = (binToInt . unP *** posToInt) . unPP
ppPPow :: forall pp . PPow pp => PP
ppPPow = ppToPP $ fromSing (sing :: SPrimePower pp)
ppsFact :: forall m . Fact m => [PP]
ppsFact = map ppToPP $ unF $ fromSing (sing :: SFactored m)
valuePrime :: forall p . Prime p => Int
valuePrime = binToInt $ unP $ fromSing (sing :: SPrimeBin p)
valueFact, totientFact, radicalFact, oddRadicalFact, valueHatFact ::
forall m . Fact m => Int
valueFact = valuePPs $ ppsFact @m
totientFact = totientPPs $ ppsFact @m
valueHatFact = valueHat $ valueFact @m
radicalFact = radicalPPs $ ppsFact @m
oddRadicalFact = oddRadicalPPs $ ppsFact @m
valueF, totientF, radicalF, oddRadicalF, valueHatF :: Factored -> Int
valueF = valuePPs . map ppToPP . unF
totientF = totientPPs . map ppToPP . unF
valueHatF = valueHat . valueF
radicalF = radicalPPs . map ppToPP . unF
oddRadicalF = oddRadicalPPs . map ppToPP . unF
primePPow, exponentPPow, valuePPow, totientPPow, radicalPPow, oddRadicalPPow, valueHatPPow ::
forall pp . PPow pp => Int
primePPow = fst $ ppPPow @pp
exponentPPow = snd $ ppPPow @pp
valuePPow = valuePP $ ppPPow @pp
totientPPow = totientPP $ ppPPow @pp
valueHatPPow = valueHat $ valuePPow @pp
radicalPPow = radicalPP $ ppPPow @pp
oddRadicalPPow = oddRadicalPP $ ppPPow @pp
valuePPs, totientPPs, radicalPPs, oddRadicalPPs :: [PP] -> Int
valuePPs = product . map valuePP
totientPPs = product . map totientPP
radicalPPs = product . map radicalPP
oddRadicalPPs = product . map oddRadicalPP
valuePP, totientPP, radicalPP, oddRadicalPP, valueHatPP :: PP -> Int
valuePP (p,e) = p^e
totientPP (_,0) = 1
totientPP (p,e) = (p-1)*(p^(e-1))
valueHatPP = valueHat . valuePP
radicalPP (_,0) = 1
radicalPP (p,_) = p
oddRadicalPP (2,_) = 1
oddRadicalPP (p,_) = p
valueP :: PrimeBin -> Int
valueP (P p) = binToInt p
valueHat :: Integral i => i -> i
valueHat m = if m `mod` 2 == 0 then m `div` 2 else m
pType :: Int -> TypeQ
pType p
| prime p = conT 'P `appT` binType p
| otherwise = fail $ "pType : non-prime p " ++ show p
ppType :: PP -> TypeQ
ppType (p,e) = conT 'PP `appT`
(promotedTupleT 2 `appT` pType p `appT` posType e)
fType :: Int -> TypeQ
fType n = conT 'F `appT` foldr (\pp -> appT (promotedConsT `appT` ppType pp))
promotedNilT (factorize n)
pDec :: Int -> DecQ
pDec p = tySynD (mkName $ "Prime" ++ show p) [] $ pType p
ppDec :: PP -> DecQ
ppDec pp@(p,e) = tySynD (mkName $ "PP" ++ show (p^e)) [] $ ppType pp
fDec :: Int -> DecQ
fDec n = tySynD (mkName $ 'F' : show n) [] $ fType n
intToFact :: Int -> Factored
intToFact m =
let fcts = factorize m
fcts' = map (\(p,e) -> PP (P $ intToBin p, intToPos e)) fcts
in F fcts'
factorize' :: [Int] -> Int -> [Int]
factorize' _ 1 = []
factorize' ds@(d:ds') n
| n > 1 = if d * d > n then [n]
else let (q,r) = n `divMod` d
in if r == 0 then d : factorize' ds q
else factorize' ds' n
factorize' _ n = error $ "can't factorize non-positive n = " ++ show n
factorize :: Int -> [(Int,Int)]
factorize = map (head &&& length) . group . factorize' primes