0 | module Data.Collections.Analysis
2 | import Control.Monad.State
6 | import Data.Fin.ToFin
8 | import Data.SortedSet.Extra
9 | import Data.Vect.Dependent
10 | import Data.Vect.Extra
12 | import Syntax.IHateParens
13 | import Syntax.Monad.Logic
22 | transitiveClosureM : Monad m => Eq a => (a -> m $
List a) -> List a -> m $
List a
23 | transitiveClosureM f xs = tr xs xs where
24 | tr : (curr : List a) -> (new : List a) -> m $
List a
25 | tr curr [] = pure curr
27 | next <- join <$> for st f
28 | let new = filter (not . (`elem` curr)) next
29 | tr (curr ++ new) new
32 | holdsOnAnyInTrCl : Monad m => Eq a => (a -> Bool) -> (a -> m $
List a) -> List a -> m Bool
33 | holdsOnAnyInTrCl prop f xs = pure (any prop xs) || tr xs xs where
34 | tr : (curr : List a) -> (new : List a) -> m Bool
35 | tr curr [] = pure False
37 | next <- join <$> for st f
38 | let new = filter (not . (`elem` curr)) next
39 | pure (any prop new) || tr (curr ++ new) new
47 | partitionEithersPos : {n : _} -> Vect n (Either a b) -> (List a, List b, FinSet n)
48 | partitionEithersPos = map @{Compose} fromList . p where
49 | p : forall n. Vect n (Either a b) -> (List a, List b, List $
Fin n)
50 | p [] = ([], [], empty)
51 | p (ab::abs) = let (as, bs, lefts) = p abs
52 | lefts = FS <$> lefts
54 | Left a => (a::as, bs, FZ::lefts)
55 | Right b => ( as, b::bs, lefts)
58 | joinEithersPos : (as : List a) -> (bs : List b) -> FinSet (as.length + bs.length) -> Maybe $
Vect (as.length + bs.length) $
Either a b
59 | joinEithersPos as bs lefts =
60 | evalState (as, bs) $
for @{Compose} range $
\idx => if contains idx lefts
63 | | ([], _) => pure $
Nothing
65 | pure $
Just $
Left x
68 | | (_, []) => pure $
Nothing
70 | pure $
Just $
Right x
77 | disjointDepSets : {n : _} -> (rawDeps : DVect n $
FinSet . Fin.finToNat) -> (givs : FinSet n) -> List $
FinSet n
78 | disjointDepSets rawDeps givs = do
81 | let minDeps = flip mapPreI rawDeps $
\i, pre => maybe last FS . leftMost . (`difference` fit givs)
84 | let minDeps = downmap (weakenToSuper {i=FS _}) minDeps
87 | let minDeps : FinMap .| S n .| FinSet n :=
88 | concatMap .| uncurry singleton .| mapSnd singleton <$> toListI minDeps
91 | Prelude.toList minDeps
98 | combinations : Vect n (List1 a) -> List1 (Vect n a)
99 | combinations l = map (rewrite plusZeroRightNeutral n in reverse) $
go l $
Nil:::[] where
100 | go : Vect m (List1 a) -> List1 (Vect k a) -> List1 (Vect (m + k) a)
102 | go {m = S m} (xs::xss) rss = rewrite plusSuccRightSucc m k in
103 | go xss $
join $
map (\x => map (\rs => x :: rs) rss) xs
106 | permutations : Ord a => SortedSet a -> List1 $
List a
107 | permutations s = case fromList s.asList of
111 | es <- permutations $
assert_smaller s $
delete e s
115 | permutationsFin : {n : _} -> FinSet n -> List1 $
List $
Fin n
116 | permutationsFin s = case fromList s.asList of
120 | es <- permutationsFin $
assert_smaller s $
delete e s
124 | permutations' : Ord a => SortedSet a -> List $
List a
125 | permutations' = forget . permutations
128 | indepPermutations : {n : _} -> (independencyGroups : List $
FinSet n) -> FinSet n -> List1 $
List $
Fin n
129 | indepPermutations groups s = map concat $
for groups $
permutationsFin . intersection s
131 | public export %inline
132 | indepPermutations' : {n : _} -> (independencyGroups : List $
FinSet n) -> FinSet n -> List $
List $
Fin n
133 | indepPermutations' = forget .: indepPermutations