diff --git a/Everything/Safe.agda b/Everything/Safe.agda index e62f9f5..4186068 100644 --- a/Everything/Safe.agda +++ b/Everything/Safe.agda @@ -12,6 +12,7 @@ open import Numbers.Integers.Integers open import Numbers.Integers.RingStructure.EuclideanDomain open import Lists.Lists +open import Lists.Filter.AllTrue open import Groups.Groups open import Groups.Abelian.Lemmas diff --git a/Groups/FinitePermutations.agda b/Groups/FinitePermutations.agda index c0934bf..b509ec2 100644 --- a/Groups/FinitePermutations.agda +++ b/Groups/FinitePermutations.agda @@ -10,88 +10,89 @@ open import Setoids.Setoids --open import Groups.Actions module Groups.FinitePermutations where - allInsertions : {a : _} {A : Set a} (x : A) (l : List A) → List (List A) - allInsertions x [] = [ x ] :: [] - allInsertions x (y :: l) = (x :: (y :: l)) :: (map (λ i → y :: i) (allInsertions x l)) - permutations : {a : _} {A : Set a} (l : List A) → List (List A) - permutations [] = [ [] ] - permutations (x :: l) = flatten (map (allInsertions x) (permutations l)) +allInsertions : {a : _} {A : Set a} (x : A) (l : List A) → List (List A) +allInsertions x [] = [ x ] :: [] +allInsertions x (y :: l) = (x :: (y :: l)) :: (map (λ i → y :: i) (allInsertions x l)) - factorial : ℕ → ℕ - factorial zero = 1 - factorial (succ n) = (succ n) *N factorial n +permutations : {a : _} {A : Set a} (l : List A) → List (List A) +permutations [] = [ [] ] +permutations (x :: l) = flatten (map (allInsertions x) (permutations l)) - factCheck : factorial 5 ≡ 120 - factCheck = refl +factorial : ℕ → ℕ +factorial zero = 1 +factorial (succ n) = (succ n) *N factorial n - allInsertionsLength : {a : _} {A : Set a} (x : A) (l : List A) → length (allInsertions x l) ≡ succ (length l) - allInsertionsLength x [] = refl - allInsertionsLength x (y :: l) = applyEquality succ (transitivity (equalityCommutative (lengthMap (allInsertions x l) {f = y ::_})) (allInsertionsLength x l)) +factCheck : factorial 5 ≡ 120 +factCheck = refl - allInsertionsLength' : {a : _} {A : Set a} (x : A) (l : List A) → allTrue (λ i → length i ≡ succ (length l)) (allInsertions x l) - allInsertionsLength' x [] = refl ,, record {} - allInsertionsLength' x (y :: l) with allInsertionsLength' x l - ... | bl = refl ,, allTrueMap (λ i → length i ≡ succ (succ (length l))) (y ::_) (allInsertions x l) (allTrueExtension (λ z → length z ≡ succ (length l)) ((λ i → length i ≡ succ (succ (length l))) ∘ (y ::_)) (allInsertions x l) (λ {x} → λ p → applyEquality succ p) bl) +allInsertionsLength : {a : _} {A : Set a} (x : A) (l : List A) → length (allInsertions x l) ≡ succ (length l) +allInsertionsLength x [] = refl +allInsertionsLength x (y :: l) = applyEquality succ (transitivity (equalityCommutative (lengthMap (allInsertions x l) {f = y ::_})) (allInsertionsLength x l)) - permutationsCheck : permutations (3 :: 4 :: []) ≡ (3 :: 4 :: []) :: (4 :: 3 :: []) :: [] - permutationsCheck = refl +allInsertionsLength' : {a : _} {A : Set a} (x : A) (l : List A) → allTrue (λ i → length i ≡ succ (length l)) (allInsertions x l) +allInsertionsLength' x [] = refl ,, record {} +allInsertionsLength' x (y :: l) with allInsertionsLength' x l +... | bl = refl ,, allTrueMap (λ i → length i ≡ succ (succ (length l))) (y ::_) (allInsertions x l) (allTrueExtension (λ z → length z ≡ succ (length l)) ((λ i → length i ≡ succ (succ (length l))) ∘ (y ::_)) (allInsertions x l) (λ {x} → λ p → applyEquality succ p) bl) - permsAllSameLength : {a : _} {A : Set a} (l : List A) → allTrue (λ i → length i ≡ length l) (permutations l) - permsAllSameLength [] = refl ,, record {} - permsAllSameLength (x :: l) with permsAllSameLength l - ... | bl = allTrueFlatten (λ i → length i ≡ succ (length l)) (map (allInsertions x) (permutations l)) (allTrueMap (allTrue (λ i → length i ≡ succ (length l))) (allInsertions x) (permutations l) (allTrueExtension (λ z → length z ≡ length l) (allTrue (λ i → length i ≡ succ (length l)) ∘ allInsertions x) (permutations l) lemma bl)) - where - lemma : {m : List _} → (lenM=lenL : length m ≡ length l) → allTrue (λ i → length i ≡ succ (length l)) (allInsertions x m) - lemma {m} lm=ll rewrite equalityCommutative lm=ll = allInsertionsLength' x m +permutationsCheck : permutations (3 :: 4 :: []) ≡ (3 :: 4 :: []) :: (4 :: 3 :: []) :: [] +permutationsCheck = refl - allTrueEqual : {a b : _} {A : Set a} {B : Set b} (f : A → B) (equalTo : B) (l : List A) → allTrue (λ i → f i ≡ equalTo) l → map f l ≡ replicate (length l) equalTo - allTrueEqual f equalTo [] pr = refl - allTrueEqual f equalTo (x :: l) (fst ,, snd) rewrite fst | allTrueEqual f equalTo l snd = refl +permsAllSameLength : {a : _} {A : Set a} (l : List A) → allTrue (λ i → length i ≡ length l) (permutations l) +permsAllSameLength [] = refl ,, record {} +permsAllSameLength (x :: l) with permsAllSameLength l +... | bl = allTrueFlatten (λ i → length i ≡ succ (length l)) (map (allInsertions x) (permutations l)) (allTrueMap (allTrue (λ i → length i ≡ succ (length l))) (allInsertions x) (permutations l) (allTrueExtension (λ z → length z ≡ length l) (allTrue (λ i → length i ≡ succ (length l)) ∘ allInsertions x) (permutations l) lemma bl)) + where + lemma : {m : List _} → (lenM=lenL : length m ≡ length l) → allTrue (λ i → length i ≡ succ (length l)) (allInsertions x m) + lemma {m} lm=ll rewrite equalityCommutative lm=ll = allInsertionsLength' x m - totalReplicate : (l len : ℕ) → total (replicate len l) ≡ l *N len - totalReplicate l zero rewrite multiplicationNIsCommutative l 0 = refl - totalReplicate l (succ len) rewrite totalReplicate l len | multiplicationNIsCommutative l (succ len) = applyEquality (l +N_) (multiplicationNIsCommutative l len) +allTrueEqual : {a b : _} {A : Set a} {B : Set b} (f : A → B) (equalTo : B) (l : List A) → allTrue (λ i → f i ≡ equalTo) l → map f l ≡ replicate (length l) equalTo +allTrueEqual f equalTo [] pr = refl +allTrueEqual f equalTo (x :: l) (fst ,, snd) rewrite fst | allTrueEqual f equalTo l snd = refl - permsLen : {a : _} {A : Set a} (l : List A) → length (permutations l) ≡ factorial (length l) - permsLen [] = refl - permsLen (x :: l) = transitivity (lengthFlatten (map (allInsertions x) (permutations l))) (transitivity (applyEquality total (mapMap (permutations l))) (transitivity (applyEquality total (mapExtension (permutations l) (length ∘ allInsertions x) (succ ∘ length) λ {y} → allInsertionsLength x y)) (transitivity (applyEquality total lemma) (transitivity (totalReplicate (succ (length l)) (length (permutations l))) ans)))) - where - lemma : map (λ a → succ (length a)) (permutations l) ≡ replicate (length (permutations l)) (succ (length l)) - lemma = allTrueEqual (λ a → succ (length a)) (succ (length l)) (permutations l) (allTrueExtension (λ z → length z ≡ length l) (λ i → succ (length i) ≡ succ (length l)) (permutations l) (λ pr → applyEquality succ pr) (permsAllSameLength l)) - ans : length (permutations l) +N length l *N length (permutations l) ≡ factorial (length l) +N length l *N factorial (length l) - ans rewrite permsLen l = refl +totalReplicate : (l len : ℕ) → fold _+N_ 0 (replicate len l) ≡ l *N len +totalReplicate l zero rewrite multiplicationNIsCommutative l 0 = refl +totalReplicate l (succ len) rewrite totalReplicate l len | multiplicationNIsCommutative l (succ len) = applyEquality (l +N_) (multiplicationNIsCommutative l len) - --act : GroupAction (symmetricGroup (symmetricSetoid (reflSetoid (FinSet n)))) - --act = ? +permsLen : {a : _} {A : Set a} (l : List A) → length (permutations l) ≡ factorial (length l) +permsLen [] = refl +permsLen (x :: l) = transitivity (lengthFlatten (map (allInsertions x) (permutations l))) (transitivity (applyEquality (fold _+N_ 0) (mapMap (permutations l))) (transitivity (applyEquality (fold _+N_ 0) (mapExtension (permutations l) (length ∘ allInsertions x) (succ ∘ length) λ {y} → allInsertionsLength x y)) (transitivity (applyEquality (fold _+N_ 0) lemma) (transitivity (totalReplicate (succ (length l)) (length (permutations l))) ans)))) + where + lemma : map (λ a → succ (length a)) (permutations l) ≡ replicate (length (permutations l)) (succ (length l)) + lemma = allTrueEqual (λ a → succ (length a)) (succ (length l)) (permutations l) (allTrueExtension (λ z → length z ≡ length l) (λ i → succ (length i) ≡ succ (length l)) (permutations l) (λ pr → applyEquality succ pr) (permsAllSameLength l)) + ans : length (permutations l) +N length l *N length (permutations l) ≡ factorial (length l) +N length l *N factorial (length l) + ans rewrite permsLen l = refl + +--act : GroupAction (symmetricGroup (symmetricSetoid (reflSetoid (FinSet n)))) +--act = ? {- TODO: show that symmetricGroup acts in the obvious way on permutations FinSet - listElements : {a : _} {A : Set a} (l : List A) → Set - listElements [] = False - listElements (x :: l) = True || listElements l +listElements : {a : _} {A : Set a} (l : List A) → Set +listElements [] = False +listElements (x :: l) = True || listElements l - listElement : {a : _} {A : Set a} {l : List A} (elt : listElements l) → A - listElement {l = []} () - listElement {l = x :: l} (inl record {}) = x - listElement {l = x :: l} (inr elt) = listElement {l = l} elt +listElement : {a : _} {A : Set a} {l : List A} (elt : listElements l) → A +listElement {l = []} () +listElement {l = x :: l} (inl record {}) = x +listElement {l = x :: l} (inr elt) = listElement {l = l} elt - backwardRange : ℕ → List ℕ - backwardRange zero = [] - backwardRange (succ n) = n :: backwardRange n +backwardRange : ℕ → List ℕ +backwardRange zero = [] +backwardRange (succ n) = n :: backwardRange n - backwardRangeLength : {n : ℕ} → length (backwardRange n) ≡ n - backwardRangeLength {zero} = refl - backwardRangeLength {succ n} rewrite backwardRangeLength {n} = refl +backwardRangeLength : {n : ℕ} → length (backwardRange n) ≡ n +backwardRangeLength {zero} = refl +backwardRangeLength {succ n} rewrite backwardRangeLength {n} = refl - applyPermutation : {n : ℕ} → (f : FinSet n → FinSet n) → listElements (permutations (backwardRange n)) → listElements (permutations (backwardRange n)) - applyPermutation {zero} f (inl record {}) = inl (record {}) - applyPermutation {zero} f (inr ()) - applyPermutation {succ n} f elt = {!!} +applyPermutation : {n : ℕ} → (f : FinSet n → FinSet n) → listElements (permutations (backwardRange n)) → listElements (permutations (backwardRange n)) +applyPermutation {zero} f (inl record {}) = inl (record {}) +applyPermutation {zero} f (inr ()) +applyPermutation {succ n} f elt = {!!} - finitePermutations : (n : ℕ) → SetoidToSet (symmetricSetoid (reflSetoid (FinSet n))) (listElements (permutations (backwardRange n))) - SetoidToSet.project (finitePermutations n) (sym {f} fBij) = {!!} - SetoidToSet.wellDefined (finitePermutations n) = {!!} - SetoidToSet.surj (finitePermutations n) = {!!} - SetoidToSet.inj (finitePermutations n) = {!!} +finitePermutations : (n : ℕ) → SetoidToSet (symmetricSetoid (reflSetoid (FinSet n))) (listElements (permutations (backwardRange n))) +SetoidToSet.project (finitePermutations n) (sym {f} fBij) = {!!} +SetoidToSet.wellDefined (finitePermutations n) = {!!} +SetoidToSet.surj (finitePermutations n) = {!!} +SetoidToSet.inj (finitePermutations n) = {!!} -} diff --git a/Lists/Concat.agda b/Lists/Concat.agda new file mode 100644 index 0000000..cdd43a9 --- /dev/null +++ b/Lists/Concat.agda @@ -0,0 +1,25 @@ +{-# OPTIONS --safe --warning=error --without-K #-} + +open import LogicalFormulae +open import Functions +open import Lists.Definition +open import Lists.Length +open import Numbers.Naturals.Semiring + +module Lists.Concat where + +appendEmptyList : {a : _} → {A : Set a} → (l : List A) → (l ++ [] ≡ l) +appendEmptyList [] = refl +appendEmptyList (x :: l) = applyEquality (_::_ x) (appendEmptyList l) + +concatAssoc : {a : _} → {A : Set a} → (x : List A) → (y : List A) → (z : List A) → ((x ++ y) ++ z) ≡ x ++ (y ++ z) +concatAssoc [] m n = refl +concatAssoc (x :: l) m n = applyEquality (_::_ x) (concatAssoc l m n) + +canMovePrepend : {a : _} → {A : Set a} → (l : A) → (x : List A) (y : List A) → ((l :: x) ++ y ≡ l :: (x ++ y)) +canMovePrepend l [] n = refl +canMovePrepend l (x :: m) n = refl + +lengthConcat : {a : _} {A : Set a} (l1 l2 : List A) → length (l1 ++ l2) ≡ length l1 +N length l2 +lengthConcat [] l2 = refl +lengthConcat (x :: l1) l2 = applyEquality succ (lengthConcat l1 l2) diff --git a/Lists/Definition.agda b/Lists/Definition.agda new file mode 100644 index 0000000..0fccbf0 --- /dev/null +++ b/Lists/Definition.agda @@ -0,0 +1,18 @@ +{-# OPTIONS --safe --warning=error --without-K #-} + +open import LogicalFormulae +open import Functions + +module Lists.Definition {a : _} where + +data List (A : Set a) : Set a where + [] : List A + _::_ : (x : A) (xs : List A) → List A +infixr 10 _::_ + +[_] : {A : Set a} → (a : A) → List A +[ a ] = a :: [] + +_++_ : {A : Set a} → List A → List A → List A +[] ++ m = m +(x :: l) ++ m = x :: (l ++ m) diff --git a/Lists/Filter/AllTrue.agda b/Lists/Filter/AllTrue.agda new file mode 100644 index 0000000..f2836c7 --- /dev/null +++ b/Lists/Filter/AllTrue.agda @@ -0,0 +1,38 @@ +{-# OPTIONS --safe --warning=error --without-K #-} + +open import LogicalFormulae +open import Functions +open import Lists.Definition +open import Lists.Monad + +module Lists.Filter.AllTrue where + +allTrue : {a b : _} {A : Set a} (f : A → Set b) (l : List A) → Set b +allTrue f [] = True' +allTrue f (x :: l) = f x && allTrue f l + +allTrueConcat : {a b : _} {A : Set a} (f : A → Set b) (l m : List A) → allTrue f l → allTrue f m → allTrue f (l ++ m) +allTrueConcat f [] m fl fm = fm +allTrueConcat f (x :: l) m (fst ,, snd) fm = fst ,, allTrueConcat f l m snd fm + +allTrueFlatten : {a b : _} {A : Set a} (f : A → Set b) (l : List (List A)) → allTrue (λ i → allTrue f i) l → allTrue f (flatten l) +allTrueFlatten f [] pr = record {} +allTrueFlatten f ([] :: ls) pr = allTrueFlatten f ls (_&&_.snd pr) +allTrueFlatten f ((x :: l) :: ls) ((fx ,, fl) ,, snd) = fx ,, allTrueConcat f l (flatten ls) fl (allTrueFlatten f ls snd) + +allTrueMap : {a b c : _} {A : Set a} {B : Set b} (pred : B → Set c) (f : A → B) (l : List A) → allTrue (pred ∘ f) l → allTrue pred (map f l) +allTrueMap pred f [] pr = record {} +allTrueMap pred f (x :: l) pr = _&&_.fst pr ,, allTrueMap pred f l (_&&_.snd pr) + +allTrueExtension : {a b : _} {A : Set a} (f g : A → Set b) (l : List A) → ({x : A} → f x → g x) → allTrue f l → allTrue g l +allTrueExtension f g [] pred t = record {} +allTrueExtension f g (x :: l) pred (fg ,, snd) = pred {x} fg ,, allTrueExtension f g l pred snd + +allTrueTail : {a b : _} {A : Set a} (pred : A → Set b) (x : A) (l : List A) → allTrue pred (x :: l) → allTrue pred l +allTrueTail pred x l (fst ,, snd) = snd + +filter : {a : _} {A : Set a} (l : List A) (f : A → Bool) → List A +filter [] f = [] +filter (x :: l) f with f x +filter (x :: l) f | BoolTrue = x :: filter l f +filter (x :: l) f | BoolFalse = filter l f diff --git a/Lists/Fold/Fold.agda b/Lists/Fold/Fold.agda new file mode 100644 index 0000000..36254f3 --- /dev/null +++ b/Lists/Fold/Fold.agda @@ -0,0 +1,11 @@ +{-# OPTIONS --safe --warning=error --without-K #-} + +open import LogicalFormulae +open import Functions +open import Lists.Definition + +module Lists.Fold.Fold {a b : _} {A : Set a} {B : Set b} where + +fold : (f : A → B → B) → B → List A → B +fold f default [] = default +fold f default (x :: l) = f x (fold f default l) diff --git a/Lists/Length.agda b/Lists/Length.agda new file mode 100644 index 0000000..bce43fd --- /dev/null +++ b/Lists/Length.agda @@ -0,0 +1,22 @@ +{-# OPTIONS --safe --warning=error --without-K #-} + +open import LogicalFormulae +open import Functions +open import Numbers.Naturals.Semiring -- for length +open import Numbers.Naturals.Order +open import Semirings.Definition +open import Lists.Definition +open import Lists.Fold.Fold + +module Lists.Length where + +length : {a : _} {A : Set a} (l : List A) → ℕ +length [] = zero +length (x :: l) = succ (length l) + +length' : {a : _} {A : Set a} → (l : List A) → ℕ +length' = fold (λ _ → succ) 0 + +length=length' : {a : _} {A : Set a} (l : List A) → length l ≡ length' l +length=length' [] = refl +length=length' (x :: l) = applyEquality succ (length=length' l) diff --git a/Lists/Lists.agda b/Lists/Lists.agda index cfe637c..7618676 100644 --- a/Lists/Lists.agda +++ b/Lists/Lists.agda @@ -1,4 +1,4 @@ -{-# OPTIONS --safe --warning=error --without-K #-} +{-# OPTIONS --warning=error --safe --without-K #-} open import LogicalFormulae open import Functions @@ -9,149 +9,19 @@ open import Maybe module Lists.Lists where -data List {a} (A : Set a) : Set a where - [] : List A - _::_ : (x : A) (xs : List A) → List A -infixr 10 _::_ - -[_] : {a : _} → {A : Set a} → (a : A) → List A -[ a ] = a :: [] - -_++_ : {a : _} → {A : Set a} → List A → List A → List A -[] ++ m = m -(x :: l) ++ m = x :: (l ++ m) - -appendEmptyList : {a : _} → {A : Set a} → (l : List A) → (l ++ [] ≡ l) -appendEmptyList [] = refl -appendEmptyList (x :: l) = applyEquality (_::_ x) (appendEmptyList l) - -concatAssoc : {a : _} → {A : Set a} → (x : List A) → (y : List A) → (z : List A) → ((x ++ y) ++ z) ≡ x ++ (y ++ z) -concatAssoc [] m n = refl -concatAssoc (x :: l) m n = applyEquality (_::_ x) (concatAssoc l m n) - -canMovePrepend : {a : _} → {A : Set a} → (l : A) → {m n : ℕ} → (x : List A) (y : List A) → ((l :: x) ++ y ≡ l :: (x ++ y)) -canMovePrepend l [] n = refl -canMovePrepend l (x :: m) n = refl - -rev : {a : _} → {A : Set a} → List A → List A -rev [] = [] -rev (x :: l) = (rev l) ++ [ x ] - -revIsHom : {a : _} → {A : Set a} → (l1 : List A) → (l2 : List A) → (rev (l1 ++ l2) ≡ (rev l2) ++ (rev l1)) -revIsHom l1 [] = applyEquality rev (appendEmptyList l1) -revIsHom [] (x :: l2) with (rev l2 ++ [ x ]) -... | r = equalityCommutative (appendEmptyList r) -revIsHom (w :: l1) (x :: l2) = transitivity t (equalityCommutative s) - where - s : ((rev l2 ++ [ x ]) ++ (rev l1 ++ [ w ])) ≡ (((rev l2 ++ [ x ]) ++ rev l1) ++ [ w ]) - s = equalityCommutative (concatAssoc (rev l2 ++ (x :: [])) (rev l1) ([ w ])) - t' : rev (l1 ++ (x :: l2)) ≡ rev (x :: l2) ++ rev l1 - t' = revIsHom l1 (x :: l2) - t : (rev (l1 ++ (x :: l2)) ++ [ w ]) ≡ ((rev l2 ++ [ x ]) ++ rev l1) ++ [ w ] - t = applyEquality (λ r → r ++ [ w ]) {rev (l1 ++ (x :: l2))} {((rev l2) ++ [ x ]) ++ rev l1} (transitivity t' (applyEquality (λ r → r ++ rev l1) {rev l2 ++ (x :: [])} {rev l2 ++ (x :: [])} refl)) - -revRevIsId : {a : _} → {A : Set a} → (l : List A) → (rev (rev l) ≡ l) -revRevIsId [] = refl -revRevIsId (x :: l) = t - where - s : rev (rev l ++ [ x ] ) ≡ [ x ] ++ rev (rev l) - s = revIsHom (rev l) [ x ] - t : rev (rev l ++ [ x ] ) ≡ [ x ] ++ l - t = identityOfIndiscernablesRight _≡_ s (applyEquality (λ n → [ x ] ++ n) (revRevIsId l)) - -fold : {a b : _} {A : Set a} {B : Set b} (f : A → B → B) → B → List A → B -fold f default [] = default -fold f default (x :: l) = f x (fold f default l) - -map : {a : _} → {b : _} → {A : Set a} → {B : Set b} → (f : A → B) → List A → List B -map f [] = [] -map f (x :: list) = (f x) :: (map f list) - -map' : {a : _} → {b : _} → {A : Set a} → {B : Set b} → (f : A → B) → List A → List B -map' f = fold (λ a → (f a) ::_ ) [] - -map=map' : {a : _} → {b : _} → {A : Set a} → {B : Set b} → (f : A → B) → (l : List A) → (map f l ≡ map' f l) -map=map' f [] = refl -map=map' f (x :: l) with map=map' f l -... | bl = applyEquality (f x ::_) bl - -flatten : {a : _} {A : Set a} → (l : List (List A)) → List A -flatten [] = [] -flatten (l :: ls) = l ++ flatten ls - -flatten' : {a : _} {A : Set a} → (l : List (List A)) → List A -flatten' = fold _++_ [] - -flatten=flatten' : {a : _} {A : Set a} (l : List (List A)) → flatten l ≡ flatten' l -flatten=flatten' [] = refl -flatten=flatten' (l :: ls) = applyEquality (l ++_) (flatten=flatten' ls) - -length : {a : _} {A : Set a} (l : List A) → ℕ -length [] = zero -length (x :: l) = succ (length l) - -length' : {a : _} {A : Set a} → (l : List A) → ℕ -length' = fold (λ _ → succ) 0 - -length=length' : {a : _} {A : Set a} (l : List A) → length l ≡ length' l -length=length' [] = refl -length=length' (x :: l) = applyEquality succ (length=length' l) - -total : List ℕ → ℕ -total [] = zero -total (x :: l) = x +N total l - -total' : List ℕ → ℕ -total' = fold _+N_ 0 - -lengthConcat : {a : _} {A : Set a} (l1 l2 : List A) → length (l1 ++ l2) ≡ length l1 +N length l2 -lengthConcat [] l2 = refl -lengthConcat (x :: l1) l2 = applyEquality succ (lengthConcat l1 l2) - -lengthFlatten : {a : _} {A : Set a} (l : List (List A)) → length (flatten l) ≡ total (map length l) -lengthFlatten [] = refl -lengthFlatten (l :: ls) rewrite lengthConcat l (flatten ls) | lengthFlatten ls = refl - -lengthMap : {a b : _} {A : Set a} {B : Set b} → (l : List A) → {f : A → B} → length l ≡ length (map f l) -lengthMap [] {f} = refl -lengthMap (x :: l) {f} rewrite lengthMap l {f} = refl - -mapMap : {a b c : _} {A : Set a} {B : Set b} {C : Set c} → (l : List A) → {f : A → B} {g : B → C} → map g (map f l) ≡ map (g ∘ f) l -mapMap [] = refl -mapMap (x :: l) {f = f} {g} rewrite mapMap l {f} {g} = refl - -mapExtension : {a b : _} {A : Set a} {B : Set b} (l : List A) (f g : A → B) → ({x : A} → (f x ≡ g x)) → map f l ≡ map g l -mapExtension [] f g pr = refl -mapExtension (x :: l) f g pr rewrite mapExtension l f g pr | pr {x} = refl +open import Lists.Definition public +open import Lists.Length public +open import Lists.Concat public +open import Lists.Map.Map public +open import Lists.Reversal.Reversal public +open import Lists.Monad public +open import Lists.Filter.AllTrue public +open import Lists.Fold.Fold public replicate : {a : _} {A : Set a} (n : ℕ) (x : A) → List A replicate zero x = [] replicate (succ n) x = x :: replicate n x -allTrue : {a b : _} {A : Set a} (f : A → Set b) (l : List A) → Set b -allTrue f [] = True' -allTrue f (x :: l) = f x && allTrue f l - -allTrueConcat : {a b : _} {A : Set a} (f : A → Set b) (l m : List A) → allTrue f l → allTrue f m → allTrue f (l ++ m) -allTrueConcat f [] m fl fm = fm -allTrueConcat f (x :: l) m (fst ,, snd) fm = fst ,, allTrueConcat f l m snd fm - -allTrueFlatten : {a b : _} {A : Set a} (f : A → Set b) (l : List (List A)) → allTrue (λ i → allTrue f i) l → allTrue f (flatten l) -allTrueFlatten f [] pr = record {} -allTrueFlatten f ([] :: ls) pr = allTrueFlatten f ls (_&&_.snd pr) -allTrueFlatten f ((x :: l) :: ls) ((fx ,, fl) ,, snd) = fx ,, allTrueConcat f l (flatten ls) fl (allTrueFlatten f ls snd) - -allTrueMap : {a b c : _} {A : Set a} {B : Set b} (pred : B → Set c) (f : A → B) (l : List A) → allTrue (pred ∘ f) l → allTrue pred (map f l) -allTrueMap pred f [] pr = record {} -allTrueMap pred f (x :: l) pr = _&&_.fst pr ,, allTrueMap pred f l (_&&_.snd pr) - -allTrueExtension : {a b : _} {A : Set a} (f g : A → Set b) (l : List A) → ({x : A} → f x → g x) → allTrue f l → allTrue g l -allTrueExtension f g [] pred t = record {} -allTrueExtension f g (x :: l) pred (fg ,, snd) = pred {x} fg ,, allTrueExtension f g l pred snd - -allTrueTail : {a b : _} {A : Set a} (pred : A → Set b) (x : A) (l : List A) → allTrue pred (x :: l) → allTrue pred l -allTrueTail pred x l (fst ,, snd) = snd - head : {a : _} {A : Set a} (l : List A) (pr : 0