module Agda.Utils.List where
import Control.Monad (filterM)
import Data.Array (Array, array, listArray)
import qualified Data.Array as Array
import Data.Bifunctor
import Data.Function
import qualified Data.List as List
import qualified Data.List.NonEmpty as List1
import Data.List.NonEmpty (pattern (:|), (<|))
import Data.Maybe
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Agda.Utils.Bag as Bag
import Agda.Utils.Function (applyWhen)
import Agda.Utils.Functor ((<.>))
import Agda.Utils.Tuple
import {-# SOURCE #-} Agda.Utils.List1 (List1)
import Agda.Utils.Impossible
snoc :: [a] -> a -> [a]
snoc :: [a] -> a -> [a]
snoc [a]
xs a
x = [a]
xs [a] -> [a] -> [a]
forall a. [a] -> [a] -> [a]
++ [a
x]
caseList :: [a] -> b -> (a -> [a] -> b) -> b
caseList :: [a] -> b -> (a -> [a] -> b) -> b
caseList [a]
xs b
n a -> [a] -> b
c = b -> (a -> [a] -> b) -> [a] -> b
forall b a. b -> (a -> [a] -> b) -> [a] -> b
listCase b
n a -> [a] -> b
c [a]
xs
caseListM :: Monad m => m [a] -> m b -> (a -> [a] -> m b) -> m b
caseListM :: m [a] -> m b -> (a -> [a] -> m b) -> m b
caseListM m [a]
mxs m b
n a -> [a] -> m b
c = m b -> (a -> [a] -> m b) -> [a] -> m b
forall b a. b -> (a -> [a] -> b) -> [a] -> b
listCase m b
n a -> [a] -> m b
c ([a] -> m b) -> m [a] -> m b
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< m [a]
mxs
listCase :: b -> (a -> [a] -> b) -> [a] -> b
listCase :: b -> (a -> [a] -> b) -> [a] -> b
listCase b
n a -> [a] -> b
c [] = b
n
listCase b
n a -> [a] -> b
c (a
x:[a]
xs) = a -> [a] -> b
c a
x [a]
xs
headWithDefault :: a -> [a] -> a
headWithDefault :: a -> [a] -> a
headWithDefault a
def = a -> Maybe a -> a
forall a. a -> Maybe a -> a
fromMaybe a
def (Maybe a -> a) -> ([a] -> Maybe a) -> [a] -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe a
forall a. [a] -> Maybe a
listToMaybe
tailMaybe :: [a] -> Maybe [a]
tailMaybe :: [a] -> Maybe [a]
tailMaybe = ((a, [a]) -> [a]) -> Maybe (a, [a]) -> Maybe [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, [a]) -> [a]
forall a b. (a, b) -> b
snd (Maybe (a, [a]) -> Maybe [a])
-> ([a] -> Maybe (a, [a])) -> [a] -> Maybe [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe (a, [a])
forall a. [a] -> Maybe (a, [a])
uncons
tailWithDefault :: [a] -> [a] -> [a]
tailWithDefault :: [a] -> [a] -> [a]
tailWithDefault [a]
def = [a] -> Maybe [a] -> [a]
forall a. a -> Maybe a -> a
fromMaybe [a]
def (Maybe [a] -> [a]) -> ([a] -> Maybe [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Maybe [a]
forall a. [a] -> Maybe [a]
tailMaybe
lastMaybe :: [a] -> Maybe a
lastMaybe :: [a] -> Maybe a
lastMaybe [] = Maybe a
forall a. Maybe a
Nothing
lastMaybe [a]
xs = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> a -> Maybe a
forall a b. (a -> b) -> a -> b
$ [a] -> a
forall a. [a] -> a
last [a]
xs
lastWithDefault :: a -> [a] -> a
lastWithDefault :: a -> [a] -> a
lastWithDefault = a -> [a] -> a
forall a. a -> [a] -> a
last1
last1 :: a -> [a] -> a
last1 :: a -> [a] -> a
last1 a
a = \case
[] -> a
a
a
b:[a]
bs -> a -> [a] -> a
forall a. a -> [a] -> a
last1 a
b [a]
bs
last2 :: [a] -> Maybe (a, a)
last2 :: [a] -> Maybe (a, a)
last2 (a
x : a
y : [a]
xs) = (a, a) -> Maybe (a, a)
forall a. a -> Maybe a
Just ((a, a) -> Maybe (a, a)) -> (a, a) -> Maybe (a, a)
forall a b. (a -> b) -> a -> b
$ a -> a -> [a] -> (a, a)
forall t. t -> t -> [t] -> (t, t)
loop a
x a
y [a]
xs
where
loop :: t -> t -> [t] -> (t, t)
loop t
x t
y [] = (t
x, t
y)
loop t
x t
y (t
z:[t]
xs) = t -> t -> [t] -> (t, t)
loop t
y t
z [t]
xs
last2 [a]
_ = Maybe (a, a)
forall a. Maybe a
Nothing
uncons :: [a] -> Maybe (a, [a])
uncons :: [a] -> Maybe (a, [a])
uncons [] = Maybe (a, [a])
forall a. Maybe a
Nothing
uncons (a
x:[a]
xs) = (a, [a]) -> Maybe (a, [a])
forall a. a -> Maybe a
Just (a
x,[a]
xs)
mcons :: Maybe a -> [a] -> [a]
mcons :: Maybe a -> [a] -> [a]
mcons Maybe a
ma [a]
as = [a] -> (a -> [a]) -> Maybe a -> [a]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [a]
as (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
as) Maybe a
ma
initLast :: [a] -> Maybe ([a],a)
initLast :: [a] -> Maybe ([a], a)
initLast [] = Maybe ([a], a)
forall a. Maybe a
Nothing
initLast (a
a:[a]
as) = ([a], a) -> Maybe ([a], a)
forall a. a -> Maybe a
Just (([a], a) -> Maybe ([a], a)) -> ([a], a) -> Maybe ([a], a)
forall a b. (a -> b) -> a -> b
$ a -> [a] -> ([a], a)
forall a. a -> [a] -> ([a], a)
initLast1 a
a [a]
as
initLast1 :: a -> [a] -> ([a], a)
initLast1 :: a -> [a] -> ([a], a)
initLast1 a
a = \case
[] -> ([], a
a)
a
b:[a]
bs -> ([a] -> [a]) -> ([a], a) -> ([a], a)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], a) -> ([a], a)) -> ([a], a) -> ([a], a)
forall a b. (a -> b) -> a -> b
$ a -> [a] -> ([a], a)
forall a. a -> [a] -> ([a], a)
initLast1 a
b [a]
bs
init1 :: a -> [a] -> [a]
init1 :: a -> [a] -> [a]
init1 a
a = \case
[] -> []
a
b:[a]
bs -> a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a] -> [a]
forall a. a -> [a] -> [a]
init1 a
b [a]
bs
initMaybe :: [a] -> Maybe [a]
initMaybe :: [a] -> Maybe [a]
initMaybe = \case
[] -> Maybe [a]
forall a. Maybe a
Nothing
a
a:[a]
as -> [a] -> Maybe [a]
forall a. a -> Maybe a
Just ([a] -> Maybe [a]) -> [a] -> Maybe [a]
forall a b. (a -> b) -> a -> b
$ a -> [a] -> [a]
forall a. a -> [a] -> [a]
init1 a
a [a]
as
initWithDefault :: [a] -> [a] -> [a]
initWithDefault :: [a] -> [a] -> [a]
initWithDefault [a]
as [] = [a]
as
initWithDefault [a]
_ (a
a:[a]
as) = a -> [a] -> [a]
forall a. a -> [a] -> [a]
init1 a
a [a]
as
(!!!) :: [a] -> Int -> Maybe a
[] !!! :: [a] -> Int -> Maybe a
!!! Int
_ = Maybe a
forall a. Maybe a
Nothing
(a
x : [a]
_) !!! Int
0 = a -> Maybe a
forall a. a -> Maybe a
Just a
x
(a
_ : [a]
xs) !!! Int
n = [a]
xs [a] -> Int -> Maybe a
forall a. [a] -> Int -> Maybe a
!!! (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
indexWithDefault :: a -> [a] -> Int -> a
indexWithDefault :: a -> [a] -> Int -> a
indexWithDefault a
a [] Int
_ = a
a
indexWithDefault a
a (a
x : [a]
_) Int
0 = a
x
indexWithDefault a
a (a
_ : [a]
xs) Int
n = a -> [a] -> Int -> a
forall a. a -> [a] -> Int -> a
indexWithDefault a
a [a]
xs (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
findWithIndex :: (a -> Bool) -> [a] -> Maybe (a, Int)
findWithIndex :: (a -> Bool) -> [a] -> Maybe (a, Int)
findWithIndex a -> Bool
p [a]
as = ((a, Int) -> Bool) -> [(a, Int)] -> Maybe (a, Int)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
List.find (a -> Bool
p (a -> Bool) -> ((a, Int) -> a) -> (a, Int) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, Int) -> a
forall a b. (a, b) -> a
fst) ([a] -> [Int] -> [(a, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [a]
as [Int
0..])
genericElemIndex :: (Eq a, Integral i) => a -> [a] -> Maybe i
genericElemIndex :: a -> [a] -> Maybe i
genericElemIndex a
x [a]
xs =
[i] -> Maybe i
forall a. [a] -> Maybe a
listToMaybe ([i] -> Maybe i) -> [i] -> Maybe i
forall a b. (a -> b) -> a -> b
$
((i, Bool) -> i) -> [(i, Bool)] -> [i]
forall a b. (a -> b) -> [a] -> [b]
map (i, Bool) -> i
forall a b. (a, b) -> a
fst ([(i, Bool)] -> [i]) -> [(i, Bool)] -> [i]
forall a b. (a -> b) -> a -> b
$
((i, Bool) -> Bool) -> [(i, Bool)] -> [(i, Bool)]
forall a. (a -> Bool) -> [a] -> [a]
filter (i, Bool) -> Bool
forall a b. (a, b) -> b
snd ([(i, Bool)] -> [(i, Bool)]) -> [(i, Bool)] -> [(i, Bool)]
forall a b. (a -> b) -> a -> b
$
[i] -> [Bool] -> [(i, Bool)]
forall a b. [a] -> [b] -> [(a, b)]
zip [i
0..] ([Bool] -> [(i, Bool)]) -> [Bool] -> [(i, Bool)]
forall a b. (a -> b) -> a -> b
$
(a -> Bool) -> [a] -> [Bool]
forall a b. (a -> b) -> [a] -> [b]
map (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x) [a]
xs
downFrom :: Integral a => a -> [a]
downFrom :: a -> [a]
downFrom a
n | a
n a -> a -> Bool
forall a. Ord a => a -> a -> Bool
<= a
0 = []
| Bool
otherwise = let n' :: a
n' = a
na -> a -> a
forall a. Num a => a -> a -> a
-a
1 in a
n' a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a]
forall a. Integral a => a -> [a]
downFrom a
n'
updateHead :: (a -> a) -> [a] -> [a]
updateHead :: (a -> a) -> [a] -> [a]
updateHead a -> a
_ [] = []
updateHead a -> a
f (a
a : [a]
as) = a -> a
f a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
as
updateLast :: (a -> a) -> [a] -> [a]
updateLast :: (a -> a) -> [a] -> [a]
updateLast a -> a
_ [] = []
updateLast a -> a
f (a
a : [a]
as) = a -> [a] -> [a]
loop a
a [a]
as
where
loop :: a -> [a] -> [a]
loop a
a [] = [a -> a
f a
a]
loop a
a (a
b : [a]
bs) = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: a -> [a] -> [a]
loop a
b [a]
bs
updateAt :: Int -> (a -> a) -> [a] -> [a]
updateAt :: Int -> (a -> a) -> [a] -> [a]
updateAt Int
_ a -> a
_ [] = []
updateAt Int
0 a -> a
f (a
a : [a]
as) = a -> a
f a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
as
updateAt Int
n a -> a
f (a
a : [a]
as) = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Int -> (a -> a) -> [a] -> [a]
forall a. Int -> (a -> a) -> [a] -> [a]
updateAt (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) a -> a
f [a]
as
type Prefix a = [a]
type Suffix a = [a]
splitExactlyAt :: Integral n => n -> [a] -> Maybe (Prefix a, Suffix a)
splitExactlyAt :: n -> [a] -> Maybe ([a], [a])
splitExactlyAt n
0 [a]
xs = ([a], [a]) -> Maybe ([a], [a])
forall (m :: * -> *) a. Monad m => a -> m a
return ([], [a]
xs)
splitExactlyAt n
n [] = Maybe ([a], [a])
forall a. Maybe a
Nothing
splitExactlyAt n
n (a
x : [a]
xs) = ([a] -> [a]) -> ([a], [a]) -> ([a], [a])
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], [a]) -> ([a], [a])) -> Maybe ([a], [a]) -> Maybe ([a], [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> n -> [a] -> Maybe ([a], [a])
forall n a. Integral n => n -> [a] -> Maybe ([a], [a])
splitExactlyAt (n
nn -> n -> n
forall a. Num a => a -> a -> a
-n
1) [a]
xs
dropEnd :: forall a. Int -> [a] -> Prefix a
dropEnd :: Int -> [a] -> [a]
dropEnd Int
n = (Int, [a]) -> [a]
forall a b. (a, b) -> b
snd ((Int, [a]) -> [a]) -> ([a] -> (Int, [a])) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (Int, [a]) -> (Int, [a])) -> (Int, [a]) -> [a] -> (Int, [a])
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> (Int, [a]) -> (Int, [a])
f (Int
n, [])
where
f :: a -> (Int, [a]) -> (Int, [a])
f :: a -> (Int, [a]) -> (Int, [a])
f a
x (Int
n, [a]
xs) = (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1, Bool -> ([a] -> [a]) -> [a] -> [a]
forall a. Bool -> (a -> a) -> a -> a
applyWhen (Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0) (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) [a]
xs)
spanEnd :: forall a. (a -> Bool) -> [a] -> (Prefix a, Suffix a)
spanEnd :: (a -> Bool) -> [a] -> ([a], [a])
spanEnd a -> Bool
p = (Bool, ([a], [a])) -> ([a], [a])
forall a b. (a, b) -> b
snd ((Bool, ([a], [a])) -> ([a], [a]))
-> ([a] -> (Bool, ([a], [a]))) -> [a] -> ([a], [a])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (Bool, ([a], [a])) -> (Bool, ([a], [a])))
-> (Bool, ([a], [a])) -> [a] -> (Bool, ([a], [a]))
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> (Bool, ([a], [a])) -> (Bool, ([a], [a]))
f (Bool
True, ([], []))
where
f :: a -> (Bool, ([a], [a])) -> (Bool, ([a], [a]))
f :: a -> (Bool, ([a], [a])) -> (Bool, ([a], [a]))
f a
x (Bool
b', ([a]
xs, [a]
ys)) = (Bool
b, if Bool
b then ([a]
xs, a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys) else (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs, [a]
ys))
where b :: Bool
b = Bool
b' Bool -> Bool -> Bool
&& a -> Bool
p a
x
breakAfter1 :: (a -> Bool) -> a -> [a] -> (List1 a, [a])
breakAfter1 :: (a -> Bool) -> a -> [a] -> (List1 a, [a])
breakAfter1 a -> Bool
p = a -> [a] -> (List1 a, [a])
loop
where
loop :: a -> [a] -> (List1 a, [a])
loop a
x = \case
xs :: [a]
xs@[] -> (a
x a -> [a] -> List1 a
forall a. a -> [a] -> NonEmpty a
:| [], [a]
xs)
xs :: [a]
xs@(a
y : [a]
ys)
| a -> Bool
p a
x -> (a
x a -> [a] -> List1 a
forall a. a -> [a] -> NonEmpty a
:| [], [a]
xs)
| Bool
otherwise -> let (List1 a
vs, [a]
ws) = a -> [a] -> (List1 a, [a])
loop a
y [a]
ys in (a
x a -> List1 a -> List1 a
forall a. a -> NonEmpty a -> NonEmpty a
<| List1 a
vs, [a]
ws)
breakAfter :: (a -> Bool) -> [a] -> ([a], [a])
breakAfter :: (a -> Bool) -> [a] -> ([a], [a])
breakAfter a -> Bool
p = \case
[] -> ([], [])
a
x:[a]
xs -> (NonEmpty a -> [a]) -> (NonEmpty a, [a]) -> ([a], [a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first NonEmpty a -> [a]
forall a. NonEmpty a -> [a]
List1.toList ((NonEmpty a, [a]) -> ([a], [a]))
-> (NonEmpty a, [a]) -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ (a -> Bool) -> a -> [a] -> (NonEmpty a, [a])
forall a. (a -> Bool) -> a -> [a] -> (List1 a, [a])
breakAfter1 a -> Bool
p a
x [a]
xs
takeWhileJust :: (a -> Maybe b) -> [a] -> Prefix b
takeWhileJust :: (a -> Maybe b) -> [a] -> Prefix b
takeWhileJust a -> Maybe b
p = [a] -> Prefix b
loop
where
loop :: [a] -> Prefix b
loop (a
a : [a]
as) | Just b
b <- a -> Maybe b
p a
a = b
b b -> Prefix b -> Prefix b
forall a. a -> [a] -> [a]
: [a] -> Prefix b
loop [a]
as
loop [a]
_ = []
spanJust :: (a -> Maybe b) -> [a] -> (Prefix b, Suffix a)
spanJust :: (a -> Maybe b) -> [a] -> (Prefix b, [a])
spanJust a -> Maybe b
p = [a] -> (Prefix b, [a])
loop
where
loop :: [a] -> (Prefix b, [a])
loop (a
a : [a]
as) | Just b
b <- a -> Maybe b
p a
a = (Prefix b -> Prefix b) -> (Prefix b, [a]) -> (Prefix b, [a])
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (b
b b -> Prefix b -> Prefix b
forall a. a -> [a] -> [a]
:) ((Prefix b, [a]) -> (Prefix b, [a]))
-> (Prefix b, [a]) -> (Prefix b, [a])
forall a b. (a -> b) -> a -> b
$ [a] -> (Prefix b, [a])
loop [a]
as
loop [a]
as = ([], [a]
as)
partitionMaybe :: (a -> Maybe b) -> [a] -> ([a], [b])
partitionMaybe :: (a -> Maybe b) -> [a] -> ([a], [b])
partitionMaybe a -> Maybe b
f = [a] -> ([a], [b])
loop
where
loop :: [a] -> ([a], [b])
loop [] = ([], [])
loop (a
a : [a]
as) = case a -> Maybe b
f a
a of
Maybe b
Nothing -> ([a] -> [a]) -> ([a], [b]) -> ([a], [b])
forall a c b. (a -> c) -> (a, b) -> (c, b)
mapFst (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], [b]) -> ([a], [b])) -> ([a], [b]) -> ([a], [b])
forall a b. (a -> b) -> a -> b
$ [a] -> ([a], [b])
loop [a]
as
Just b
b -> ([b] -> [b]) -> ([a], [b]) -> ([a], [b])
forall b d a. (b -> d) -> (a, b) -> (a, d)
mapSnd (b
b b -> [b] -> [b]
forall a. a -> [a] -> [a]
:) (([a], [b]) -> ([a], [b])) -> ([a], [b]) -> ([a], [b])
forall a b. (a -> b) -> a -> b
$ [a] -> ([a], [b])
loop [a]
as
filterAndRest :: (a -> Bool) -> [a] -> ([a], Suffix a)
filterAndRest :: (a -> Bool) -> [a] -> ([a], [a])
filterAndRest a -> Bool
p = (a -> Maybe a) -> [a] -> ([a], [a])
forall a b. (a -> Maybe b) -> [a] -> ([b], [a])
mapMaybeAndRest ((a -> Maybe a) -> [a] -> ([a], [a]))
-> (a -> Maybe a) -> [a] -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ \ a
a -> if a -> Bool
p a
a then a -> Maybe a
forall a. a -> Maybe a
Just a
a else Maybe a
forall a. Maybe a
Nothing
mapMaybeAndRest :: (a -> Maybe b) -> [a] -> ([b], Suffix a)
mapMaybeAndRest :: (a -> Maybe b) -> [a] -> ([b], [a])
mapMaybeAndRest a -> Maybe b
f = [a] -> [a] -> ([b], [a])
loop [] where
loop :: [a] -> [a] -> ([b], [a])
loop [a]
acc = \case
[] -> ([], [a] -> [a]
forall a. [a] -> [a]
reverse [a]
acc)
a
x:[a]
xs | Just b
y <- a -> Maybe b
f a
x -> ([b] -> [b]) -> ([b], [a]) -> ([b], [a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (b
yb -> [b] -> [b]
forall a. a -> [a] -> [a]
:) (([b], [a]) -> ([b], [a])) -> ([b], [a]) -> ([b], [a])
forall a b. (a -> b) -> a -> b
$ [a] -> [a] -> ([b], [a])
loop [] [a]
xs
| Bool
otherwise -> [a] -> [a] -> ([b], [a])
loop (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
acc) [a]
xs
isSublistOf :: Eq a => [a] -> [a] -> Bool
isSublistOf :: [a] -> [a] -> Bool
isSublistOf = [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
List.isSubsequenceOf
holes :: [a] -> [(a, [a])]
holes :: [a] -> [(a, [a])]
holes [] = []
holes (a
x:[a]
xs) = (a
x, [a]
xs) (a, [a]) -> [(a, [a])] -> [(a, [a])]
forall a. a -> [a] -> [a]
: ((a, [a]) -> (a, [a])) -> [(a, [a])] -> [(a, [a])]
forall a b. (a -> b) -> [a] -> [b]
map (([a] -> [a]) -> (a, [a]) -> (a, [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) ([a] -> [(a, [a])]
forall a. [a] -> [(a, [a])]
holes [a]
xs)
commonPrefix :: Eq a => [a] -> [a] -> Prefix a
commonPrefix :: [a] -> [a] -> [a]
commonPrefix [] [a]
_ = []
commonPrefix [a]
_ [] = []
commonPrefix (a
x:[a]
xs) (a
y:[a]
ys)
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
commonPrefix [a]
xs [a]
ys
| Bool
otherwise = []
dropCommon :: [a] -> [b] -> (Suffix a, Suffix b)
dropCommon :: [a] -> [b] -> ([a], [b])
dropCommon (a
x : [a]
xs) (b
y : [b]
ys) = [a] -> [b] -> ([a], [b])
forall a b. [a] -> [b] -> ([a], [b])
dropCommon [a]
xs [b]
ys
dropCommon [a]
xs [b]
ys = ([a]
xs, [b]
ys)
stripPrefixBy :: (a -> a -> Bool) -> Prefix a -> [a] -> Maybe (Suffix a)
stripPrefixBy :: (a -> a -> Bool) -> Prefix a -> Prefix a -> Maybe (Prefix a)
stripPrefixBy a -> a -> Bool
eq = Prefix a -> Prefix a -> Maybe (Prefix a)
loop
where
loop :: Prefix a -> Prefix a -> Maybe (Prefix a)
loop [] Prefix a
rest = Prefix a -> Maybe (Prefix a)
forall a. a -> Maybe a
Just Prefix a
rest
loop (a
_:Prefix a
_) [] = Maybe (Prefix a)
forall a. Maybe a
Nothing
loop (a
p:Prefix a
pat) (a
r:Prefix a
rest)
| a -> a -> Bool
eq a
p a
r = Prefix a -> Prefix a -> Maybe (Prefix a)
loop Prefix a
pat Prefix a
rest
| Bool
otherwise = Maybe (Prefix a)
forall a. Maybe a
Nothing
commonSuffix :: Eq a => [a] -> [a] -> Suffix a
commonSuffix :: [a] -> [a] -> [a]
commonSuffix [a]
xs [a]
ys = [a] -> [a]
forall a. [a] -> [a]
reverse ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ ([a] -> [a] -> [a]
forall a. Eq a => [a] -> [a] -> [a]
commonPrefix ([a] -> [a] -> [a]) -> ([a] -> [a]) -> [a] -> [a] -> [a]
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` [a] -> [a]
forall a. [a] -> [a]
reverse) [a]
xs [a]
ys
stripSuffix :: Eq a => Suffix a -> [a] -> Maybe (Prefix a)
stripSuffix :: Suffix a -> Suffix a -> Maybe (Suffix a)
stripSuffix [] = Suffix a -> Maybe (Suffix a)
forall a. a -> Maybe a
Just
stripSuffix Suffix a
s = Suffix a -> Suffix a -> Maybe (Suffix a)
forall a.
Eq a =>
ReversedSuffix a -> ReversedSuffix a -> Maybe (ReversedSuffix a)
stripReversedSuffix (Suffix a -> Suffix a
forall a. [a] -> [a]
reverse Suffix a
s)
type ReversedSuffix a = [a]
stripReversedSuffix :: forall a. Eq a => ReversedSuffix a -> [a] -> Maybe (Prefix a)
stripReversedSuffix :: ReversedSuffix a -> ReversedSuffix a -> Maybe (ReversedSuffix a)
stripReversedSuffix ReversedSuffix a
rs = StrSufSt a -> Maybe (ReversedSuffix a)
final (StrSufSt a -> Maybe (ReversedSuffix a))
-> (ReversedSuffix a -> StrSufSt a)
-> ReversedSuffix a
-> Maybe (ReversedSuffix a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> StrSufSt a -> StrSufSt a)
-> StrSufSt a -> ReversedSuffix a -> StrSufSt a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr a -> StrSufSt a -> StrSufSt a
step (ReversedSuffix a -> StrSufSt a
forall a. ReversedSuffix a -> StrSufSt a
SSSStrip ReversedSuffix a
rs)
where
step :: a -> StrSufSt a -> StrSufSt a
step :: a -> StrSufSt a -> StrSufSt a
step a
x = \case
StrSufSt a
SSSMismatch -> StrSufSt a
forall a. StrSufSt a
SSSMismatch
SSSResult ReversedSuffix a
xs -> ReversedSuffix a -> StrSufSt a
forall a. ReversedSuffix a -> StrSufSt a
SSSResult (a
xa -> ReversedSuffix a -> ReversedSuffix a
forall a. a -> [a] -> [a]
:ReversedSuffix a
xs)
SSSStrip [] -> ReversedSuffix a -> StrSufSt a
forall a. ReversedSuffix a -> StrSufSt a
SSSResult [a
x]
SSSStrip (a
y:ReversedSuffix a
ys)
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y -> ReversedSuffix a -> StrSufSt a
forall a. ReversedSuffix a -> StrSufSt a
SSSStrip ReversedSuffix a
ys
| Bool
otherwise -> StrSufSt a
forall a. StrSufSt a
SSSMismatch
final :: StrSufSt a -> Maybe (Prefix a)
final :: StrSufSt a -> Maybe (ReversedSuffix a)
final = \case
SSSResult ReversedSuffix a
xs -> ReversedSuffix a -> Maybe (ReversedSuffix a)
forall a. a -> Maybe a
Just ReversedSuffix a
xs
SSSStrip [] -> ReversedSuffix a -> Maybe (ReversedSuffix a)
forall a. a -> Maybe a
Just []
StrSufSt a
_ -> Maybe (ReversedSuffix a)
forall a. Maybe a
Nothing
data StrSufSt a
= SSSMismatch
| SSSStrip (ReversedSuffix a)
| SSSResult [a]
findOverlap :: forall a. Eq a => [a] -> [a] -> (Int, Int)
findOverlap :: [a] -> [a] -> (Int, Int)
findOverlap [a]
xs [a]
ys =
(Int, Int) -> [(Int, Int)] -> (Int, Int)
forall a. a -> [a] -> a
headWithDefault (Int, Int)
forall a. HasCallStack => a
__IMPOSSIBLE__ ([(Int, Int)] -> (Int, Int)) -> [(Int, Int)] -> (Int, Int)
forall a b. (a -> b) -> a -> b
$ ((Int, [a]) -> Maybe (Int, Int)) -> [(Int, [a])] -> [(Int, Int)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe (Int, [a]) -> Maybe (Int, Int)
maybePrefix ([(Int, [a])] -> [(Int, Int)]) -> [(Int, [a])] -> [(Int, Int)]
forall a b. (a -> b) -> a -> b
$ [Int] -> [[a]] -> [(Int, [a])]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] ([a] -> [[a]]
forall a. [a] -> [[a]]
List.tails [a]
xs)
where
maybePrefix :: (Int, [a]) -> Maybe (Int, Int)
maybePrefix :: (Int, [a]) -> Maybe (Int, Int)
maybePrefix (Int
k, [a]
xs')
| [a]
xs' [a] -> [a] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`List.isPrefixOf` [a]
ys = (Int, Int) -> Maybe (Int, Int)
forall a. a -> Maybe a
Just (Int
k, [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs')
| Bool
otherwise = Maybe (Int, Int)
forall a. Maybe a
Nothing
groupOn :: Ord b => (a -> b) -> [a] -> [[a]]
groupOn :: (a -> b) -> [a] -> [[a]]
groupOn a -> b
f = (a -> a -> Bool) -> [a] -> [[a]]
forall a. (a -> a -> Bool) -> [a] -> [[a]]
List.groupBy (b -> b -> Bool
forall a. Eq a => a -> a -> Bool
(==) (b -> b -> Bool) -> (a -> b) -> a -> a -> Bool
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f) ([a] -> [[a]]) -> ([a] -> [a]) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> Ordering) -> [a] -> [a]
forall a. (a -> a -> Ordering) -> [a] -> [a]
List.sortBy (b -> b -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (b -> b -> Ordering) -> (a -> b) -> a -> a -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` a -> b
f)
groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy' :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy' a -> a -> Bool
_ [] = []
groupBy' a -> a -> Bool
p xxs :: [a]
xxs@(a
x : [a]
xs) = a -> [(Bool, a)] -> [[a]]
forall a. a -> [(Bool, a)] -> [[a]]
grp a
x ([(Bool, a)] -> [[a]]) -> [(Bool, a)] -> [[a]]
forall a b. (a -> b) -> a -> b
$ (a -> a -> (Bool, a)) -> [a] -> [a] -> [(Bool, a)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\a
x a
y -> (a -> a -> Bool
p a
x a
y, a
y)) [a]
xxs [a]
xs
where
grp :: a -> [(Bool, a)] -> [[a]]
grp a
x [(Bool, a)]
ys = (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: ((Bool, a) -> a) -> [(Bool, a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (Bool, a) -> a
forall a b. (a, b) -> b
snd [(Bool, a)]
xs) [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [[a]]
tail
where ([(Bool, a)]
xs, [(Bool, a)]
rest) = ((Bool, a) -> Bool) -> [(Bool, a)] -> ([(Bool, a)], [(Bool, a)])
forall a. (a -> Bool) -> [a] -> ([a], [a])
span (Bool, a) -> Bool
forall a b. (a, b) -> a
fst [(Bool, a)]
ys
tail :: [[a]]
tail = case [(Bool, a)]
rest of
[] -> []
((Bool
_, a
z) : [(Bool, a)]
zs) -> a -> [(Bool, a)] -> [[a]]
grp a
z [(Bool, a)]
zs
wordsBy :: (a -> Bool) -> [a] -> [[a]]
wordsBy :: (a -> Bool) -> [a] -> [[a]]
wordsBy a -> Bool
p [a]
xs = [a] -> [[a]]
yesP [a]
xs
where
yesP :: [a] -> [[a]]
yesP [a]
xs = [a] -> [[a]]
noP ((a -> Bool) -> [a] -> [a]
forall a. (a -> Bool) -> [a] -> [a]
dropWhile a -> Bool
p [a]
xs)
noP :: [a] -> [[a]]
noP [] = []
noP [a]
xs = [a]
ys [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: [a] -> [[a]]
yesP [a]
zs
where
([a]
ys,[a]
zs) = (a -> Bool) -> [a] -> ([a], [a])
forall a. (a -> Bool) -> [a] -> ([a], [a])
break a -> Bool
p [a]
xs
chop :: Int -> [a] -> [[a]]
chop :: Int -> [a] -> [[a]]
chop Int
_ [] = []
chop Int
n [a]
xs = [a]
ys [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: Int -> [a] -> [[a]]
forall a. Int -> [a] -> [[a]]
chop Int
n [a]
zs
where ([a]
ys,[a]
zs) = Int -> [a] -> ([a], [a])
forall a. Int -> [a] -> ([a], [a])
splitAt Int
n [a]
xs
chopWhen :: forall a. (a -> Bool) -> [a] -> [[a]]
chopWhen :: (a -> Bool) -> [a] -> [[a]]
chopWhen a -> Bool
p [] = []
chopWhen a -> Bool
p (a
x:[a]
xs) = List1 a -> [[a]]
loop (a
x a -> [a] -> List1 a
forall a. a -> [a] -> NonEmpty a
:| [a]
xs)
where
loop :: List1 a -> [[a]]
loop :: List1 a -> [[a]]
loop List1 a
xs = case (a -> Bool) -> List1 a -> ([a], [a])
forall a. (a -> Bool) -> NonEmpty a -> ([a], [a])
List1.break a -> Bool
p List1 a
xs of
([a]
w, [] ) -> [[a]
w]
([a]
w, a
_ : [] ) -> [[a]
w, []]
([a]
w, a
_ : a
y : [a]
ys) -> [a]
w [a] -> [[a]] -> [[a]]
forall a. a -> [a] -> [a]
: List1 a -> [[a]]
loop (a
y a -> [a] -> List1 a
forall a. a -> [a] -> NonEmpty a
:| [a]
ys)
hasElem :: Ord a => [a] -> a -> Bool
hasElem :: [a] -> a -> Bool
hasElem [a]
xs = (a -> Set a -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` [a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList [a]
xs)
sorted :: Ord a => [a] -> Bool
sorted :: [a] -> Bool
sorted [] = Bool
True
sorted [a]
xs = [Bool] -> Bool
forall (t :: * -> *). Foldable t => t Bool -> Bool
and ([Bool] -> Bool) -> [Bool] -> Bool
forall a b. (a -> b) -> a -> b
$ (a -> a -> Bool) -> [a] -> [a] -> [Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith a -> a -> Bool
forall a. Ord a => a -> a -> Bool
(<=) [a]
xs ([a] -> [a]
forall a. [a] -> [a]
tail [a]
xs)
distinct :: Eq a => [a] -> Bool
distinct :: [a] -> Bool
distinct [] = Bool
True
distinct (a
x:[a]
xs) = a
x a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [a]
xs Bool -> Bool -> Bool
&& [a] -> Bool
forall a. Eq a => [a] -> Bool
distinct [a]
xs
fastDistinct :: Ord a => [a] -> Bool
fastDistinct :: [a] -> Bool
fastDistinct [a]
xs = Set a -> Int
forall a. Set a -> Int
Set.size ([a] -> Set a
forall a. Ord a => [a] -> Set a
Set.fromList [a]
xs) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs
duplicates :: Ord a => [a] -> [a]
duplicates :: [a] -> [a]
duplicates = ([a] -> Maybe a) -> [[a]] -> [a]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe [a] -> Maybe a
forall a. [a] -> Maybe a
dup ([[a]] -> [a]) -> ([a] -> [[a]]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bag a -> [[a]]
forall a. Bag a -> [[a]]
Bag.groups (Bag a -> [[a]]) -> ([a] -> Bag a) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Bag a
forall a. Ord a => [a] -> Bag a
Bag.fromList
where
dup :: [a] -> Maybe a
dup (a
a : a
_ : [a]
_) = a -> Maybe a
forall a. a -> Maybe a
Just a
a
dup [a]
_ = Maybe a
forall a. Maybe a
Nothing
allDuplicates :: Ord a => [a] -> [a]
allDuplicates :: [a] -> [a]
allDuplicates = ([a] -> [a]) -> [[a]] -> [a]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
drop Int
1 ([a] -> [a]) -> ([a] -> [a]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> [a]
forall a. [a] -> [a]
reverse) ([[a]] -> [a]) -> ([a] -> [[a]]) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bag a -> [[a]]
forall a. Bag a -> [[a]]
Bag.groups (Bag a -> [[a]]) -> ([a] -> Bag a) -> [a] -> [[a]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Bag a
forall a. Ord a => [a] -> Bag a
Bag.fromList
nubAndDuplicatesOn :: Ord b => (a -> b) -> [a] -> ([a], [a])
nubAndDuplicatesOn :: (a -> b) -> [a] -> ([a], [a])
nubAndDuplicatesOn a -> b
f = Set b -> [a] -> ([a], [a])
loop Set b
forall a. Set a
Set.empty
where
loop :: Set b -> [a] -> ([a], [a])
loop Set b
s [] = ([], [])
loop Set b
s (a
a:[a]
as)
| b
b b -> Set b -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set b
s = ([a] -> [a]) -> ([a], [a]) -> ([a], [a])
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], [a]) -> ([a], [a])) -> ([a], [a]) -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ Set b -> [a] -> ([a], [a])
loop Set b
s [a]
as
| Bool
otherwise = ([a] -> [a]) -> ([a], [a]) -> ([a], [a])
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (a
aa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) (([a], [a]) -> ([a], [a])) -> ([a], [a]) -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ Set b -> [a] -> ([a], [a])
loop (b -> Set b -> Set b
forall a. Ord a => a -> Set a -> Set a
Set.insert b
b Set b
s) [a]
as
where b :: b
b = a -> b
f a
a
nubOn :: Ord b => (a -> b) -> [a] -> [a]
nubOn :: (a -> b) -> [a] -> [a]
nubOn a -> b
f = Set b -> [a] -> [a]
loop Set b
forall a. Set a
Set.empty
where
loop :: Set b -> [a] -> [a]
loop Set b
s [] = []
loop Set b
s (a
a:[a]
as)
| b
b b -> Set b -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set b
s = Set b -> [a] -> [a]
loop Set b
s [a]
as
| Bool
otherwise = a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: Set b -> [a] -> [a]
loop (b -> Set b -> Set b
forall a. Ord a => a -> Set a -> Set a
Set.insert b
b Set b
s) [a]
as
where b :: b
b = a -> b
f a
a
uniqOn :: Ord b => (a -> b) -> [a] -> [a]
uniqOn :: (a -> b) -> [a] -> [a]
uniqOn a -> b
key = Map b a -> [a]
forall k a. Map k a -> [a]
Map.elems (Map b a -> [a]) -> ([a] -> Map b a) -> [a] -> [a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a -> a) -> [(b, a)] -> Map b a
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith (\ a
_ -> a -> a
forall a. a -> a
id) ([(b, a)] -> Map b a) -> ([a] -> [(b, a)]) -> [a] -> Map b a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (b, a)) -> [a] -> [(b, a)]
forall a b. (a -> b) -> [a] -> [b]
map (\ a
a -> (a -> b
key a
a, a
a))
allEqual :: Eq a => [a] -> Bool
allEqual :: [a] -> Bool
allEqual [] = Bool
True
allEqual (a
x : [a]
xs) = (a -> Bool) -> [a] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
x) [a]
xs
nubM :: Monad m => (a -> a -> m Bool) -> [a] -> m [a]
nubM :: (a -> a -> m Bool) -> [a] -> m [a]
nubM a -> a -> m Bool
eq = [a] -> m [a]
loop where
loop :: [a] -> m [a]
loop [] = [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
loop (a
a:[a]
as) = (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ([a] -> [a]) -> m [a] -> m [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> do [a] -> m [a]
loop ([a] -> m [a]) -> m [a] -> m [a]
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (a -> m Bool) -> [a] -> m [a]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM (Bool -> Bool
not (Bool -> Bool) -> (a -> m Bool) -> a -> m Bool
forall (m :: * -> *) b c a.
Functor m =>
(b -> c) -> (a -> m b) -> a -> m c
<.> a -> a -> m Bool
eq a
a) [a]
as
zipWith' :: (a -> b -> c) -> [a] -> [b] -> Maybe [c]
zipWith' :: (a -> b -> c) -> [a] -> [b] -> Maybe [c]
zipWith' a -> b -> c
f = [a] -> [b] -> Maybe [c]
loop
where
loop :: [a] -> [b] -> Maybe [c]
loop [] [] = [c] -> Maybe [c]
forall a. a -> Maybe a
Just []
loop (a
x : [a]
xs) (b
y : [b]
ys) = (a -> b -> c
f a
x b
y c -> [c] -> [c]
forall a. a -> [a] -> [a]
:) ([c] -> [c]) -> Maybe [c] -> Maybe [c]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> [b] -> Maybe [c]
loop [a]
xs [b]
ys
loop [] (b
_ : [b]
_) = Maybe [c]
forall a. Maybe a
Nothing
loop (a
_ : [a]
_) [] = Maybe [c]
forall a. Maybe a
Nothing
zipWithKeepRest :: (a -> b -> b) -> [a] -> [b] -> [b]
zipWithKeepRest :: (a -> b -> b) -> [a] -> [b] -> [b]
zipWithKeepRest a -> b -> b
f = [a] -> [b] -> [b]
loop
where
loop :: [a] -> [b] -> [b]
loop [] [b]
bs = [b]
bs
loop [a]
as [] = []
loop (a
a : [a]
as) (b
b : [b]
bs) = a -> b -> b
f a
a b
b b -> [b] -> [b]
forall a. a -> [a] -> [a]
: [a] -> [b] -> [b]
loop [a]
as [b]
bs
unzipWith :: (a -> (b, c)) -> [a] -> ([b], [c])
unzipWith :: (a -> (b, c)) -> [a] -> ([b], [c])
unzipWith a -> (b, c)
f = [(b, c)] -> ([b], [c])
forall a b. [(a, b)] -> ([a], [b])
unzip ([(b, c)] -> ([b], [c])) -> ([a] -> [(b, c)]) -> [a] -> ([b], [c])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> (b, c)) -> [a] -> [(b, c)]
forall a b. (a -> b) -> [a] -> [b]
map a -> (b, c)
f
editDistanceSpec :: Eq a => [a] -> [a] -> Int
editDistanceSpec :: [a] -> [a] -> Int
editDistanceSpec [] [a]
ys = [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ys
editDistanceSpec [a]
xs [] = [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs
editDistanceSpec (a
x : [a]
xs) (a
y : [a]
ys)
| a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
y = [a] -> [a] -> Int
forall a. Eq a => [a] -> [a] -> Int
editDistanceSpec [a]
xs [a]
ys
| Bool
otherwise = Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [ [a] -> [a] -> Int
forall a. Eq a => [a] -> [a] -> Int
editDistanceSpec (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs) [a]
ys
, [a] -> [a] -> Int
forall a. Eq a => [a] -> [a] -> Int
editDistanceSpec [a]
xs (a
y a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
ys)
, [a] -> [a] -> Int
forall a. Eq a => [a] -> [a] -> Int
editDistanceSpec [a]
xs [a]
ys ]
editDistance :: forall a. Eq a => [a] -> [a] -> Int
editDistance :: [a] -> [a] -> Int
editDistance [a]
xs [a]
ys = Int -> Int -> Int
editD Int
0 Int
0
where
editD :: Int -> Int -> Int
editD Int
i Int
j = Array (Int, Int) Int
tbl Array (Int, Int) Int -> (Int, Int) -> Int
forall i e. Ix i => Array i e -> i -> e
Array.! (Int
i, Int
j)
tbl :: Array (Int,Int) Int
tbl :: Array (Int, Int) Int
tbl = ((Int, Int), (Int, Int))
-> [((Int, Int), Int)] -> Array (Int, Int) Int
forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array ((Int
0,Int
0), (Int
n,Int
m)) [ ((Int
i, Int
j), Int -> Int -> Int
editD' Int
i Int
j) | Int
i <- [Int
0..Int
n], Int
j <- [Int
0..Int
m] ]
editD' :: Int -> Int -> Int
editD' Int
i Int
j =
case (Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
n, Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
j Int
m) of
(Ordering
LT, Ordering
LT)
| Array Int a
xsA Array Int a -> Int -> a
forall i e. Ix i => Array i e -> i -> e
Array.! Int
i a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== Array Int a
ysA Array Int a -> Int -> a
forall i e. Ix i => Array i e -> i -> e
Array.! Int
j
-> Int -> Int -> Int
editD Int
i' Int
j'
| Bool
otherwise -> Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Int] -> Int
forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
minimum [ Int -> Int -> Int
editD Int
i' Int
j, Int -> Int -> Int
editD Int
i Int
j', Int -> Int -> Int
editD Int
i' Int
j' ]
(Ordering
EQ, Ordering
LT) -> Int
m Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
j
(Ordering
LT, Ordering
EQ) -> Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i
(Ordering, Ordering)
_ -> Int
0
where (Int
i',Int
j') = (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1, Int
jInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
n :: Int
n = [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs
m :: Int
m = [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
ys
xsA, ysA :: Array Int a
xsA :: Array Int a
xsA = (Int, Int) -> [a] -> Array Int a
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0,Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [a]
xs
ysA :: Array Int a
ysA = (Int, Int) -> [a] -> Array Int a
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0,Int
mInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) [a]
ys
mergeStrictlyOrderedBy :: (a -> a -> Bool) -> [a] -> [a] -> Maybe [a]
mergeStrictlyOrderedBy :: (a -> a -> Bool) -> [a] -> [a] -> Maybe [a]
mergeStrictlyOrderedBy a -> a -> Bool
(<) = [a] -> [a] -> Maybe [a]
loop where
loop :: [a] -> [a] -> Maybe [a]
loop [] [a]
ys = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
ys
loop [a]
xs [] = [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
xs
loop (a
x:[a]
xs) (a
y:[a]
ys)
| a
x a -> a -> Bool
< a
y = (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ([a] -> [a]) -> Maybe [a] -> Maybe [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> [a] -> Maybe [a]
loop [a]
xs (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys)
| a
y a -> a -> Bool
< a
x = (a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:) ([a] -> [a]) -> Maybe [a] -> Maybe [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a] -> [a] -> Maybe [a]
loop (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs) [a]
ys
| Bool
otherwise = Maybe [a]
forall a. Maybe a
Nothing