Skip to content

Got sets working with new syntax and fixed tests. #1348

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions lib/set.dx
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import sort

'## Monoidal enforcement of uniqueness in sorted lists

def last(xs:n=>a) -> Maybe a given (n|Ix, a) =
def last(xs:n=>a) -> Maybe a given (n|Ix, a:Type) =
s = size n
case s == 0 of
True -> Nothing
False -> Just xs[unsafe_from_ordinal (unsafe_nat_diff s 1)]

def first(xs:n=>a) -> Maybe a given (n|Ix, a) =
def first(xs:n=>a) -> Maybe a given (n|Ix, a:Type) =
s = size n
case s == 0 of
True -> Nothing
False -> Just xs[unsafe_from_ordinal 0]

def all_except_last(xs:n=>a) -> List a given (n|Ix, a) =
shortSize = Fin (size n -| 1)
allButLast = for i:shortSize. xs[unsafe_from_ordinal (ordinal i)]
AsList _ allButLast
def all_except_last(xs:n=>a) -> List a given (n|Ix, a:Type) =
newlen = size n -| 1
allButLast = for i:(Fin newlen). xs[unsafe_from_ordinal (ordinal i)]
AsList newlen allButLast

def merge_unique_sorted_lists(xlist:List a, ylist:List a) -> List a given (a|Eq) =
# This function is associative, for use in a monoidal reduction.
Expand All @@ -39,8 +39,7 @@ def merge_unique_sorted_lists(xlist:List a, ylist:List a) -> List a given (a|Eq)

def remove_duplicates_from_sorted(xs:n=>a) -> List a given (n|Ix, a|Eq) =
xlists = for i:n. (AsList 1 [xs[i]])
reduce (AsList 0 []) merge_unique_sorted_lists xlists

reduce xlists (AsList 0 []) merge_unique_sorted_lists

'## Sets

Expand Down Expand Up @@ -82,7 +81,7 @@ def set_intersect(
UnsafeAsSet(nx, xs) = sx
UnsafeAsSet(ny, ys) = sy
# This could be done in O(nx + ny) instead of O(nx log ny).
isInYs = \x. case search_sorted_exact ys x of
isInYs = \x:a. case search_sorted_exact ys x of
Just x -> True
Nothing -> False
AsList(n', intersection) = filter xs isInYs
Expand Down
8 changes: 4 additions & 4 deletions tests/set-tests.dx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import set

-- check order invariance.
# check order invariance.
:p (to_set ["Bob", "Alice", "Charlie"]) == (to_set ["Charlie", "Bob", "Alice"])
> True

-- check uniqueness.
# check uniqueness.
:p (to_set ["Bob", "Alice", "Alice", "Charlie"]) == (to_set ["Charlie", "Charlie", "Bob", "Alice"])
> True

Expand Down Expand Up @@ -57,13 +57,13 @@ Person : Type = Element names2
:p size Person
> 3

-- Check that ordinal and unsafeFromOrdinal are inverses.
# Check that ordinal and unsafeFromOrdinal are inverses.
roundTrip = for i:Person.
i == (unsafe_from_ordinal (ordinal i))
:p all roundTrip
> True

-- Check that member and value are inverses.
# Check that member and value are inverses.
roundTrip2 = for i:Person.
s = value i
ix = member s names2
Expand Down
Loading