{-# LANGUAGE BlockArguments      #-}
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE OverloadedStrings   #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications    #-}
{-# LANGUAGE ViewPatterns        #-}

module Dovetail.Core.Data.Foldable where

import Data.Foldable (fold)
import Data.Vector (Vector)
import Data.Vector qualified as Vector
import Dovetail
import Dovetail.Evaluate (builtIn)

env :: forall ctx. Env ctx
env :: Env ctx
env = do
  let _ModuleName :: ModuleName
_ModuleName = Text -> ModuleName
ModuleName Text
"Data.Foldable"

  [Env ctx] -> Env ctx
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold
    [ -- foldrArray :: forall a b. (a -> b -> b) -> b -> Array a -> b
      ModuleName
-> Text
-> ((Value ctx -> Value ctx -> Eval ctx (Value ctx))
    -> Value ctx -> Vector (Value ctx) -> Eval ctx (Value ctx))
-> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @((Value ctx -> Value ctx -> Eval ctx (Value ctx)) -> Value ctx -> Vector (Value ctx) -> Eval ctx (Value ctx))
        ModuleName
_ModuleName Text
"foldrArray"
        \Value ctx -> Value ctx -> Eval ctx (Value ctx)
f Value ctx
b -> (Value ctx -> Value ctx -> Eval ctx (Value ctx))
-> Value ctx -> Vector (Value ctx) -> Eval ctx (Value ctx)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Vector b -> m a
Vector.foldM ((Value ctx -> Value ctx -> Eval ctx (Value ctx))
-> Value ctx -> Value ctx -> Eval ctx (Value ctx)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Value ctx -> Value ctx -> Eval ctx (Value ctx)
f) Value ctx
b (Vector (Value ctx) -> Eval ctx (Value ctx))
-> (Vector (Value ctx) -> Vector (Value ctx))
-> Vector (Value ctx)
-> Eval ctx (Value ctx)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector (Value ctx) -> Vector (Value ctx)
forall a. Vector a -> Vector a
Vector.reverse
      -- foldlArray :: forall a b. (b -> a -> b) -> b -> Array a -> b
    , ModuleName
-> Text
-> ((Value ctx -> Value ctx -> Eval ctx (Value ctx))
    -> Value ctx -> Vector (Value ctx) -> Eval ctx (Value ctx))
-> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @((Value ctx -> Value ctx -> Eval ctx (Value ctx)) -> Value ctx -> Vector (Value ctx) -> Eval ctx (Value ctx))
        ModuleName
_ModuleName Text
"foldlArray"
        (Value ctx -> Value ctx -> Eval ctx (Value ctx))
-> Value ctx -> Vector (Value ctx) -> Eval ctx (Value ctx)
forall (m :: * -> *) a b.
Monad m =>
(a -> b -> m a) -> a -> Vector b -> m a
Vector.foldM 
    ]