Skip to content

Commit

Permalink
Use text, fmt, and megaparsec libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveBarton committed Jun 13, 2024
1 parent ba4bf76 commit 251dfb8
Show file tree
Hide file tree
Showing 20 changed files with 648 additions and 535 deletions.
26 changes: 14 additions & 12 deletions bp-demo/BPDemo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Math.Algebra.Commutative.BinPoly

-- import Control.Monad (when)
import Data.Foldable (toList)
import qualified Data.Text as T
import Data.Word (Word64)
import Fmt ((+|), (|+), (+||), (||+), build, fmtLn)

import Control.Concurrent (runInUnboundThread)

Expand All @@ -17,30 +19,29 @@ import Control.Concurrent (runInUnboundThread)

demoOps :: Int -> StdEvCmp ->
(GBPolyOps EV58 (BinPoly EV58), BPOtherOps EV58 Word64)
demoOps nVars sec = bp58Ops evCmp isGraded descVarSs (UseSugar False)
demoOps nVars sec = bp58Ops evCmp isGraded descVarTs (UseSugar False)
where
evCmp = evCmp58 sec
isGraded = secIsGraded sec
xVarSs = ['X' : show n | n <- [1 :: Int ..]]
descVarSs = take nVars (map (: []) ['a' .. 'z'] ++ xVarSs)
xVarTs = ["X" <> showT n | n <- [1 :: Int ..]]
descVarTs = take nVars (map T.singleton ['a' .. 'z'] ++ xVarTs)

bpDemo :: Int -> IO ()
bpDemo gbTrace = do
putStrLn $ name ++ " " ++ show sec
-- when (length reducedGBGensSeq < 250) $ mapM_ (putStrLn . pShow) reducedGBGensL
putStrLn $ show (bpCountZeros bpoA reducedGBGensL) ++ " receiver zeros"
putStrLn $ show (bpCountZeros bpoA toReduce) ++ " sender zeros"
putStrLn $ show (length revSend) ++ " of " ++ show (length toReduce) ++
" generators to send:"
putStrLn $ showGens pShow (reverse revSend)
fmtLn $ ""+|name|+" "+||sec||+""
-- when (length reducedGBGensSeq < 250) $ mapM_ (fmtLn . build . pShowPrec) reducedGBGensL
fmtLn $ ""+|bpCountZeros bpoA reducedGBGensL|+" receiver zeros"
fmtLn $ ""+|bpCountZeros bpoA toReduce|+" sender zeros"
fmtLn $ ""+|length revSend|+" of "+|length toReduce|+" generators to send:"
fmtLn $ build $ gensShowPrec pShowPrec (reverse revSend)
where
-- To run a demo, first set the "@@" lines below the way you want.

sec = LexCmp -- @@ LexCmp, GrLexCmp, or GrRevLexCmp
(gbpA@(GBPolyOps { pR, pShow }), bpoA@(BPOtherOps { pRead })) = demoOps nVars sec
(gbpA@(GBPolyOps { pR, pShowPrec }), bpoA@(BPOtherOps { pParse })) = demoOps nVars sec
smA@(SubmoduleOps { .. }) = gbiSmOps gbpA

initGensL = map (map pRead) initGenSsL
initGensL = map (map (parseAllOrErr pParse)) initGenSsL
gbIdeal = fromGens smA gbTrace (initGensL !! 1)
reducedGBGensSeq = stdGens (IsDeep True) gbIdeal
reducedGBGensL = toList reducedGBGensSeq
Expand All @@ -53,6 +54,7 @@ bpDemo gbTrace = do

