module Crypto.Lol.PosBinDefs
(
Pos(..), Sing(SO, SS), SPos, PosC
, posToInt, addPos, sAddPos, AddPos, subPos, sSubPos, SubPos
, posType, posDec
, OSym0, SSym0, SSym1, AddPosSym0, AddPosSym1, SubPosSym0, SubPosSym1
, Bin(..), Sing(SB1, SD0, SD1), SBin, BinC
, binToInt, binType, binDec
, B1Sym0, D0Sym0, D0Sym1, D1Sym0, D1Sym1
, intDec, primes, prime
)
where
import Data.Singletons.Prelude
import Data.Singletons.TH
import Language.Haskell.TH
import Algebra.ToInteger as ToInteger
import NumericPrelude
singletons [d|
data Pos = O
| S Pos
deriving (Show, Eq)
instance Ord Pos where
compare O O = EQ
compare O (S _) = LT
compare (S _) O = GT
compare (S a) (S b) = compare a b
addPos :: Pos -> Pos -> Pos
addPos O b = S b
addPos (S a) b = S $ addPos a b
subPos :: Pos -> Pos -> Pos
subPos (S a) O = a
subPos (S a) (S b) = subPos a b
subPos O _ = error "Invalid call to subPos: a <= b"
|]
posToInt :: ToInteger.C z => Pos -> z
posToInt O = one
posToInt (S a) = one + posToInt a
singletons [d|
data Bin = B1
| D0 Bin
| D1 Bin
deriving (Show, Eq)
instance Ord Bin where
compare B1 B1 = EQ
compare B1 (D0 _) = LT
compare B1 (D1 _) = LT
compare (D0 _) B1 = GT
compare (D1 _) B1 = GT
compare (D0 a) (D0 b) = compare a b
compare (D1 a) (D1 b) = compare a b
compare (D0 a) (D1 b) = case compare a b of
EQ -> LT
LT -> LT
GT -> GT
compare (D1 a) (D0 b) = case compare a b of
EQ -> GT
LT -> LT
GT -> GT
|]
binToInt :: ToInteger.C z => Bin -> z
binToInt B1 = one
binToInt (D0 a) = 2 * binToInt a
binToInt (D1 a) = 1 + 2 * binToInt a
type PosC (p :: Pos) = SingI p
type BinC (b :: Bin) = SingI b
posType :: Int -> TypeQ
posType n
| n <= 0 = fail $ "posType: non-positive argument n = " ++ show n
| n == 1 = conT 'O
| otherwise = conT 'S `appT` posType (n1)
binType :: Int -> TypeQ
binType n
| n <= 0 = fail $ "binType: non-positive argument n = " ++ show n
| otherwise = case n `quotRem` 2 of
(0,1) -> conT 'B1
(q,0) -> conT 'D0 `appT` binType q
(q,1) -> conT 'D1 `appT` binType q
_ -> fail "internal error in PosBinTH.bin"
posDec, binDec :: Int -> DecQ
posDec = intDec "P" posType
binDec = intDec "B" binType
intDec :: String
-> (Int -> TypeQ)
-> Int
-> DecQ
intDec pfx f n = tySynD (mkName $ pfx ++ show n) [] (f n)
primes :: [Int]
primes = 2 : 3 : 5 : primes'
where
isPrime (p:ps) n = p*p > n || n `rem` p /= 0 && isPrime ps n
primes' = 7 : filter (isPrime primes') (scanl (+) 11 $ cycle [2,4,2,4,6,2,6,4])
prime :: Int -> Bool
prime = go primes
where go (p:ps) n = case compare p n of
LT -> go ps n
EQ -> True
GT -> False