-
Notifications
You must be signed in to change notification settings - Fork 46
Add Haskell.Data.List
#396
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
omelkonian
merged 5 commits into
agda:master
from
HeinrichApfelmus:HeinrichApfelmus/data-list
Mar 10, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e19cc05
Add `Haskell.Data.Ord`
HeinrichApfelmus 6dff125
Add `Haskell.Data.List`
HeinrichApfelmus 95fd085
Add `sort` to `Data.List`
HeinrichApfelmus dac005f
Add `nub` to `Data.List`
HeinrichApfelmus 4cea90b
Prove properties about `nub`
HeinrichApfelmus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| module Haskell.Data.List where | ||
|
|
||
| open import Haskell.Prelude | ||
|
|
||
| open import Haskell.Data.Ord using (comparing) | ||
|
|
||
| open import Haskell.Law.Eq | ||
| open import Haskell.Law.Equality | ||
|
|
||
| {----------------------------------------------------------------------------- | ||
| Operations | ||
| ------------------------------------------------------------------------------} | ||
|
|
||
| partition : (a → Bool) → List a → (List a × List a) | ||
| partition p xs = (filter p xs , filter (not ∘ p) xs) | ||
|
|
||
| -- | Delete all occurrences of an item. | ||
| -- Not part of 'Data.List'. | ||
| deleteAll : ⦃ _ : Eq a ⦄ → @0 ⦃ IsLawfulEq a ⦄ → a → List a → List a | ||
| deleteAll x = filter (not ∘ (x ==_)) | ||
|
|
||
| -- | These semantics of 'nub' assume that the 'Eq' instance | ||
| -- is lawful. | ||
| -- These semantics are inefficient, but good for proofs. | ||
| nub : ⦃ _ : Eq a ⦄ → @0 ⦃ IsLawfulEq a ⦄ → List a → List a | ||
| nub [] = [] | ||
| nub (x ∷ xs) = x ∷ deleteAll x (nub xs) | ||
|
|
||
| postulate | ||
| sortBy : (a → a → Ordering) → List a → List a | ||
|
|
||
| sort : ⦃ Ord a ⦄ → List a → List a | ||
| sort = sortBy compare | ||
|
|
||
| sortOn : ⦃ Ord b ⦄ → (a → b) → List a → List a | ||
| sortOn f = | ||
| map snd | ||
| ∘ sortBy (comparing fst) | ||
| ∘ map (λ x → let y = f x in seq y (y , x)) | ||
|
|
||
| {----------------------------------------------------------------------------- | ||
| Properties | ||
| ------------------------------------------------------------------------------} | ||
|
|
||
| -- | A deleted item is no longer an element. | ||
| -- | ||
| prop-elem-deleteAll | ||
| : ∀ ⦃ _ : Eq a ⦄ ⦃ _ : IsLawfulEq a ⦄ | ||
| (x y : a) (zs : List a) | ||
| → elem x (deleteAll y zs) | ||
| ≡ (if x == y then False else elem x zs) | ||
| -- | ||
| prop-elem-deleteAll x y [] | ||
| with x == y | ||
| ... | False = refl | ||
| ... | True = refl | ||
| prop-elem-deleteAll x y (z ∷ zs) | ||
| with recurse ← prop-elem-deleteAll x y zs | ||
| with y == z in eqyz | ||
| ... | True | ||
| with x == z in eqxz | ||
| ... | True | ||
| rewrite equality' _ _ (trans (equality x z eqxz) (sym (equality y z eqyz))) | ||
| = recurse | ||
| ... | False | ||
| = recurse | ||
| prop-elem-deleteAll x y (z ∷ zs) | ||
| | False | ||
| with x == z in eqxz | ||
| ... | True | ||
| rewrite equality x z eqxz | eqSymmetry y z | eqyz | ||
| = refl | ||
| ... | False | ||
| = recurse | ||
|
|
||
| -- | An item is an element of the 'nub' iff it is | ||
| -- an element of the original list. | ||
| -- | ||
| prop-elem-nub | ||
| : ∀ ⦃ _ : Eq a ⦄ ⦃ _ : IsLawfulEq a ⦄ | ||
| (x : a) (ys : List a) | ||
| → elem x (nub ys) | ||
| ≡ elem x ys | ||
| -- | ||
| prop-elem-nub x [] = refl | ||
| prop-elem-nub x (y ∷ ys) | ||
| rewrite prop-elem-deleteAll x y (nub ys) | ||
| with x == y | ||
| ... | True = refl | ||
| ... | False = prop-elem-nub x ys | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| module Haskell.Data.Ord where | ||
|
|
||
| open import Haskell.Prelude | ||
|
|
||
| comparing : ⦃ Ord a ⦄ → (b → a) → b → b → Ordering | ||
| comparing p x y = compare (p x) (p y) | ||
|
|
||
| clamp : ⦃ Ord a ⦄ → (a × a) → a → a | ||
| clamp (low , high) a = min high (max a low) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.