{-# LANGUAGE FlexibleContexts #-}

module Opaleye.Internal.Lateral
  ( lateral
  , viaLateral
  , laterally
  , bilaterally
  , bind
  , arrowApply
  )
where

import           Opaleye.Internal.QueryArr

-- | Lifts operations like 'Opaleye.Aggregate.aggregate',
-- 'Opaleye.Order.orderBy' and 'Opaleye.Order.limit', which are restricted to
-- 'Select' normally, to operate on 'SelectArr's taking arbitrary inputs.
laterally :: (Select a -> Select b) -> SelectArr i a -> SelectArr i b
laterally :: (Select a -> Select b) -> SelectArr i a -> SelectArr i b
laterally Select a -> Select b
f SelectArr i a
as = (i -> Select b) -> SelectArr i b
forall i a. (i -> Select a) -> SelectArr i a
lateral (\i
i -> Select a -> Select b
f (SelectArr i a -> i -> Select a
forall i a. SelectArr i a -> i -> Select a
viaLateral SelectArr i a
as i
i))


-- | Lifts operations like 'Opaleye.Binary.union', 'Opaleye.Binary.intersect'
-- and 'Opaleye.Binary.except', which are restricted to 'Select' normally, to
-- operate on 'SelectArr's taking arbitrary inputs.
bilaterally :: (Select a -> Select b -> Select c)
            -> SelectArr i a -> SelectArr i b -> SelectArr i c
bilaterally :: (Select a -> Select b -> Select c)
-> SelectArr i a -> SelectArr i b -> SelectArr i c
bilaterally Select a -> Select b -> Select c
f SelectArr i a
as SelectArr i b
bs = (i -> Select c) -> SelectArr i c
forall i a. (i -> Select a) -> SelectArr i a
lateral (\i
i -> Select a -> Select b -> Select c
f (SelectArr i a -> i -> Select a
forall i a. SelectArr i a -> i -> Select a
viaLateral SelectArr i a
as i
i) (SelectArr i b -> i -> Select b
forall i a. SelectArr i a -> i -> Select a
viaLateral SelectArr i b
bs i
i))