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

module Dovetail.Core.Control.Bind where

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
"Control.Bind"

  -- arrayBind :: forall a b. Array a -> (a -> Array b) -> Array b
  ModuleName
-> Text
-> (Vector (Value ctx)
    -> (Value ctx -> Eval ctx (Vector (Value ctx)))
    -> Eval ctx (Vector (Value ctx)))
-> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @(Vector (Value ctx) -> (Value ctx -> Eval ctx (Vector (Value ctx))) -> Eval ctx (Vector (Value ctx)))
    ModuleName
_ModuleName Text
"arrayBind"
    \Vector (Value ctx)
xs Value ctx -> Eval ctx (Vector (Value ctx))
f -> [Vector (Value ctx)] -> Vector (Value ctx)
forall a. [Vector a] -> Vector a
Vector.concat ([Vector (Value ctx)] -> Vector (Value ctx))
-> Eval ctx [Vector (Value ctx)] -> Eval ctx (Vector (Value ctx))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value ctx -> Eval ctx (Vector (Value ctx)))
-> [Value ctx] -> Eval ctx [Vector (Value ctx)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value ctx -> Eval ctx (Vector (Value ctx))
f (Vector (Value ctx) -> [Value ctx]
forall a. Vector a -> [a]
Vector.toList Vector (Value ctx)
xs)