{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
module Jikka.Core.Evaluate
( run,
callProgram,
Value (..),
)
where
import Control.Monad.Except
import Data.Bits
import Data.List (maximumBy, minimumBy, sortBy)
import qualified Data.Vector as V
import Jikka.Common.Alpha
import Jikka.Common.Error
import Jikka.Common.Matrix
import Jikka.Core.Format (formatBuiltinIsolated)
import Jikka.Core.Language.BuiltinPatterns
import Jikka.Core.Language.Expr
import Jikka.Core.Language.Lint
import Jikka.Core.Language.Runtime
import Jikka.Core.Language.Util
import Jikka.Core.Language.Value
iterate' :: MonadError Error m => Integer -> Value -> Value -> m Value
iterate' :: Integer -> Value -> Value -> m Value
iterate' Integer
n Value
_ Value
_ | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String -> m Value
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m Value) -> String -> m Value
forall a b. (a -> b) -> a -> b
$ String
"negative number of iteration: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
n
iterate' Integer
0 Value
_ Value
base = Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return Value
base
iterate' Integer
n Value
step Value
base = do
Value
base <- Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
step [Value
base]
Integer -> Value -> Value -> m Value
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> Value -> m Value
iterate' (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1) Value
step Value
base
map' :: MonadError Error m => Value -> V.Vector Value -> m (V.Vector Value)
map' :: Value -> Vector Value -> m (Vector Value)
map' Value
f Vector Value
a = [Value] -> Vector Value
forall a. [a] -> Vector a
V.fromList ([Value] -> Vector Value) -> m [Value] -> m (Vector Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value -> m Value) -> [Value] -> m [Value]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\Value
val -> Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value
val]) (Vector Value -> [Value]
forall a. Vector a -> [a]
V.toList Vector Value
a)
scanM :: Monad m => (a -> b -> m a) -> a -> V.Vector b -> m (V.Vector a)
scanM :: (a -> b -> m a) -> a -> Vector b -> m (Vector a)
scanM a -> b -> m a
f a
y Vector b
xs = do
([a]
ys, a
y) <- (([a], a) -> b -> m ([a], a)) -> ([a], a) -> Vector b -> m ([a], a)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Vector b -> m a
V.foldM (\([a]
ys, a
y) b
x -> (a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
ys,) (a -> ([a], a)) -> m a -> m ([a], a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> a -> b -> m a
f a
y b
x) ([], a
y) Vector b
xs
Vector a -> m (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> m (Vector a)) -> Vector a -> m (Vector a)
forall a b. (a -> b) -> a -> b
$ [a] -> Vector a
forall a. [a] -> Vector a
V.fromList ([a] -> [a]
forall a. [a] -> [a]
reverse (a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
ys))
atEither :: MonadError Error m => V.Vector a -> Integer -> m a
atEither :: Vector a -> Integer -> m a
atEither Vector a
xs Integer
i = case Vector a
xs Vector a -> Int -> Maybe a
forall a. Vector a -> Int -> Maybe a
V.!? Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
i of
Just a
x -> a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
Maybe a
Nothing -> String -> m a
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m a) -> String -> m a
forall a b. (a -> b) -> a -> b
$ String
"out of bounds: length = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Vector a -> Int
forall a. Vector a -> Int
V.length Vector a
xs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", index = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
i
setAtEither :: MonadError Error m => V.Vector a -> Integer -> a -> m (V.Vector a)
setAtEither :: Vector a -> Integer -> a -> m (Vector a)
setAtEither Vector a
xs Integer
i a
x =
if Integer
0 Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
i Bool -> Bool -> Bool
&& Integer
i Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Vector a -> Int
forall a. Vector a -> Int
V.length Vector a
xs)
then Vector a -> m (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> m (Vector a)) -> Vector a -> m (Vector a)
forall a b. (a -> b) -> a -> b
$ Vector a
xs Vector a -> [(Int, a)] -> Vector a
forall a. Vector a -> [(Int, a)] -> Vector a
V.// [(Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
i, a
x)]
else String -> m (Vector a)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m (Vector a)) -> String -> m (Vector a)
forall a b. (a -> b) -> a -> b
$ String
"out of bounds: length = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (Vector a -> Int
forall a. Vector a -> Int
V.length Vector a
xs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", index = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
i
sortVector :: V.Vector Value -> V.Vector Value
sortVector :: Vector Value -> Vector Value
sortVector = [Value] -> Vector Value
forall a. [a] -> Vector a
V.fromList ([Value] -> Vector Value)
-> (Vector Value -> [Value]) -> Vector Value -> Vector Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Value -> Ordering) -> [Value] -> [Value]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Value -> Value -> Ordering
compareValues' ([Value] -> [Value])
-> (Vector Value -> [Value]) -> Vector Value -> [Value]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Value -> [Value]
forall a. Vector a -> [a]
V.toList
range1 :: MonadError Error m => Integer -> m (V.Vector Value)
range1 :: Integer -> m (Vector Value)
range1 Integer
n | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String -> m (Vector Value)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m (Vector Value)) -> String -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ String
"invalid argument for range1: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
n
range1 Integer
n = Vector Value -> m (Vector Value)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector Value -> m (Vector Value))
-> Vector Value -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ [Value] -> Vector Value
forall a. [a] -> Vector a
V.fromList ((Integer -> Value) -> [Integer] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Value
ValInt [Integer
0 .. Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1])
range2 :: MonadError Error m => Integer -> Integer -> m (V.Vector Value)
range2 :: Integer -> Integer -> m (Vector Value)
range2 Integer
l Integer
r | Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
r = String -> m (Vector Value)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m (Vector Value)) -> String -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ String
"invalid argument for range2: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Integer, Integer) -> String
forall a. Show a => a -> String
show (Integer
l, Integer
r)
range2 Integer
l Integer
r = Vector Value -> m (Vector Value)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector Value -> m (Vector Value))
-> Vector Value -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ [Value] -> Vector Value
forall a. [a] -> Vector a
V.fromList ((Integer -> Value) -> [Integer] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Value
ValInt [Integer
l .. Integer
r Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1])
range3 :: MonadError Error m => Integer -> Integer -> Integer -> m (V.Vector Value)
range3 :: Integer -> Integer -> Integer -> m (Vector Value)
range3 Integer
l Integer
r Integer
step | Bool -> Bool
not (Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
r Bool -> Bool -> Bool
&& Integer
step Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0) = String -> m (Vector Value)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m (Vector Value)) -> String -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ String
"invalid argument for range3: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Integer, Integer, Integer) -> String
forall a. Show a => a -> String
show (Integer
l, Integer
r, Integer
step)
range3 Integer
l Integer
r Integer
step = Vector Value -> m (Vector Value)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector Value -> m (Vector Value))
-> Vector Value -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ [Value] -> Vector Value
forall a. [a] -> Vector a
V.fromList ((Integer -> Value) -> [Integer] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map Integer -> Value
ValInt [Integer
l, Integer
l Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
step .. Integer
r])
matap' :: (Num a, MonadError Error m) => Matrix a -> V.Vector a -> m (V.Vector a)
matap' :: Matrix a -> Vector a -> m (Vector a)
matap' Matrix a
f Vector a
x | (Int, Int) -> Int
forall a b. (a, b) -> b
snd (Matrix a -> (Int, Int)
forall a. Matrix a -> (Int, Int)
matsize Matrix a
f) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Vector a -> Int
forall a. Vector a -> Int
V.length Vector a
x = String -> m (Vector a)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwInternalError String
"invalid argument"
matap' Matrix a
f Vector a
x = Vector a -> m (Vector a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Vector a -> m (Vector a)) -> Vector a -> m (Vector a)
forall a b. (a -> b) -> a -> b
$ Matrix a -> Vector a -> Vector a
forall a. Num a => Matrix a -> Vector a -> Vector a
matap Matrix a
f Vector a
x
matadd' :: (Num a, MonadError Error m) => Matrix a -> Matrix a -> m (Matrix a)
matadd' :: Matrix a -> Matrix a -> m (Matrix a)
matadd' Matrix a
f Matrix a
g | Matrix a -> (Int, Int)
forall a. Matrix a -> (Int, Int)
matsize Matrix a
f (Int, Int) -> (Int, Int) -> Bool
forall a. Eq a => a -> a -> Bool
/= Matrix a -> (Int, Int)
forall a. Matrix a -> (Int, Int)
matsize Matrix a
g = String -> m (Matrix a)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwInternalError String
"invalid argument"
matadd' Matrix a
f Matrix a
g = Matrix a -> m (Matrix a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Matrix a -> m (Matrix a)) -> Matrix a -> m (Matrix a)
forall a b. (a -> b) -> a -> b
$ Matrix a -> Matrix a -> Matrix a
forall a. Num a => Matrix a -> Matrix a -> Matrix a
matadd Matrix a
f Matrix a
g
matmul' :: (Num a, MonadError Error m) => Matrix a -> Matrix a -> m (Matrix a)
matmul' :: Matrix a -> Matrix a -> m (Matrix a)
matmul' Matrix a
f Matrix a
g | (Int, Int) -> Int
forall a b. (a, b) -> b
snd (Matrix a -> (Int, Int)
forall a. Matrix a -> (Int, Int)
matsize Matrix a
f) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= (Int, Int) -> Int
forall a b. (a, b) -> a
fst (Matrix a -> (Int, Int)
forall a. Matrix a -> (Int, Int)
matsize Matrix a
g) = String -> m (Matrix a)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwInternalError String
"invalid argument"
matmul' Matrix a
f Matrix a
g = Matrix a -> m (Matrix a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Matrix a -> m (Matrix a)) -> Matrix a -> m (Matrix a)
forall a b. (a -> b) -> a -> b
$ Matrix a -> Matrix a -> Matrix a
forall a. Num a => Matrix a -> Matrix a -> Matrix a
matmul Matrix a
f Matrix a
g
matpow' :: (Num a, Show a, MonadError Error m) => Matrix a -> Integer -> m (Matrix a)
matpow' :: Matrix a -> Integer -> m (Matrix a)
matpow' Matrix a
f Integer
_ | let (Int
h, Int
w) = Matrix a -> (Int, Int)
forall a. Matrix a -> (Int, Int)
matsize Matrix a
f in Int
h Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
w = String -> m (Matrix a)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwInternalError (String -> m (Matrix a)) -> String -> m (Matrix a)
forall a b. (a -> b) -> a -> b
$ String
"matrix is not square: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (Int, Int) -> String
forall a. Show a => a -> String
show (Matrix a -> (Int, Int)
forall a. Matrix a -> (Int, Int)
matsize Matrix a
f)
matpow' Matrix a
_ Integer
k | Integer
k Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String -> m (Matrix a)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m (Matrix a)) -> String -> m (Matrix a)
forall a b. (a -> b) -> a -> b
$ String
"exponent is negative: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
k
matpow' Matrix a
f Integer
k = Matrix a -> m (Matrix a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Matrix a -> m (Matrix a)) -> Matrix a -> m (Matrix a)
forall a b. (a -> b) -> a -> b
$ Matrix a -> Integer -> Matrix a
forall a. (Show a, Num a) => Matrix a -> Integer -> Matrix a
matpow Matrix a
f Integer
k
convexHullTrickGetMin :: MonadError Error m => V.Vector (Integer, Integer) -> Integer -> m Integer
convexHullTrickGetMin :: Vector (Integer, Integer) -> Integer -> m Integer
convexHullTrickGetMin Vector (Integer, Integer)
cht Integer
x =
if Vector (Integer, Integer) -> Bool
forall a. Vector a -> Bool
V.null Vector (Integer, Integer)
cht
then String -> m Integer
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError String
"the set of lines is empty"
else Integer -> m Integer
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> m Integer) -> Integer -> m Integer
forall a b. (a -> b) -> a -> b
$ Vector Integer -> Integer
forall a. Ord a => Vector a -> a
V.minimum (((Integer, Integer) -> Integer)
-> Vector (Integer, Integer) -> Vector Integer
forall a b. (a -> b) -> Vector a -> Vector b
V.map (\(Integer
a, Integer
b) -> Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
b) Vector (Integer, Integer)
cht)
segmentTreeGetRange :: MonadError Error m => Semigroup' -> [Integer] -> Integer -> Integer -> m Integer
segmentTreeGetRange :: Semigroup' -> [Integer] -> Integer -> Integer -> m Integer
segmentTreeGetRange Semigroup'
semigrp [Integer]
segtree Integer
l Integer
r
| Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
r = String -> m Integer
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m Integer) -> String -> m Integer
forall a b. (a -> b) -> a -> b
$ String
"the range has negative length: l = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
l String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
", r = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
r
| Integer
l Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
r = String -> m Integer
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m Integer) -> String -> m Integer
forall a b. (a -> b) -> a -> b
$ String
"the range is empty: l = r = " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
l
| Bool
otherwise =
let slice :: [Integer]
slice = Int -> [Integer] -> [Integer]
forall a. Int -> [a] -> [a]
take (Integer -> Int
forall a. Num a => Integer -> a
fromInteger (Integer
r Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
l)) (Int -> [Integer] -> [Integer]
forall a. Int -> [a] -> [a]
drop (Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
l) [Integer]
segtree)
in Integer -> m Integer
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> m Integer) -> Integer -> m Integer
forall a b. (a -> b) -> a -> b
$ case Semigroup'
semigrp of
Semigroup'
SemigroupIntPlus -> [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer]
slice
Semigroup'
SemigroupIntMin -> [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [Integer]
slice
Semigroup'
SemigroupIntMax -> [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [Integer]
slice
build :: MonadError Error m => (V.Vector Value -> m Value) -> V.Vector Value -> Integer -> m (V.Vector Value)
build :: (Vector Value -> m Value)
-> Vector Value -> Integer -> m (Vector Value)
build Vector Value -> m Value
_ Vector Value
_ Integer
n | Integer
n Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 = String -> m (Vector Value)
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwRuntimeError (String -> m (Vector Value)) -> String -> m (Vector Value)
forall a b. (a -> b) -> a -> b
$ String
"negative length: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Integer -> String
forall a. Show a => a -> String
show Integer
n
build Vector Value -> m Value
_ Vector Value
xs Integer
0 = Vector Value -> m (Vector Value)
forall (m :: * -> *) a. Monad m => a -> m a
return Vector Value
xs
build Vector Value -> m Value
f Vector Value
xs Integer
n = do
Value
y <- Vector Value -> m Value
f Vector Value
xs
(Vector Value -> m Value)
-> Vector Value -> Integer -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
(Vector Value -> m Value)
-> Vector Value -> Integer -> m (Vector Value)
build Vector Value -> m Value
f (Vector Value -> Value -> Vector Value
forall a. Vector a -> a -> Vector a
V.snoc Vector Value
xs Value
y) (Integer
n Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
1)
callBuiltin :: MonadError Error m => Builtin -> [Value] -> m Value
callBuiltin :: Builtin -> [Value] -> m Value
callBuiltin Builtin
builtin [Value]
args = String -> m Value -> m Value
forall (m :: * -> *) a. MonadError Error m => String -> m a -> m a
wrapError' (String
"while calling builtin " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Builtin -> String
formatBuiltinIsolated Builtin
builtin) (m Value -> m Value) -> m Value -> m Value
forall a b. (a -> b) -> a -> b
$ do
let go0 :: (t -> Value) -> t -> m Value
go0 t -> Value
ret t
f = Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue (t -> Value
ret t
f) [Value]
args
let go1' :: (Value -> m a) -> (a -> Value) -> (a -> m a) -> m Value
go1' Value -> m a
t1 a -> Value
ret a -> m a
f = case [Value]
args of
Value
v1 : [Value]
args -> do
Value
f <- a -> Value
ret (a -> Value) -> m a -> m Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> m a
f (a -> m a) -> m a -> m a
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Value -> m a
t1 Value
v1)
Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value]
args
[Value]
_ -> Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Value -> m Value) -> Value -> m Value
forall a b. (a -> b) -> a -> b
$ Builtin -> [Value] -> Value
ValBuiltin Builtin
builtin [Value]
args
let go1 :: (Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m a
t1 a -> Value
ret a -> a
f = (Value -> m a) -> (a -> Value) -> (a -> m a) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> m a) -> m Value
go1' Value -> m a
t1 a -> Value
ret (a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a
f)
let go2' :: (Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m a
t1 Value -> m a
t2 a -> Value
ret a -> a -> m a
f = case [Value]
args of
Value
v1 : Value
v2 : [Value]
args -> do
Value
f <- a -> Value
ret (a -> Value) -> m a -> m Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (m a) -> m a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (a -> a -> m a
f (a -> a -> m a) -> m a -> m (a -> m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m a
t1 Value
v1 m (a -> m a) -> m a -> m (m a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value -> m a
t2 Value
v2)
Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value]
args
[Value]
_ -> Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Value -> m Value) -> Value -> m Value
forall a b. (a -> b) -> a -> b
$ Builtin -> [Value] -> Value
ValBuiltin Builtin
builtin [Value]
args
let go2 :: (Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m a
t1 Value -> m a
t2 a -> Value
ret a -> a -> a
f = (Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m a
t1 Value -> m a
t2 a -> Value
ret ((a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> a) -> a -> m a) -> (a -> a -> a) -> a -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> a
f)
let go3' :: (Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m a
t1 Value -> m a
t2 Value -> m a
t3 a -> Value
ret a -> a -> a -> m a
f = case [Value]
args of
Value
v1 : Value
v2 : Value
v3 : [Value]
args -> do
Value
f <- a -> Value
ret (a -> Value) -> m a -> m Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m (m a) -> m a
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (a -> a -> a -> m a
f (a -> a -> a -> m a) -> m a -> m (a -> a -> m a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> m a
t1 Value
v1 m (a -> a -> m a) -> m a -> m (a -> m a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value -> m a
t2 Value
v2 m (a -> m a) -> m a -> m (m a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value -> m a
t3 Value
v3)
Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value]
args
[Value]
_ -> Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Value -> m Value) -> Value -> m Value
forall a b. (a -> b) -> a -> b
$ Builtin -> [Value] -> Value
ValBuiltin Builtin
builtin [Value]
args
let go3 :: (Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> a)
-> m Value
go3 Value -> m a
t1 Value -> m a
t2 Value -> m a
t3 a -> Value
ret a -> a -> a -> a
f = (Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m a
t1 Value -> m a
t2 Value -> m a
t3 a -> Value
ret (((a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> m a) -> (a -> a) -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> a) -> a -> m a) -> (a -> a -> a) -> a -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
.) ((a -> a -> a) -> a -> a -> m a)
-> (a -> a -> a -> a) -> a -> a -> a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> a -> a -> a
f)
let goN :: Int -> (Value -> m b) -> (b -> Value) -> ([b] -> b) -> m Value
goN Int
n Value -> m b
t b -> Value
ret [b] -> b
f =
if [Value] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Value]
args Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n
then Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Value -> m Value) -> Value -> m Value
forall a b. (a -> b) -> a -> b
$ Builtin -> [Value] -> Value
ValBuiltin Builtin
builtin [Value]
args
else do
Value
f <- b -> Value
ret (b -> Value) -> ([b] -> b) -> [b] -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b] -> b
f ([b] -> Value) -> m [b] -> m Value
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value -> m b) -> [Value] -> m [b]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Value -> m b
t (Int -> [Value] -> [Value]
forall a. Int -> [a] -> [a]
take Int
n [Value]
args)
Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f (Int -> [Value] -> [Value]
forall a. Int -> [a] -> [a]
drop Int
n [Value]
args)
case Builtin
builtin of
Builtin
Negate -> (Value -> m Integer)
-> (Integer -> Value) -> (Integer -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer
forall a. Num a => a -> a
negate
Builtin
Plus -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(+)
Builtin
Minus -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt (-)
Builtin
Mult -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
(*)
Builtin
FloorDiv -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
floorDiv
Builtin
FloorMod -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
floorMod
Builtin
CeilDiv -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
ceilDiv
Builtin
CeilMod -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
ceilMod
Builtin
Pow -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
(^)
Builtin
Abs -> (Value -> m Integer)
-> (Integer -> Value) -> (Integer -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer
forall a. Num a => a -> a
abs
Builtin
Gcd -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
gcd
Builtin
Lcm -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
lcm
Min2 Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> Value)
-> (Value -> Value -> Value)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> Value
forall a. a -> a
id Value -> Value -> Value
minValue
Max2 Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> Value)
-> (Value -> Value -> Value)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> Value
forall a. a -> a
id Value -> Value -> Value
maxValue
Iterate Type
_ -> (Value -> m Integer)
-> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> Value)
-> (Integer -> Value -> Value -> m Value)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> Value
forall a. a -> a
id ((Integer -> Value -> Value -> m Value) -> m Value)
-> (Integer -> Value -> Value -> m Value) -> m Value
forall a b. (a -> b) -> a -> b
$ \Integer
n Value
step Value
base -> Integer -> Value -> Value -> m Value
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> Value -> m Value
iterate' Integer
n Value
step Value
base
Builtin
Not -> (Value -> m Bool) -> (Bool -> Value) -> (Bool -> Bool) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Bool -> Value
ValBool Bool -> Bool
not
Builtin
And -> (Value -> m Bool)
-> (Value -> m Bool)
-> (Bool -> Value)
-> (Bool -> Bool -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Bool -> Value
ValBool Bool -> Bool -> Bool
(&&)
Builtin
Or -> (Value -> m Bool)
-> (Value -> m Bool)
-> (Bool -> Value)
-> (Bool -> Bool -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Bool -> Value
ValBool Bool -> Bool -> Bool
(||)
Builtin
Implies -> (Value -> m Bool)
-> (Value -> m Bool)
-> (Bool -> Value)
-> (Bool -> Bool -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Bool -> Value
ValBool ((Bool -> Bool -> Bool) -> m Value)
-> (Bool -> Bool -> Bool) -> m Value
forall a b. (a -> b) -> a -> b
$ \Bool
p Bool
q -> Bool -> Bool
not Bool
p Bool -> Bool -> Bool
|| Bool
q
If Type
_ -> (Value -> m Bool)
-> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> Value)
-> (Bool -> Value -> Value -> Value)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> a)
-> m Value
go3 Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> Value
forall a. a -> a
id ((Bool -> Value -> Value -> Value) -> m Value)
-> (Bool -> Value -> Value -> Value) -> m Value
forall a b. (a -> b) -> a -> b
$ \Bool
p Value
a Value
b -> if Bool
p then Value
a else Value
b
Builtin
BitNot -> (Value -> m Integer)
-> (Integer -> Value) -> (Integer -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer
forall a. Bits a => a -> a
complement
Builtin
BitAnd -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
(.&.)
Builtin
BitOr -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
(.|.)
Builtin
BitXor -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer
forall a. Bits a => a -> a -> a
xor
Builtin
BitLeftShift -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt ((Integer -> Integer -> Integer) -> m Value)
-> (Integer -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Integer
a Integer
b -> Integer
a Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shift` Integer -> Int
forall a. Num a => Integer -> a
fromInteger Integer
b
Builtin
BitRightShift -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt ((Integer -> Integer -> Integer) -> m Value)
-> (Integer -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Integer
a Integer
b -> Integer
a Integer -> Int -> Integer
forall a. Bits a => a -> Int -> a
`shift` Integer -> Int
forall a. Num a => Integer -> a
fromInteger (- Integer
b)
MatAp Int
_ Int
_ -> (Value -> m (Matrix Integer))
-> (Value -> m (Vector Integer))
-> (Vector Integer -> Value)
-> (Matrix Integer -> Vector Integer -> m (Vector Integer))
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m (Matrix Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Matrix Integer)
valueToMatrix Value -> m (Vector Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Integer)
valueToVector Vector Integer -> Value
valueFromVector Matrix Integer -> Vector Integer -> m (Vector Integer)
forall a (m :: * -> *).
(Num a, MonadError Error m) =>
Matrix a -> Vector a -> m (Vector a)
matap'
MatZero Int
n -> (Matrix Integer -> Value) -> Matrix Integer -> m Value
forall (m :: * -> *) t.
MonadError Error m =>
(t -> Value) -> t -> m Value
go0 Matrix Integer -> Value
valueFromMatrix (Int -> Matrix Integer
forall a. Num a => Int -> Matrix a
matzero Int
n)
MatOne Int
n -> (Matrix Integer -> Value) -> Matrix Integer -> m Value
forall (m :: * -> *) t.
MonadError Error m =>
(t -> Value) -> t -> m Value
go0 Matrix Integer -> Value
valueFromMatrix (Int -> Matrix Integer
forall a. Num a => Int -> Matrix a
matone Int
n)
MatAdd Int
_ Int
_ -> (Value -> m (Matrix Integer))
-> (Value -> m (Matrix Integer))
-> (Matrix Integer -> Value)
-> (Matrix Integer -> Matrix Integer -> m (Matrix Integer))
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m (Matrix Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Matrix Integer)
valueToMatrix Value -> m (Matrix Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Matrix Integer)
valueToMatrix Matrix Integer -> Value
valueFromMatrix Matrix Integer -> Matrix Integer -> m (Matrix Integer)
forall a (m :: * -> *).
(Num a, MonadError Error m) =>
Matrix a -> Matrix a -> m (Matrix a)
matadd'
MatMul Int
_ Int
_ Int
_ -> (Value -> m (Matrix Integer))
-> (Value -> m (Matrix Integer))
-> (Matrix Integer -> Value)
-> (Matrix Integer -> Matrix Integer -> m (Matrix Integer))
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m (Matrix Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Matrix Integer)
valueToMatrix Value -> m (Matrix Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Matrix Integer)
valueToMatrix Matrix Integer -> Value
valueFromMatrix Matrix Integer -> Matrix Integer -> m (Matrix Integer)
forall a (m :: * -> *).
(Num a, MonadError Error m) =>
Matrix a -> Matrix a -> m (Matrix a)
matmul'
MatPow Int
_ -> (Value -> m (Matrix Integer))
-> (Value -> m Integer)
-> (Matrix Integer -> Value)
-> (Matrix Integer -> Integer -> m (Matrix Integer))
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m (Matrix Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Matrix Integer)
valueToMatrix Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Matrix Integer -> Value
valueFromMatrix Matrix Integer -> Integer -> m (Matrix Integer)
forall a (m :: * -> *).
(Num a, Show a, MonadError Error m) =>
Matrix a -> Integer -> m (Matrix a)
matpow'
VecFloorMod Int
_ -> (Value -> m (Vector Integer))
-> (Value -> m Integer)
-> (Vector Integer -> Value)
-> (Vector Integer -> Integer -> Vector Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m (Vector Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Integer)
valueToVector Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Vector Integer -> Value
valueFromVector ((Vector Integer -> Integer -> Vector Integer) -> m Value)
-> (Vector Integer -> Integer -> Vector Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Vector Integer
x Integer
m -> (Integer -> Integer) -> Vector Integer -> Vector Integer
forall a b. (a -> b) -> Vector a -> Vector b
V.map (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m) Vector Integer
x
MatFloorMod Int
_ Int
_ -> (Value -> m (Matrix Integer))
-> (Value -> m Integer)
-> (Matrix Integer -> Value)
-> (Matrix Integer -> Integer -> Matrix Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m (Matrix Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Matrix Integer)
valueToMatrix Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Matrix Integer -> Value
valueFromMatrix ((Matrix Integer -> Integer -> Matrix Integer) -> m Value)
-> (Matrix Integer -> Integer -> Matrix Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Matrix Integer
f Integer
m -> (Integer -> Integer) -> Matrix Integer -> Matrix Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m) Matrix Integer
f
Builtin
ModNegate -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt ((Integer -> Integer -> Integer) -> m Value)
-> (Integer -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Integer
a Integer
m -> (- Integer
a) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
Builtin
ModPlus -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> a)
-> m Value
go3 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt ((Integer -> Integer -> Integer -> Integer) -> m Value)
-> (Integer -> Integer -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Integer
a Integer
b Integer
m -> (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
b) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
Builtin
ModMinus -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> a)
-> m Value
go3 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt ((Integer -> Integer -> Integer -> Integer) -> m Value)
-> (Integer -> Integer -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Integer
a Integer
b Integer
m -> (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
b) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
Builtin
ModMult -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> a)
-> m Value
go3 Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt ((Integer -> Integer -> Integer -> Integer) -> m Value)
-> (Integer -> Integer -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Integer
a Integer
b Integer
m -> (Integer
a Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
b) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
Builtin
ModInv -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
modinv
Builtin
ModPow -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> Integer -> m Integer
modpow
ModMatAp Int
_ Int
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> m Integer)
-> (Vector ModInt -> Value)
-> (Value -> Value -> Integer -> m (Vector ModInt))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Vector ModInt -> Value
valueFromModVector ((Value -> Value -> Integer -> m (Vector ModInt)) -> m Value)
-> (Value -> Value -> Integer -> m (Vector ModInt)) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Value
x Integer
m -> m (m (Vector ModInt)) -> m (Vector ModInt)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Matrix ModInt -> Vector ModInt -> m (Vector ModInt)
forall a (m :: * -> *).
(Num a, MonadError Error m) =>
Matrix a -> Vector a -> m (Vector a)
matap' (Matrix ModInt -> Vector ModInt -> m (Vector ModInt))
-> m (Matrix ModInt) -> m (Vector ModInt -> m (Vector ModInt))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> Value -> m (Matrix ModInt)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> m (Matrix ModInt)
valueToModMatrix Integer
m Value
f m (Vector ModInt -> m (Vector ModInt))
-> m (Vector ModInt) -> m (m (Vector ModInt))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Integer -> Value -> m (Vector ModInt)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> m (Vector ModInt)
valueToModVector Integer
m Value
x)
ModMatAdd Int
_ Int
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> m Integer)
-> (Matrix ModInt -> Value)
-> (Value -> Value -> Integer -> m (Matrix ModInt))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Matrix ModInt -> Value
valueFromModMatrix ((Value -> Value -> Integer -> m (Matrix ModInt)) -> m Value)
-> (Value -> Value -> Integer -> m (Matrix ModInt)) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Value
g Integer
m -> m (m (Matrix ModInt)) -> m (Matrix ModInt)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Matrix ModInt -> Matrix ModInt -> m (Matrix ModInt)
forall a (m :: * -> *).
(Num a, MonadError Error m) =>
Matrix a -> Matrix a -> m (Matrix a)
matadd' (Matrix ModInt -> Matrix ModInt -> m (Matrix ModInt))
-> m (Matrix ModInt) -> m (Matrix ModInt -> m (Matrix ModInt))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> Value -> m (Matrix ModInt)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> m (Matrix ModInt)
valueToModMatrix Integer
m Value
f m (Matrix ModInt -> m (Matrix ModInt))
-> m (Matrix ModInt) -> m (m (Matrix ModInt))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Integer -> Value -> m (Matrix ModInt)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> m (Matrix ModInt)
valueToModMatrix Integer
m Value
g)
ModMatMul Int
_ Int
_ Int
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> m Integer)
-> (Matrix ModInt -> Value)
-> (Value -> Value -> Integer -> m (Matrix ModInt))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Matrix ModInt -> Value
valueFromModMatrix ((Value -> Value -> Integer -> m (Matrix ModInt)) -> m Value)
-> (Value -> Value -> Integer -> m (Matrix ModInt)) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Value
g Integer
m -> m (m (Matrix ModInt)) -> m (Matrix ModInt)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Matrix ModInt -> Matrix ModInt -> m (Matrix ModInt)
forall a (m :: * -> *).
(Num a, MonadError Error m) =>
Matrix a -> Matrix a -> m (Matrix a)
matmul' (Matrix ModInt -> Matrix ModInt -> m (Matrix ModInt))
-> m (Matrix ModInt) -> m (Matrix ModInt -> m (Matrix ModInt))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> Value -> m (Matrix ModInt)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> m (Matrix ModInt)
valueToModMatrix Integer
m Value
f m (Matrix ModInt -> m (Matrix ModInt))
-> m (Matrix ModInt) -> m (m (Matrix ModInt))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Integer -> Value -> m (Matrix ModInt)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> m (Matrix ModInt)
valueToModMatrix Integer
m Value
g)
ModMatPow Int
_ -> (Value -> m Value)
-> (Value -> m Integer)
-> (Value -> m Integer)
-> (Matrix ModInt -> Value)
-> (Value -> Integer -> Integer -> m (Matrix ModInt))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Matrix ModInt -> Value
valueFromModMatrix ((Value -> Integer -> Integer -> m (Matrix ModInt)) -> m Value)
-> (Value -> Integer -> Integer -> m (Matrix ModInt)) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Integer
k Integer
m -> m (m (Matrix ModInt)) -> m (Matrix ModInt)
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Matrix ModInt -> Integer -> m (Matrix ModInt)
forall a (m :: * -> *).
(Num a, Show a, MonadError Error m) =>
Matrix a -> Integer -> m (Matrix a)
matpow' (Matrix ModInt -> Integer -> m (Matrix ModInt))
-> m (Matrix ModInt) -> m (Integer -> m (Matrix ModInt))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Integer -> Value -> m (Matrix ModInt)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Value -> m (Matrix ModInt)
valueToModMatrix Integer
m Value
f m (Integer -> m (Matrix ModInt))
-> m Integer -> m (m (Matrix ModInt))
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Integer -> m Integer
forall (f :: * -> *) a. Applicative f => a -> f a
pure Integer
k)
Cons Type
_ -> (Value -> m Value)
-> (Value -> m (Vector Value))
-> (Vector Value -> Value)
-> (Value -> Vector Value -> Vector Value)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Vector Value -> Value
ValList Value -> Vector Value -> Vector Value
forall a. a -> Vector a -> Vector a
V.cons
Snoc Type
_ -> (Value -> m (Vector Value))
-> (Value -> m Value)
-> (Vector Value -> Value)
-> (Vector Value -> Value -> Vector Value)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Value -> Value
ValList Vector Value -> Value -> Vector Value
forall a. Vector a -> a -> Vector a
V.snoc
Foldl Type
_ Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> m (Vector Value))
-> (Value -> Value)
-> (Value -> Value -> Vector Value -> m Value)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> Value
forall a. a -> a
id ((Value -> Value -> Vector Value -> m Value) -> m Value)
-> (Value -> Value -> Vector Value -> m Value) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Value
x Vector Value
a -> (Value -> Value -> m Value) -> Value -> Vector Value -> m Value
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Vector b -> m a
V.foldM (\Value
x Value
y -> Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value
x, Value
y]) Value
x Vector Value
a
Scanl Type
_ Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Value -> m (Vector Value))
-> (Vector Value -> Value)
-> (Value -> Value -> Vector Value -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Vector Value -> Value
ValList ((Value -> Value -> Vector Value -> m (Vector Value)) -> m Value)
-> (Value -> Value -> Vector Value -> m (Vector Value)) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Value
x Vector Value
a -> (Value -> Value -> m Value)
-> Value -> Vector Value -> m (Vector Value)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Vector b -> m (Vector a)
scanM (\Value
x Value
y -> Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value
x, Value
y]) Value
x Vector Value
a
Build Type
_ -> (Value -> m Value)
-> (Value -> m (Vector Value))
-> (Value -> m Integer)
-> (Vector Value -> Value)
-> (Value -> Vector Value -> Integer -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Vector Value -> Value
ValList ((Value -> Vector Value -> Integer -> m (Vector Value)) -> m Value)
-> (Value -> Vector Value -> Integer -> m (Vector Value))
-> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Vector Value
xs Integer
n -> (Vector Value -> m Value)
-> Vector Value -> Integer -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
(Vector Value -> m Value)
-> Vector Value -> Integer -> m (Vector Value)
build (\Vector Value
xs -> Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Vector Value -> Value
ValList Vector Value
xs]) Vector Value
xs Integer
n
Len Type
_ -> (Value -> m (Vector Value))
-> (Integer -> Value) -> (Vector Value -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Integer -> Value
ValInt (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer)
-> (Vector Value -> Int) -> Vector Value -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Value -> Int
forall a. Vector a -> Int
V.length)
Map Type
_ Type
_ -> (Value -> m Value)
-> (Value -> m (Vector Value))
-> (Vector Value -> Value)
-> (Value -> Vector Value -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Vector Value -> Value
ValList Value -> Vector Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> Vector Value -> m (Vector Value)
map'
Filter Type
_ -> (Value -> m Value)
-> (Value -> m (Vector Value))
-> (Vector Value -> Value)
-> (Value -> Vector Value -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Vector Value -> Value
ValList ((Value -> Vector Value -> m (Vector Value)) -> m Value)
-> (Value -> Vector Value -> m (Vector Value)) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
f Vector Value
xs -> (Value -> m Bool) -> Vector Value -> m (Vector Value)
forall (m :: * -> *) a.
Monad m =>
(a -> m Bool) -> Vector a -> m (Vector a)
V.filterM (\Value
x -> (Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
/= Bool -> Value
ValBool Bool
False) (Value -> Bool) -> m Value -> m Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value
x]) Vector Value
xs
At Type
_ -> (Value -> m (Vector Value))
-> (Value -> m Integer)
-> (Value -> Value)
-> (Vector Value -> Integer -> m Value)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> Value
forall a. a -> a
id Vector Value -> Integer -> m Value
forall (m :: * -> *) a.
MonadError Error m =>
Vector a -> Integer -> m a
atEither
SetAt Type
_ -> (Value -> m (Vector Value))
-> (Value -> m Integer)
-> (Value -> m Value)
-> (Vector Value -> Value)
-> (Vector Value -> Integer -> Value -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Value -> Value
ValList Vector Value -> Integer -> Value -> m (Vector Value)
forall (m :: * -> *) a.
MonadError Error m =>
Vector a -> Integer -> a -> m (Vector a)
setAtEither
Elem Type
_ -> (Value -> m Value)
-> (Value -> m (Vector Value))
-> (Bool -> Value)
-> (Value -> Vector Value -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Bool -> Value
ValBool Value -> Vector Value -> Bool
forall a. Eq a => a -> Vector a -> Bool
V.elem
Builtin
Sum -> (Value -> m [Integer])
-> (Integer -> Value) -> ([Integer] -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m [Integer]
forall (m :: * -> *). MonadError Error m => Value -> m [Integer]
valueToIntList Integer -> Value
ValInt [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum
Builtin
ModSum -> (Value -> m [Integer])
-> (Value -> m Integer)
-> (Integer -> Value)
-> ([Integer] -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m [Integer]
forall (m :: * -> *). MonadError Error m => Value -> m [Integer]
valueToIntList Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt (([Integer] -> Integer -> Integer) -> m Value)
-> ([Integer] -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \[Integer]
xs Integer
m -> [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Integer]
xs Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
Builtin
Product -> (Value -> m [Integer])
-> (Integer -> Value) -> ([Integer] -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m [Integer]
forall (m :: * -> *). MonadError Error m => Value -> m [Integer]
valueToIntList Integer -> Value
ValInt [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product
Builtin
ModProduct -> (Value -> m [Integer])
-> (Value -> m Integer)
-> (Integer -> Value)
-> ([Integer] -> Integer -> Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m [Integer]
forall (m :: * -> *). MonadError Error m => Value -> m [Integer]
valueToIntList Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt (([Integer] -> Integer -> Integer) -> m Value)
-> ([Integer] -> Integer -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \[Integer]
xs Integer
m -> [Integer] -> Integer
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
product [Integer]
xs Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
m
Min1 Type
_ -> (Value -> m (Vector Value))
-> (Value -> Value) -> (Vector Value -> Value) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> Value
forall a. a -> a
id ((Value -> Value -> Ordering) -> Vector Value -> Value
forall a. (a -> a -> Ordering) -> Vector a -> a
V.minimumBy Value -> Value -> Ordering
compareValues')
Max1 Type
_ -> (Value -> m (Vector Value))
-> (Value -> Value) -> (Vector Value -> Value) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> Value
forall a. a -> a
id ((Value -> Value -> Ordering) -> Vector Value -> Value
forall a. (a -> a -> Ordering) -> Vector a -> a
V.maximumBy Value -> Value -> Ordering
compareValues')
ArgMin Type
_ -> (Value -> m (Vector Value))
-> (Integer -> Value) -> (Vector Value -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Integer -> Value
ValInt ((Vector Value -> Integer) -> m Value)
-> (Vector Value -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Vector Value
xs -> (Value, Integer) -> Integer
forall a b. (a, b) -> b
snd (((Value, Integer) -> (Value, Integer) -> Ordering)
-> [(Value, Integer)] -> (Value, Integer)
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
minimumBy (\(Value
x, Integer
i) (Value
y, Integer
j) -> Value -> Value -> Ordering
compareValues' Value
x Value
y Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Integer -> Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Integer
i Integer
j) ([Value] -> [Integer] -> [(Value, Integer)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Vector Value -> [Value]
forall a. Vector a -> [a]
V.toList Vector Value
xs) [Integer
0 ..]))
ArgMax Type
_ -> (Value -> m (Vector Value))
-> (Integer -> Value) -> (Vector Value -> Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Integer -> Value
ValInt ((Vector Value -> Integer) -> m Value)
-> (Vector Value -> Integer) -> m Value
forall a b. (a -> b) -> a -> b
$ \Vector Value
xs -> (Value, Integer) -> Integer
forall a b. (a, b) -> b
snd (((Value, Integer) -> (Value, Integer) -> Ordering)
-> [(Value, Integer)] -> (Value, Integer)
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
maximumBy (\(Value
x, Integer
i) (Value
y, Integer
j) -> Value -> Value -> Ordering
compareValues' Value
x Value
y Ordering -> Ordering -> Ordering
forall a. Semigroup a => a -> a -> a
<> Integer -> Integer -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Integer
i Integer
j) ([Value] -> [Integer] -> [(Value, Integer)]
forall a b. [a] -> [b] -> [(a, b)]
zip (Vector Value -> [Value]
forall a. Vector a -> [a]
V.toList Vector Value
xs) [Integer
0 ..]))
Builtin
All -> (Value -> m [Bool])
-> (Bool -> Value) -> ([Bool] -> Bool) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m [Bool]
forall (m :: * -> *). MonadError Error m => Value -> m [Bool]
valueToBoolList Bool -> Value
ValBool [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and
Builtin
Any -> (Value -> m [Bool])
-> (Bool -> Value) -> ([Bool] -> Bool) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m [Bool]
forall (m :: * -> *). MonadError Error m => Value -> m [Bool]
valueToBoolList Bool -> Value
ValBool [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
or
Sorted Type
_ -> (Value -> m (Vector Value))
-> (Vector Value -> Value)
-> (Vector Value -> Vector Value)
-> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Vector Value -> Value
ValList Vector Value -> Vector Value
sortVector
Reversed Type
_ -> (Value -> m (Vector Value))
-> (Vector Value -> Value)
-> (Vector Value -> Vector Value)
-> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Vector Value -> Value
ValList Vector Value -> Vector Value
forall a. Vector a -> Vector a
V.reverse
Builtin
Range1 -> (Value -> m Integer)
-> (Vector Value -> Value)
-> (Integer -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> m a) -> m Value
go1' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Vector Value -> Value
ValList Integer -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Integer -> m (Vector Value)
range1
Builtin
Range2 -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Vector Value -> Value)
-> (Integer -> Integer -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Vector Value -> Value
ValList Integer -> Integer -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m (Vector Value)
range2
Builtin
Range3 -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Value -> m Integer)
-> (Vector Value -> Value)
-> (Integer -> Integer -> Integer -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Vector Value -> Value
ValList Integer -> Integer -> Integer -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> Integer -> m (Vector Value)
range3
Tuple [Type]
ts -> Int
-> (Value -> m Value)
-> ([Value] -> Value)
-> ([Value] -> [Value])
-> m Value
forall (m :: * -> *) b b.
MonadError Error m =>
Int -> (Value -> m b) -> (b -> Value) -> ([b] -> b) -> m Value
goN ([Type] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Type]
ts) Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Value] -> Value
ValTuple [Value] -> [Value]
forall a. a -> a
id
Proj [Type]
_ Int
n -> (Value -> m [Value])
-> (Value -> Value) -> ([Value] -> Value) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m [Value]
forall (m :: * -> *). MonadError Error m => Value -> m [Value]
valueToTuple Value -> Value
forall a. a -> a
id ([Value] -> Int -> Value
forall a. [a] -> Int -> a
!! Int
n)
LessThan Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Bool -> Value)
-> (Value -> Value -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool -> Value
ValBool ((Value -> Value -> Bool) -> m Value)
-> (Value -> Value -> Bool) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
a Value
b -> Value -> Value -> Maybe Ordering
compareValues Value
a Value
b Maybe Ordering -> Maybe Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering -> Maybe Ordering
forall a. a -> Maybe a
Just Ordering
LT
LessEqual Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Bool -> Value)
-> (Value -> Value -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool -> Value
ValBool ((Value -> Value -> Bool) -> m Value)
-> (Value -> Value -> Bool) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
a Value
b -> Value -> Value -> Maybe Ordering
compareValues Value
a Value
b Maybe Ordering -> Maybe Ordering -> Bool
forall a. Eq a => a -> a -> Bool
/= Ordering -> Maybe Ordering
forall a. a -> Maybe a
Just Ordering
GT
GreaterThan Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Bool -> Value)
-> (Value -> Value -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool -> Value
ValBool ((Value -> Value -> Bool) -> m Value)
-> (Value -> Value -> Bool) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
a Value
b -> Value -> Value -> Maybe Ordering
compareValues Value
a Value
b Maybe Ordering -> Maybe Ordering -> Bool
forall a. Eq a => a -> a -> Bool
== Ordering -> Maybe Ordering
forall a. a -> Maybe a
Just Ordering
GT
GreaterEqual Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Bool -> Value)
-> (Value -> Value -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool -> Value
ValBool ((Value -> Value -> Bool) -> m Value)
-> (Value -> Value -> Bool) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
a Value
b -> Value -> Value -> Maybe Ordering
compareValues Value
a Value
b Maybe Ordering -> Maybe Ordering -> Bool
forall a. Eq a => a -> a -> Bool
/= Ordering -> Maybe Ordering
forall a. a -> Maybe a
Just Ordering
LT
Equal Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Bool -> Value)
-> (Value -> Value -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool -> Value
ValBool Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
(==)
NotEqual Type
_ -> (Value -> m Value)
-> (Value -> m Value)
-> (Bool -> Value)
-> (Value -> Value -> Bool)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> a) -> m Value
go2 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool -> Value
ValBool Value -> Value -> Bool
forall a. Eq a => a -> a -> Bool
(/=)
Builtin
Fact -> (Value -> m Integer)
-> (Integer -> Value) -> (Integer -> m Integer) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> m a) -> m Value
go1' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> m Integer
forall (m :: * -> *). MonadError Error m => Integer -> m Integer
fact
Builtin
Choose -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
choose
Builtin
Permute -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
permute
Builtin
MultiChoose -> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Integer -> Integer -> m Integer
multichoose
Builtin
ConvexHullTrickInit -> (Vector Value -> Value) -> Vector Value -> m Value
forall (m :: * -> *) t.
MonadError Error m =>
(t -> Value) -> t -> m Value
go0 Vector Value -> Value
ValList Vector Value
forall a. Vector a
V.empty
Builtin
ConvexHullTrickGetMin -> (Value -> m (Vector (Integer, Integer)))
-> (Value -> m Integer)
-> (Integer -> Value)
-> (Vector (Integer, Integer) -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a) -> (a -> Value) -> (a -> a -> m a) -> m Value
go2' ((Value -> m (Integer, Integer))
-> Vector Value -> m (Vector (Integer, Integer))
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> Vector a -> m (Vector b)
V.mapM Value -> m (Integer, Integer)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Integer, Integer)
valueToIntPair (Vector Value -> m (Vector (Integer, Integer)))
-> (Value -> m (Vector Value))
-> Value
-> m (Vector (Integer, Integer))
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList) Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt Vector (Integer, Integer) -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Vector (Integer, Integer) -> Integer -> m Integer
convexHullTrickGetMin
Builtin
ConvexHullTrickInsert -> (Value -> m (Vector Value))
-> (Value -> m Value)
-> (Value -> m Value)
-> (Vector Value -> Value)
-> (Vector Value -> Value -> Value -> Vector Value)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> a)
-> m Value
go3 Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Value -> Value
ValList ((Vector Value -> Value -> Value -> Vector Value) -> m Value)
-> (Vector Value -> Value -> Value -> Vector Value) -> m Value
forall a b. (a -> b) -> a -> b
$ \Vector Value
cht Value
a Value
b -> Vector Value -> Value -> Vector Value
forall a. Vector a -> a -> Vector a
V.snoc Vector Value
cht ([Value] -> Value
ValTuple [Value
a, Value
b])
SegmentTreeInitList Semigroup'
_ -> (Value -> m Value)
-> (Value -> Value) -> (Value -> Value) -> m Value
forall (m :: * -> *) a a.
MonadError Error m =>
(Value -> m a) -> (a -> Value) -> (a -> a) -> m Value
go1 Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value -> Value
forall a. a -> a
id Value -> Value
forall a. a -> a
id
SegmentTreeGetRange Semigroup'
semigrp -> (Value -> m [Integer])
-> (Value -> m Integer)
-> (Value -> m Integer)
-> (Integer -> Value)
-> ([Integer] -> Integer -> Integer -> m Integer)
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m [Integer]
forall (m :: * -> *). MonadError Error m => Value -> m [Integer]
valueToIntList Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Integer -> Value
ValInt (Semigroup' -> [Integer] -> Integer -> Integer -> m Integer
forall (m :: * -> *).
MonadError Error m =>
Semigroup' -> [Integer] -> Integer -> Integer -> m Integer
segmentTreeGetRange Semigroup'
semigrp)
SegmentTreeSetPoint Semigroup'
_ -> (Value -> m (Vector Value))
-> (Value -> m Integer)
-> (Value -> m Value)
-> (Vector Value -> Value)
-> (Vector Value -> Integer -> Value -> m (Vector Value))
-> m Value
forall (m :: * -> *) a a a a.
MonadError Error m =>
(Value -> m a)
-> (Value -> m a)
-> (Value -> m a)
-> (a -> Value)
-> (a -> a -> a -> m a)
-> m Value
go3' Value -> m (Vector Value)
forall (m :: * -> *).
MonadError Error m =>
Value -> m (Vector Value)
valueToList Value -> m Integer
forall (m :: * -> *). MonadError Error m => Value -> m Integer
valueToInt Value -> m Value
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Value -> Value
ValList Vector Value -> Integer -> Value -> m (Vector Value)
forall (m :: * -> *) a.
MonadError Error m =>
Vector a -> Integer -> a -> m (Vector a)
setAtEither
callLambda :: MonadError Error m => Maybe VarName -> Env -> VarName -> Type -> Expr -> [Value] -> m Value
callLambda :: Maybe VarName
-> Env -> VarName -> Type -> Expr -> [Value] -> m Value
callLambda = \Maybe VarName
name Env
env VarName
x Type
t Expr
body [Value]
args -> String -> m Value -> m Value
forall (m :: * -> *) a. MonadError Error m => String -> m a -> m a
wrapError' (String
"while calling lambda " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> (VarName -> String) -> Maybe VarName -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"(anonymous)" VarName -> String
unVarName Maybe VarName
name) (m Value -> m Value) -> m Value -> m Value
forall a b. (a -> b) -> a -> b
$ Maybe VarName
-> Env -> VarName -> Type -> Expr -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Maybe VarName
-> Env -> VarName -> Type -> Expr -> [Value] -> m Value
go Maybe VarName
forall a. Maybe a
Nothing Env
env VarName
x Type
t Expr
body [Value]
args
where
go :: Maybe VarName
-> Env -> VarName -> Type -> Expr -> [Value] -> m Value
go Maybe VarName
name Env
env VarName
x Type
t Expr
body [] = Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Value -> m Value) -> Value -> m Value
forall a b. (a -> b) -> a -> b
$ Maybe VarName -> Env -> VarName -> Type -> Expr -> Value
ValLambda Maybe VarName
name Env
env VarName
x Type
t Expr
body
go Maybe VarName
name Env
env VarName
x Type
_ Expr
body (Value
e : [Value]
args) = (m Value -> m Value)
-> (VarName -> m Value -> m Value)
-> Maybe VarName
-> m Value
-> m Value
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m Value -> m Value
forall a. a -> a
id (\VarName
name -> String -> m Value -> m Value
forall (m :: * -> *) a. MonadError Error m => String -> m a -> m a
wrapError' (String -> m Value -> m Value) -> String -> m Value -> m Value
forall a b. (a -> b) -> a -> b
$ String
"while calling lambda " String -> String -> String
forall a. [a] -> [a] -> [a]
++ VarName -> String
unVarName VarName
name) Maybe VarName
name (m Value -> m Value) -> m Value -> m Value
forall a b. (a -> b) -> a -> b
$ do
Value
body <- Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr ((VarName
x, Value
e) (VarName, Value) -> Env -> Env
forall a. a -> [a] -> [a]
: Env
env) Expr
body
case Value
body of
ValLambda Maybe VarName
name Env
env VarName
x Type
t Expr
body -> Maybe VarName
-> Env -> VarName -> Type -> Expr -> [Value] -> m Value
go Maybe VarName
name Env
env VarName
x Type
t Expr
body [Value]
args
Value
_ -> Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
body [Value]
args
callValue :: MonadError Error m => Value -> [Value] -> m Value
callValue :: Value -> [Value] -> m Value
callValue Value
f [Value]
args = case (Value
f, [Value]
args) of
(ValBuiltin Builtin
builtin [Value]
args', [Value]
_) -> Builtin -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Builtin -> [Value] -> m Value
callBuiltin Builtin
builtin ([Value]
args' [Value] -> [Value] -> [Value]
forall a. [a] -> [a] -> [a]
++ [Value]
args)
(ValLambda Maybe VarName
name Env
env VarName
x Type
t Expr
body, [Value]
_) -> Maybe VarName
-> Env -> VarName -> Type -> Expr -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Maybe VarName
-> Env -> VarName -> Type -> Expr -> [Value] -> m Value
callLambda Maybe VarName
name Env
env VarName
x Type
t Expr
body [Value]
args
(Value
_, []) -> Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return Value
f
(Value, [Value])
_ -> String -> m Value
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwInternalError (String -> m Value) -> String -> m Value
forall a b. (a -> b) -> a -> b
$ String
"cannot call a non-function: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Value -> String
formatValue Value
f
evaluateExpr :: MonadError Error m => Env -> Expr -> m Value
evaluateExpr :: Env -> Expr -> m Value
evaluateExpr Env
env = \case
Var VarName
x -> case VarName -> Env -> Maybe Value
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup VarName
x Env
env of
Maybe Value
Nothing -> String -> m Value
forall (m :: * -> *) a. MonadError Error m => String -> m a
throwInternalError (String -> m Value) -> String -> m Value
forall a b. (a -> b) -> a -> b
$ String
"undefined variable: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ VarName -> String
unVarName VarName
x
Just Value
val -> Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return Value
val
Lit Literal
lit -> Literal -> m Value
forall (m :: * -> *). MonadError Error m => Literal -> m Value
literalToValue Literal
lit
If' Type
_ Expr
p Expr
e1 Expr
e2 -> do
Bool
p <- Value -> m Bool
forall (m :: * -> *). MonadError Error m => Value -> m Bool
valueToBool (Value -> m Bool) -> m Value -> m Bool
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env Expr
p
if Bool
p
then Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env Expr
e1
else Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env Expr
e2
e :: Expr
e@(App Expr
_ Expr
_) -> do
let (Expr
f, [Expr]
args) = Expr -> (Expr, [Expr])
curryApp Expr
e
Value
f <- Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env Expr
f
[Value]
args <- (Expr -> m Value) -> [Expr] -> m [Value]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env) [Expr]
args
Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
f [Value]
args
Lam VarName
x Type
t Expr
body -> Value -> m Value
forall (m :: * -> *) a. Monad m => a -> m a
return (Value -> m Value) -> Value -> m Value
forall a b. (a -> b) -> a -> b
$ Maybe VarName -> Env -> VarName -> Type -> Expr -> Value
ValLambda Maybe VarName
forall a. Maybe a
Nothing Env
env VarName
x Type
t Expr
body
Let VarName
x Type
_ Expr
e1 Expr
e2 -> do
Value
v1 <- Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env Expr
e1
Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr ((VarName
x, Value
v1) (VarName, Value) -> Env -> Env
forall a. a -> [a] -> [a]
: Env
env) Expr
e2
callToplevelExpr :: (MonadFix m, MonadError Error m) => Env -> ToplevelExpr -> [Value] -> m Value
callToplevelExpr :: Env -> ToplevelExpr -> [Value] -> m Value
callToplevelExpr Env
env ToplevelExpr
e [Value]
args = case ToplevelExpr
e of
ToplevelLet VarName
x Type
_ Expr
e ToplevelExpr
cont -> do
Value
val <- Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env Expr
e
Env -> ToplevelExpr -> [Value] -> m Value
forall (m :: * -> *).
(MonadFix m, MonadError Error m) =>
Env -> ToplevelExpr -> [Value] -> m Value
callToplevelExpr ((VarName
x, Value
val) (VarName, Value) -> Env -> Env
forall a. a -> [a] -> [a]
: Env
env) ToplevelExpr
cont [Value]
args
ToplevelLetRec VarName
f [(VarName, Type)]
args' Type
_ Expr
body ToplevelExpr
cont -> do
Value
val <- (Value -> m Value) -> m Value
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix ((Value -> m Value) -> m Value) -> (Value -> m Value) -> m Value
forall a b. (a -> b) -> a -> b
$ \Value
val -> Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr ((VarName
f, Value
val) (VarName, Value) -> Env -> Env
forall a. a -> [a] -> [a]
: Env
env) ([(VarName, Type)] -> Expr -> Expr
curryLam [(VarName, Type)]
args' Expr
body)
Env -> ToplevelExpr -> [Value] -> m Value
forall (m :: * -> *).
(MonadFix m, MonadError Error m) =>
Env -> ToplevelExpr -> [Value] -> m Value
callToplevelExpr ((VarName
f, Value
val) (VarName, Value) -> Env -> Env
forall a. a -> [a] -> [a]
: Env
env) ToplevelExpr
cont [Value]
args
ResultExpr Expr
e -> do
Value
val <- Env -> Expr -> m Value
forall (m :: * -> *). MonadError Error m => Env -> Expr -> m Value
evaluateExpr Env
env Expr
e
Value -> [Value] -> m Value
forall (m :: * -> *).
MonadError Error m =>
Value -> [Value] -> m Value
callValue Value
val [Value]
args
callProgram :: (MonadFix m, MonadError Error m) => Program -> [Value] -> m Value
callProgram :: ToplevelExpr -> [Value] -> m Value
callProgram ToplevelExpr
prog [Value]
args = String -> m Value -> m Value
forall (m :: * -> *) a. MonadError Error m => String -> m a -> m a
wrapError' String
"Jikka.Core.Evaluate" (m Value -> m Value) -> m Value -> m Value
forall a b. (a -> b) -> a -> b
$ do
m () -> m ()
forall (m :: * -> *) a. MonadError Error m => m a -> m a
precondition (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
ToplevelExpr -> m ()
forall (m :: * -> *). MonadError Error m => ToplevelExpr -> m ()
ensureEagerlyEvaluatable ToplevelExpr
prog
ToplevelExpr -> m ()
forall (m :: * -> *). MonadError Error m => ToplevelExpr -> m ()
ensureWellTyped ToplevelExpr
prog
Env -> ToplevelExpr -> [Value] -> m Value
forall (m :: * -> *).
(MonadFix m, MonadError Error m) =>
Env -> ToplevelExpr -> [Value] -> m Value
callToplevelExpr [] ToplevelExpr
prog [Value]
args
run :: (MonadAlpha m, MonadFix m, MonadError Error m) => Program -> [Value] -> m Value
run :: ToplevelExpr -> [Value] -> m Value
run ToplevelExpr
prog [Value]
args = do
ToplevelExpr -> [Value] -> m Value
forall (m :: * -> *).
(MonadFix m, MonadError Error m) =>
ToplevelExpr -> [Value] -> m Value
callProgram ToplevelExpr
prog [Value]
args