{-|
    Module      :  AERN2.Select
    Description :  multivalued select operation
    Copyright   :  (c) Michal Konecny
    License     :  BSD3

    Maintainer  :  mikkonecny@gmail.com
    Stability   :  experimental
    Portability :  portable

    Generic multivalued select operation
-}
module AERN2.Select where

import MixedTypesNumPrelude

import qualified Numeric.CollectErrors as CN

import AERN2.Kleenean

class (IsBool (SelectType k)) => CanSelect k where
  {-| Must be Bool or similar -}
  type SelectType k 
  {-|
    Execute two lazy computations "in parallel" until one of them succeeds. 
  -}
  select :: k -> k -> (SelectType k) {-^ True means that the first computation succeeded. -}

type CanSelectBool k = (CanSelect k, SelectType k ~ Bool)
type CanSelectCNBool k = (CanSelect k, SelectType k ~ CN Bool)

instance CanSelect Kleenean where
  type SelectType Kleenean = CN Bool
  select :: Kleenean -> Kleenean -> SelectType Kleenean
select Kleenean
CertainTrue Kleenean
_ = forall v. v -> CN v
cn Bool
True
  select Kleenean
_ Kleenean
CertainTrue = forall v. v -> CN v
cn Bool
False
  select Kleenean
CertainFalse Kleenean
CertainFalse =
    forall v. NumError -> CN v
CN.noValueNumErrorPotential forall a b. (a -> b) -> a -> b
$ 
      String -> NumError
CN.NumError String
"select (Kleenean): Both branches failed!"
  select Kleenean
_ Kleenean
_ = 
    forall v. NumError -> CN v
CN.noValueNumErrorPotential forall a b. (a -> b) -> a -> b
$ 
      String -> NumError
CN.NumError String
"select (Kleenean): Insufficient information to determine selection."

instance CanSelect (CN Kleenean) where
  type SelectType (CN Kleenean) = CN Bool
  select :: CN Kleenean -> CN Kleenean -> SelectType (CN Kleenean)
select CN Kleenean
cnk1 CN Kleenean
cnk2 =
    do
    Kleenean
k1 <- CN Kleenean
cnk1
    Kleenean
k2 <- CN Kleenean
cnk2
    forall k. CanSelect k => k -> k -> SelectType k
select Kleenean
k1 Kleenean
k2

class CanSelectCountable k where
  {-| Must be Integer or similar -}
  type SelectCountableType k 
  {-|
    Execute a sequence of lazy computations "in parallel" until one of them succeeds
    and return the index of a succeeding computation. 
  -}
  selectCountable :: (Integer -> k) -> SelectCountableType k

type CanSelectCountableInteger k = (CanSelectCountable k, SelectCountableType k ~ Integer)
type CanSelectCountableCNInteger k = (CanSelectCountable k, SelectCountableType k ~ CN Integer)

instance CanSelectCountable Kleenean where
  type SelectCountableType Kleenean = Integer
  selectCountable :: (Integer -> Kleenean) -> SelectCountableType Kleenean
selectCountable Integer -> Kleenean
kleeneans = Integer -> Integer
aux Integer
0
    where
    aux :: Integer -> Integer
aux Integer
i = case Integer -> Kleenean
kleeneans Integer
i of
      Kleenean
CertainTrue -> Integer
i
      Kleenean
_ -> Integer -> Integer
aux (Integer
i forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ Integer
1)

instance CanSelectCountable (CN Kleenean) where
  type SelectCountableType (CN Kleenean) = CN Integer
  selectCountable :: (Integer -> CN Kleenean) -> SelectCountableType (CN Kleenean)
selectCountable Integer -> CN Kleenean
cnkleeneans = Integer -> CollectErrors NumErrors Integer
aux Integer
0
    where
    aux :: Integer -> CollectErrors NumErrors Integer
aux Integer
i = do
      Kleenean
ki <- Integer -> CN Kleenean
cnkleeneans Integer
i
      case Kleenean
ki of
        Kleenean
CertainTrue -> forall v. v -> CN v
cn Integer
i
        Kleenean
_ -> Integer -> CollectErrors NumErrors Integer
aux (Integer
i forall t1 t2. CanAddAsymmetric t1 t2 => t1 -> t2 -> AddType t1 t2
+ Integer
1)