diff --git a/src/Jikka/CPlusPlus/Language/Expr.hs b/src/Jikka/CPlusPlus/Language/Expr.hs index fdeda591..e9f76132 100644 --- a/src/Jikka/CPlusPlus/Language/Expr.hs +++ b/src/Jikka/CPlusPlus/Language/Expr.hs @@ -57,8 +57,10 @@ data Monoid' MonoidIntMin | -- | \((\mathrm{int64\_t}, \max, \mathrm{INT64\_MIN})\) MonoidIntMax - | MonoidIntGcd - | MonoidIntLcm + | -- | \((\mathbb{Z}, \gcd, 0)\) + MonoidIntGcd + | -- | \((\mathbb{Z}, \mathrm{lcm}, 1)\) + MonoidIntLcm deriving (Eq, Ord, Show, Read) data Literal diff --git a/src/Jikka/Core/Evaluate.hs b/src/Jikka/Core/Evaluate.hs index 8037a18c..921f9763 100644 --- a/src/Jikka/Core/Evaluate.hs +++ b/src/Jikka/Core/Evaluate.hs @@ -114,6 +114,8 @@ segmentTreeGetRange semigrp segtree l r SemigroupIntPlus -> sum slice SemigroupIntMin -> minimum slice SemigroupIntMax -> maximum slice + SemigroupIntGcd -> foldl gcd 0 slice + SemigroupIntLcm -> foldl lcm 1 slice build :: MonadError Error m => (V.Vector Value -> m Value) -> V.Vector Value -> Integer -> m (V.Vector Value) build _ _ n | n < 0 = throwRuntimeError $ "negative length: " ++ show n @@ -223,6 +225,8 @@ callBuiltin builtin ts args = wrapError' ("while calling builtin " ++ formatBuil Max1 -> go1 valueToList id (V.maximumBy compareValues') ArgMin -> go1 valueToList ValInt $ \xs -> snd (minimumBy (\(x, i) (y, j) -> compareValues' x y <> compare i j) (zip (V.toList xs) [0 ..])) ArgMax -> go1 valueToList ValInt $ \xs -> snd (maximumBy (\(x, i) (y, j) -> compareValues' x y <> compare i j) (zip (V.toList xs) [0 ..])) + Gcd1 -> go1 valueToIntList ValInt (foldl gcd 0) + Lcm1 -> go1 valueToIntList ValInt (foldl lcm 1) All -> go1 valueToBoolList ValBool and Any -> go1 valueToBoolList ValBool or Sorted -> go1 valueToList ValList sortVector diff --git a/src/Jikka/Core/Language/Util.hs b/src/Jikka/Core/Language/Util.hs index e2c7ee05..e80881b4 100644 --- a/src/Jikka/Core/Language/Util.hs +++ b/src/Jikka/Core/Language/Util.hs @@ -274,6 +274,8 @@ isConstantTimeBuiltin = \case Max1 -> False ArgMin -> False ArgMax -> False + Gcd1 -> False + Lcm1 -> False All -> False Any -> False Sorted -> False