-- @@ choose a name, nVars, and gens, commenting out the other examples:
-- 'a' is the most main variable
name :: Text
{-
name = "logic3"
nVars = 10
Expand Down
17 changes: 9 additions & 8 deletions calculi.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ bug-reports: https://github.com/DaveBarton/calculi/issues
license: BSD-3-Clause
license-file: LICENSE
author: Dave Barton
maintainer: [email protected]
maintainer: Dave Barton
copyright: Copyright (c) David R. Barton, 2022-2024

category: Math
category: Math, Algebra
extra-doc-files: CHANGELOG.md dirs.txt timings/timings.txt
tested-with: ghc == 9.4.4 || == 9.6.1 || == 9.8.1 || == 9.10.1

Expand All @@ -26,7 +26,8 @@ source-repository head
common deps
default-language: GHC2021
default-extensions: DuplicateRecordFields LambdaCase MonoLocalBinds NegativeLiterals
NoFieldSelectors OverloadedRecordDot PatternSynonyms RecordWildCards
NoFieldSelectors OverloadedRecordDot OverloadedStrings PatternSynonyms
RecordWildCards
other-extensions: CPP DataKinds FunctionalDependencies Strict ViewPatterns
ghc-options: -Wall -Wcompat -feager-blackholing -threaded
if impl(ghc >= 9.8.1)
Expand All @@ -35,7 +36,11 @@ common deps
base >= 4.17 && < 5.0,
containers >= 0.6 && < 0.8,
extra >= 1.6 && < 1.8,
strict >= 0.4 && < 0.6
fmt >= 0.4 && < 0.7,
megaparsec < 9.7,
parser-combinators < 1.4,
strict >= 0.4 && < 0.6,
text < 2.2

library
import: deps
Expand All @@ -60,7 +65,6 @@ library
build-depends:
async < 2.3,
contiguous >= 0.6.1.1 && < 0.7,
-- fmt >= 0.4 && < 0.7,
ghc-trace-events < 0.2,
-- hashable, unordered-containers,
-- inspection-testing >= 0.3 && < 0.6,
Expand All @@ -71,7 +75,6 @@ library
rrb-vector >= 0.2.1 && < 0.3,
stm >= 2.4 && < 2.6,
strict-list < 0.2,
-- text,
time >= 1.9.3 && < 2,
timestats < 0.3,
vector < 0.14
Expand Down Expand Up @@ -106,8 +109,6 @@ test-suite calculi-test
Math.Algebra.Commutative.TestEPoly
Math.Algebra.Commutative.TestBinPoly

other-extensions: OverloadedStrings

build-depends: calculi, hedgehog == 1.*, mod, rrb-vector, strict-list, tasty < 1.6,
tasty-hedgehog < 1.5

Expand Down
10 changes: 5 additions & 5 deletions src/Control/Parallel/Cooperative.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ import System.IO.Unsafe (unsafePerformIO)

import Data.Time.Clock.System (SystemTime(MkSystemTime), getSystemTime)
import qualified Debug.TimeStats as TS
-- import Numeric (showFFloat)
-- import Numeric (showFFloatAlt)


-- * Utilities
Expand Down Expand Up @@ -191,9 +191,9 @@ evalPar es = if null (drop 1 es) then seqElts es else
let taskF _numCaps _capIndex = do
me <- popTVar todo
{- now <- getSystemTimeNS
putStrLn $ "evalPar - capIndex " ++ show _capIndex ++ ", "
++ showFFloat (Just 3) (1e-9 * fromInteger (now - start) :: Double) "s: "
++ show (isJust me) -}
putStrLn $ "evalPar - capIndex " <> show _capIndex <> ", "
<> showFFloatAlt (Just 3) (1e-9 * fromInteger (now - start) :: Double) "s: "
<> show (isJust me) -}
pure (maybe False (`seq` True) me)
parNonBlocking wakeAllThreads numSleepingVar taskF
pure es
Expand All @@ -204,7 +204,7 @@ forkJoinPar :: (a -> [b]) -> ([c] -> d) -> (b -> c) -> a -> d
forkJoinPar split join f a = join $ evalPar (map f (split a))

checkChunkSize :: HasCallStack => Int -> a -> a
checkChunkSize d = if d < 1 then error ("Bad chunksize: " ++ show d) else id
checkChunkSize d = if d < 1 then error ("Bad chunksize: " <> show d) else id

