module Data.List.Match.Private where
import Data.Maybe (fromJust, isNothing, )
import Data.Maybe.HT (toMaybe, )
import Data.Tuple.HT (mapFst, forcePair, )
import Data.Bool.HT (if', )
import qualified Data.List as List
import Control.Functor.HT (void, )
import Prelude hiding (take, drop, splitAt, replicate, )
take :: [b] -> [a] -> [a]
take :: [b] -> [a] -> [a]
take = (b -> a -> a) -> [b] -> [a] -> [a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith ((a -> b -> a) -> b -> a -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> a
forall a b. a -> b -> a
const)
drop :: [b] -> [a] -> [a]
drop :: [b] -> [a] -> [a]
drop [b]
xs [a]
ys0 =
([a] -> b -> [a]) -> [a] -> [b] -> [a]
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\[a]
ys b
_ -> [a] -> [a]
forall a. [a] -> [a]
laxTail [a]
ys) [a]
ys0 [b]
xs
dropRec :: [b] -> [a] -> [a]
dropRec :: [b] -> [a] -> [a]
dropRec (b
_:[b]
xs) (a
_:[a]
ys) = [b] -> [a] -> [a]
forall b a. [b] -> [a] -> [a]
dropRec [b]
xs [a]
ys
dropRec [b]
_ [a]
ys = [a]
ys
drop0 :: [b] -> [a] -> [a]
drop0 :: [b] -> [a] -> [a]
drop0 [b]
xs [a]
ys =
(Maybe a -> a) -> [Maybe a] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map Maybe a -> a
forall a. HasCallStack => Maybe a -> a
fromJust ((Maybe a -> Bool) -> [Maybe a] -> [Maybe a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Maybe a -> Bool
forall a. Maybe a -> Bool
isNothing
(([b] -> a -> Maybe a) -> [[b]] -> [a] -> [Maybe a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Bool -> a -> Maybe a
forall a. Bool -> a -> Maybe a
toMaybe (Bool -> a -> Maybe a) -> ([b] -> Bool) -> [b] -> a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null) (([b] -> [b]) -> [b] -> [[b]]
forall a. (a -> a) -> a -> [a]
iterate [b] -> [b]
forall a. [a] -> [a]
laxTail [b]
xs) [a]
ys))
drop1 :: [b] -> [a] -> [a]
drop1 :: [b] -> [a] -> [a]
drop1 [b]
xs [a]
ys =
(([b], a) -> a) -> [([b], a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map ([b], a) -> a
forall a b. (a, b) -> b
snd ((([b], a) -> Bool) -> [([b], a)] -> [([b], a)]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Bool -> Bool
not (Bool -> Bool) -> (([b], a) -> Bool) -> ([b], a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([b] -> Bool) -> (([b], a) -> [b]) -> ([b], a) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([b], a) -> [b]
forall a b. (a, b) -> a
fst) ([[b]] -> [a] -> [([b], a)]
forall a b. [a] -> [b] -> [(a, b)]
zip (([b] -> [b]) -> [b] -> [[b]]
forall a. (a -> a) -> a -> [a]
iterate [b] -> [b]
forall a. [a] -> [a]
laxTail [b]
xs) [a]
ys))
drop2 :: [b] -> [a] -> [a]
drop2 :: [b] -> [a] -> [a]
drop2 [b]
xs [a]
ys =
([b], [a]) -> [a]
forall a b. (a, b) -> b
snd (([b], [a]) -> [a]) -> ([b], [a]) -> [a]
forall a b. (a -> b) -> a -> b
$ [([b], [a])] -> ([b], [a])
forall a. [a] -> a
head ([([b], [a])] -> ([b], [a])) -> [([b], [a])] -> ([b], [a])
forall a b. (a -> b) -> a -> b
$
(([b], [a]) -> Bool) -> [([b], [a])] -> [([b], [a])]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile (Bool -> Bool
not (Bool -> Bool) -> (([b], [a]) -> Bool) -> ([b], [a]) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [b] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([b] -> Bool) -> (([b], [a]) -> [b]) -> ([b], [a]) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([b], [a]) -> [b]
forall a b. (a, b) -> a
fst) ([([b], [a])] -> [([b], [a])]) -> [([b], [a])] -> [([b], [a])]
forall a b. (a -> b) -> a -> b
$
[[b]] -> [[a]] -> [([b], [a])]
forall a b. [a] -> [b] -> [(a, b)]
zip (([b] -> [b]) -> [b] -> [[b]]
forall a. (a -> a) -> a -> [a]
iterate [b] -> [b]
forall a. [a] -> [a]
laxTail [b]
xs) (([a] -> [a]) -> [a] -> [[a]]
forall a. (a -> a) -> a -> [a]
iterate [a] -> [a]
forall a. [a] -> [a]
laxTail [a]
ys)
laxTail :: [a] -> [a]
laxTail :: [a] -> [a]
laxTail [a]
xt = case [a]
xt of [] -> []; a
_:[a]
xs -> [a]
xs
laxTail0 :: [a] -> [a]
laxTail0 :: [a] -> [a]
laxTail0 = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
List.drop Int
1
splitAt :: [b] -> [a] -> ([a],[a])
splitAt :: [b] -> [a] -> ([a], [a])
splitAt [b]
nt [a]
xt =
([a], [a]) -> ([a], [a])
forall a b. (a, b) -> (a, b)
forcePair (([a], [a]) -> ([a], [a])) -> ([a], [a]) -> ([a], [a])
forall a b. (a -> b) -> a -> b
$
case ([b]
nt,[a]
xt) of
(b
_:[b]
ns, a
x:[a]
xs) -> ([a] -> [a]) -> ([a], [a]) -> ([a], [a])
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], [a]) -> ([a], [a])) -> ([a], [a]) -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ [b] -> [a] -> ([a], [a])
forall b a. [b] -> [a] -> ([a], [a])
splitAt [b]
ns [a]
xs
([b]
_, [a]
xs) -> ([],[a]
xs)
takeRev :: [b] -> [a] -> [a]
takeRev :: [b] -> [a] -> [a]
takeRev [b]
ys [a]
xs = [a] -> [a] -> [a]
forall b a. [b] -> [a] -> [a]
drop ([b] -> [a] -> [a]
forall b a. [b] -> [a] -> [a]
drop [b]
ys [a]
xs) [a]
xs
dropRev :: [b] -> [a] -> [a]
dropRev :: [b] -> [a] -> [a]
dropRev [b]
ys [a]
xs = [a] -> [a] -> [a]
forall b a. [b] -> [a] -> [a]
take ([b] -> [a] -> [a]
forall b a. [b] -> [a] -> [a]
drop [b]
ys [a]
xs) [a]
xs
equalLength :: [a] -> [b] -> Bool
equalLength :: [a] -> [b] -> Bool
equalLength [a]
xs [b]
ys =
[a] -> [()]
forall (f :: * -> *) a. Functor f => f a -> f ()
void [a]
xs [()] -> [()] -> Bool
forall a. Eq a => a -> a -> Bool
== [b] -> [()]
forall (f :: * -> *) a. Functor f => f a -> f ()
void [b]
ys
compareLength :: [a] -> [b] -> Ordering
compareLength :: [a] -> [b] -> Ordering
compareLength [a]
xs [b]
ys =
[()] -> [()] -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ([a] -> [()]
forall (f :: * -> *) a. Functor f => f a -> f ()
void [a]
xs) ([b] -> [()]
forall (f :: * -> *) a. Functor f => f a -> f ()
void [b]
ys)
compareLength0 :: [a] -> [b] -> Ordering
compareLength0 :: [a] -> [b] -> Ordering
compareLength0 =
let recourse :: [a] -> [a] -> Ordering
recourse (a
_:[a]
xs) (a
_:[a]
ys) = [a] -> [a] -> Ordering
recourse [a]
xs [a]
ys
recourse [] [] = Ordering
EQ
recourse (a
_:[a]
_) [] = Ordering
GT
recourse [] (a
_:[a]
_) = Ordering
LT
in [a] -> [b] -> Ordering
forall a a. [a] -> [a] -> Ordering
recourse
compareLength1 :: [a] -> [b] -> Ordering
compareLength1 :: [a] -> [b] -> Ordering
compareLength1 [a]
xs [b]
ys =
Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) ([b] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [b]
ys)
lessOrEqualLength :: [a] -> [b] -> Bool
lessOrEqualLength :: [a] -> [b] -> Bool
lessOrEqualLength [] [b]
_ = Bool
True
lessOrEqualLength [a]
_ [] = Bool
False
lessOrEqualLength (a
_:[a]
xs) (b
_:[b]
ys) = [a] -> [b] -> Bool
forall a b. [a] -> [b] -> Bool
lessOrEqualLength [a]
xs [b]
ys
shorterList :: [a] -> [a] -> [a]
shorterList :: [a] -> [a] -> [a]
shorterList [a]
xs [a]
ys =
let useX :: Bool
useX = [a] -> [a] -> Bool
forall a b. [a] -> [b] -> Bool
lessOrEqualLength [a]
xs [a]
ys
in (a -> a -> a) -> [a] -> [a] -> [a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (Bool -> a -> a -> a
forall a. Bool -> a -> a -> a
if' Bool
useX) [a]
xs [a]
ys
shorterListEq :: (Eq a) => [a] -> [a] -> [a]
shorterListEq :: [a] -> [a] -> [a]
shorterListEq [a]
xs [a]
ys =
let useX :: Bool
useX = [a] -> [a] -> Bool
forall a b. [a] -> [b] -> Bool
lessOrEqualLength [a]
xs [a]
ys
in (a -> a -> a) -> [a] -> [a] -> [a]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\a
x a
y -> Bool -> a -> a -> a
forall a. Bool -> a -> a -> a
if' (a
xa -> a -> Bool
forall a. Eq a => a -> a -> Bool
==a
y Bool -> Bool -> Bool
|| Bool
useX) a
x a
y) [a]
xs [a]
ys
replicate :: [a] -> b -> [b]
replicate :: [a] -> b -> [b]
replicate [a]
xs b
y =
[a] -> [b] -> [b]
forall b a. [b] -> [a] -> [a]
take [a]
xs (b -> [b]
forall a. a -> [a]
repeat b
y)