module Language.Copilot.Libs.Indexes(soonest, soonestFail, latest, latestFail) where
import Prelude (id, Int, String, fromIntegral, ($))
import qualified Prelude as P
import Data.Int (Int16)
import Language.Copilot.Libs.ErrorChks
import Language.Copilot.Core
import Language.Copilot.Language
soonestHlp :: String -> (Spec Bool -> Spec Bool) -> Int -> Spec Bool -> Spec Int16
soonestHlp name f n s = int16Chk name n $ nPosChk name n (buildStr 0)
where buildStr m =
if m P.> n then (1)
else mux (f $ drop m s) (fI m) (buildStr (m P.+ 1))
fI = fromIntegral
latestHlp :: String -> (Spec Bool -> Spec Bool) -> Int -> Spec Bool -> Spec Int16
latestHlp name f n s = int16Chk name n $ nPosChk name n (buildStr n)
where buildStr m =
if m P.< 0 then (1)
else mux (f $ drop m s) (fI m) (buildStr (m P.- 1))
fI = fromIntegral
soonest :: Int -> Spec Bool -> Spec Int16
soonest = soonestHlp "soonest" id
soonestFail :: Int -> Spec Bool -> Spec Int16
soonestFail = soonestHlp "soonestFail" not
latest :: Int -> Spec Bool -> Spec Int16
latest = latestHlp "latest" id
latestFail :: Int -> Spec Bool -> Spec Int16
latestFail = latestHlp "latest" not