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

module Dovetail.Core.Data.Unfoldable where

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

-- unfoldrArrayImpl
--   :: forall a b
--    . (forall x. Maybe x -> Boolean)
--   -> (forall x. Maybe x -> x)
--   -> (forall x y. Tuple x y -> x)
--   -> (forall x y. Tuple x y -> y)
--   -> (b -> Maybe (Tuple a b))
--   -> b
--   -> Array a
unfoldrArrayImpl 
  :: (Value ctx -> Eval ctx Bool)
  -> (Value ctx -> Eval ctx (Value ctx))
  -> (Value ctx -> Eval ctx (Value ctx))
  -> (Value ctx -> Eval ctx (Value ctx))
  -> (Value ctx -> Eval ctx (Value ctx)) 
  -> Value ctx 
  -> Eval ctx (Vector (Value ctx))
unfoldrArrayImpl :: (Value ctx -> Eval ctx Bool)
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> Value ctx
-> Eval ctx (Vector (Value ctx))
unfoldrArrayImpl Value ctx -> Eval ctx Bool
_isNothing Value ctx -> Eval ctx (Value ctx)
_fromJust Value ctx -> Eval ctx (Value ctx)
_fst Value ctx -> Eval ctx (Value ctx)
_snd Value ctx -> Eval ctx (Value ctx)
_f Value ctx
_x = do
  let toMaybe :: Value ctx -> Eval ctx (Maybe (Value ctx))
toMaybe Value ctx
x = Value ctx -> Eval ctx Bool
_isNothing Value ctx
x Eval ctx Bool
-> (Bool -> Eval ctx (Maybe (Value ctx)))
-> Eval ctx (Maybe (Value ctx))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Bool
b -> if Bool
b then Maybe (Value ctx) -> Eval ctx (Maybe (Value ctx))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Value ctx)
forall a. Maybe a
Nothing else Value ctx -> Maybe (Value ctx)
forall a. a -> Maybe a
Just (Value ctx -> Maybe (Value ctx))
-> Eval ctx (Value ctx) -> Eval ctx (Maybe (Value ctx))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value ctx -> Eval ctx (Value ctx)
_fromJust Value ctx
x
      toTuple :: Value ctx -> Eval ctx (Value ctx, Value ctx)
toTuple Value ctx
x = (,) (Value ctx -> Value ctx -> (Value ctx, Value ctx))
-> Eval ctx (Value ctx)
-> Eval ctx (Value ctx -> (Value ctx, Value ctx))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Value ctx -> Eval ctx (Value ctx)
_fst Value ctx
x Eval ctx (Value ctx -> (Value ctx, Value ctx))
-> Eval ctx (Value ctx) -> Eval ctx (Value ctx, Value ctx)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Value ctx -> Eval ctx (Value ctx)
_snd Value ctx
x
  (Value ctx -> Eval ctx (Maybe (Value ctx, Value ctx)))
-> Value ctx -> Eval ctx (Vector (Value ctx))
forall (m :: * -> *) b a.
Monad m =>
(b -> m (Maybe (a, b))) -> b -> m (Vector a)
Vector.unfoldrM (\Value ctx
b -> do 
    Value ctx
x <- Value ctx -> Eval ctx (Value ctx)
_f Value ctx
b 
    Maybe (Value ctx)
m <- Value ctx -> Eval ctx (Maybe (Value ctx))
toMaybe Value ctx
x
    (Value ctx -> Eval ctx (Value ctx, Value ctx))
-> Maybe (Value ctx) -> Eval ctx (Maybe (Value ctx, Value ctx))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse Value ctx -> Eval ctx (Value ctx, Value ctx)
toTuple Maybe (Value ctx)
m) Value ctx
_x

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

  ModuleName
-> Text
-> ((Value ctx -> Eval ctx Bool)
    -> (Value ctx -> Eval ctx (Value ctx))
    -> (Value ctx -> Eval ctx (Value ctx))
    -> (Value ctx -> Eval ctx (Value ctx))
    -> (Value ctx -> Eval ctx (Value ctx))
    -> Value ctx
    -> Eval ctx (Vector (Value ctx)))
-> Env ctx
forall ctx a. ToValue ctx a => ModuleName -> Text -> a -> Env ctx
builtIn @ctx @_
    ModuleName
_ModuleName Text
"unfoldrArrayImpl"
    (Value ctx -> Eval ctx Bool)
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> Value ctx
-> Eval ctx (Vector (Value ctx))
forall ctx.
(Value ctx -> Eval ctx Bool)
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> (Value ctx -> Eval ctx (Value ctx))
-> Value ctx
-> Eval ctx (Vector (Value ctx))
unfoldrArrayImpl