mapParChunk :: HasCallStack => Int -> (a -> b) -> [a] -> [b]
{- ^ Map a function down a list, processing chunks in parallel. The chunk size must be
Expand Down
22 changes: 10 additions & 12 deletions src/Math/Algebra/Commutative/BinPoly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ data BPOtherOps ev vals = BPOtherOps {
(∧) :: Op2 (BinPoly ev), -- ^ \"and\", same as multiplication
(∨) :: Op2 (BinPoly ev), -- ^ \"or\", @x ∨ y = x + y + xy = (x+1)(y+1) + 1@
pAt :: BinPoly ev -> vals -> Bool,
pShowPrec :: ShowPrec (BinPoly ev),
pRead :: String -> BinPoly ev
pParse :: Parser (BinPoly ev)
}

bpSortCancel :: Cmp ev -> SL.List ev -> BinPoly ev
Expand All @@ -124,14 +123,14 @@ bpSortCancel evCmp evs = cancelRev (sortBy evCmp (SL.toListReversed evs)) SL.Ni
cancelRev [v] r = v :! r
cancelRev [] r = r

bp58Ops :: Cmp EV58 -> IsGraded -> [String] -> UseSugar ->
bp58Ops :: Cmp EV58 -> IsGraded -> [Text] -> UseSugar ->
(GBPolyOps EV58 (BinPoly EV58), BPOtherOps EV58 Word64)
-- ^ In @bp58Ops evCmp isGraded descVarSs useSugar@, @descVarSs@ lists more main variables
-- first.
bp58Ops evCmp isGraded descVarSs useSugar = assert (nVars <= 58)
-- ^ In @bp58Ops evCmp isGraded descVarTs useSugar@, @descVarTs@ lists more main variables
-- first, and each variable name must be a legal identifier name.
bp58Ops evCmp isGraded descVarTs useSugar = assert (nVars <= 58)
(GBPolyOps { monicizeU = id, .. }, BPOtherOps { .. })
where
nVars = length descVarSs
nVars = length descVarTs

evOne = EV58 0
evTimes = evLCM nVars
Expand All @@ -147,9 +146,9 @@ bp58Ops evCmp isGraded descVarSs useSugar = assert (nVars <= 58)
isRev = nVars > 1 && evCmp (fromBits58 1) (fromBits58 2) == GT
varBitJsDesc = -- most main first
(if isRev then id else reverse) [0 .. nVars - 1]
evShowPrec prec w = productSPrec (const id) prec (catMaybes mVarSs)
evShowPrec w = productPT (map (PrecText atomPrec) (catMaybes mVarTs))
where
mVarSs = zipWith (pureIf . testBit w.w64) varBitJsDesc descVarSs
mVarTs = zipWith (pureIf . testBit w.w64) varBitJsDesc descVarTs

bpPlus = go SL.Nil
where
Expand Down Expand Up @@ -190,7 +189,7 @@ bp58Ops evCmp isGraded descVarSs useSugar = assert (nVars <= 58)
mult1 _ = undefined
homogDeg0 = if isGraded.b then SL.match 0 (\v _ -> evTotDeg v) else
foldl' (\d v -> max d (evTotDeg v)) 0
pShow = pShowPrec 0
pShowPrec = sumPT . map evShowPrec . toList

evMainBit (EV58 w) | isRev = fromBits58 (w .&. (- w))
evMainBit (EV58 0) = EV58 0
Expand All @@ -201,8 +200,7 @@ bp58Ops evCmp isGraded descVarSs useSugar = assert (nVars <= 58)
(∧) = pR.times
x y = bpNot (bpNot x bpNot y)
pAt p bs = foldl' (\b v -> b /= evAt v bs) False p
pShowPrec prec = sumSPrec evShowPrec prec . toList
pRead = readSToRead $ polynomReads pR (zip descVarSs descVarPs)
pParse = zzGensRingParse pR (varParse descVarTs descVarPs)

bpCountZeros :: BPOtherOps EV58 Word64 -> [BinPoly EV58] -> Int
-- ^ @1 <= nVars <= 58@; fastest if the first polynomials are short or have few zeros
Expand Down
35 changes: 17 additions & 18 deletions src/Math/Algebra/Commutative/EPoly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
module Math.Algebra.Commutative.EPoly (
ExponVec, evMake, exponsL, evPlus, evMinusMay, evDividesF, evLCMF, epEvCmpF,
EPoly, RingTgtXs(..), EPolyUniv, EPolyOps(..),
headTotDeg, epOps, EvVarSs(..), evVarSs, evShowPrecF,
headTotDeg, epOps, EvVarPTs(..), evVarPTs, evShowPrecF,
epGBPOps, epCountZeros
) where

Expand Down Expand Up @@ -49,7 +49,7 @@ data Expons = Expons1 Word64

instance Show Expons where -- e.g. for testing & debugging
show (Expons1 w) = show0x w
show (Expons2 w v) = show0x w ++ " " ++ show0x v
show (Expons2 w v) = show0x w <> " " <> show0x v
show (ExponsB m ws) = unwords (map show0x (m : U.toList ws))
show (ExponsN a) = show a

Expand Down Expand Up @@ -325,34 +325,33 @@ epOps cR nVars evCmp = EPolyOps { .. }
foldr1 tR.times (cToT c : zipWithExact (rExpt tR) xTs (exponsL nVars ev))


data EvVarSs = EvVarSs { descVarSs :: [String], nVars :: Int, isRev :: Bool }
-- ^ @descVarSs@ lists more main variables first, and each @varS@ has precedence > '^'.
data EvVarPTs = EvVarPTs { descVarPTs :: [PrecText], nVars :: Int, isRev :: Bool }
-- ^ @descVarPTs@ lists more main variables first.

evVarSs :: [String] -> Cmp ExponVec -> EvVarSs
evVarSs descVarSs evCmp = EvVarSs { descVarSs, nVars, isRev }
evVarPTs :: [PrecText] -> Cmp ExponVec -> EvVarPTs
evVarPTs descVarPTs evCmp = EvVarPTs { descVarPTs, nVars, isRev }
where
nVars = length descVarSs
nVars = length descVarPTs
isRev = nVars > 0 && evCmp (evMake es) (evMake (reverse es)) == LT
where
~es = 1 : replicate (nVars - 1) 0

evShowPrecF :: EvVarSs -> ShowPrec ExponVec
evShowPrecF (EvVarSs { descVarSs, nVars, isRev }) prec ev =
productSPrec powSP prec (zip descVarSs es)
evShowPrecF :: EvVarPTs -> ShowPrec ExponVec
evShowPrecF (EvVarPTs { descVarPTs, nVars, isRev }) ev =
productPT (zipWith varPowShowPrec descVarPTs es)
where
es = (if isRev then reverse else id) (exponsL nVars ev)
powSP prec1 (varS, e) = varPowShowPrec varS prec1 e

epGBPOps :: forall c. Cmp ExponVec -> IsGraded -> Ring c -> [String] -> ShowPrec c ->
epGBPOps :: forall c. Cmp ExponVec -> IsGraded -> Ring c -> [PrecText] -> ShowPrec c ->
UseSugar -> GBPolyOps ExponVec (EPoly c)
{- ^ In @ep58GBPOps evCmp isGraded cR descVarSs cShowPrec useSugar@, @descVarSs@ lists more main
variables first, and each @varS@ has precedence > '^'. -}
epGBPOps evCmp isGraded cR descVarSs cShowPrec useSugar = GBPolyOps { .. }
{- ^ In @ep58GBPOps evCmp isGraded cR descVarPTs cShowPrec useSugar@, @descVarPTs@ lists more
main variables first. -}
epGBPOps evCmp isGraded cR descVarPTs cShowPrec useSugar = GBPolyOps { .. }
where
evSs@(EvVarSs { nVars, isRev }) = evVarSs descVarSs evCmp
evVPTs@(EvVarPTs { nVars, isRev }) = evVarPTs descVarPTs evCmp
nEvGroups = nVars
evGroup = (if isRev then reverse else id) . exponsL nVars
evShowPrec = evShowPrecF evSs
evShowPrec = evShowPrecF evVPTs
EPolyOps { epUniv } = epOps cR nVars evCmp
UnivL pR (RingTgtXs _cToP xs) _pUnivF = epUniv
descVarPs = if isRev then reverse xs else xs
Expand All @@ -364,7 +363,7 @@ epGBPOps evCmp isGraded cR descVarSs cShowPrec useSugar = GBPolyOps { .. }
(\ _ ev t -> ssShift (evPlus nVars (fromJust (evMinusMay nVars m ev))) t)
homogDeg0 = if isGraded.b then sparseSum 0 (\_ ev _ -> evTotDeg ev) else
foldl' (\acc (SSTerm _ !ev) -> max acc ev.totDeg) 0
pShow = ssShowPrec evShowPrec cShowPrec 0
pShowPrec = ssShowPrec evShowPrec cShowPrec

epCountZeros :: Ring c -> [c] -> EPolyOps c -> [EPoly c] -> Int
-- ^ fastest if the first polynomials are short or have few zeros
Expand Down
Loading

0 comments on commit 251dfb8

Please sign in to comment.