Skip to content
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

Add immutable ordered finite 'map' and 'set' to the stdlib #561

Open
wants to merge 29 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
76080ef
Initial draft of (tree) maps & sets
jiribenes Aug 30, 2024
418dc4f
Add a quick unit test
jiribenes Aug 30, 2024
992e4c4
Use pattern guards in rotateL, rotateR
jiribenes Aug 30, 2024
9236b29
Fix wrong right rotation
jiribenes Aug 30, 2024
a1995da
Add a few more regression tests
jiribenes Aug 30, 2024
55f2a50
Update tests once again
jiribenes Aug 30, 2024
e94e51f
Add a 'counter' example
jiribenes Aug 30, 2024
c2ad2c0
Use '///' for doc comments
jiribenes Oct 2, 2024
bc6bf33
Move internal tools into their own namespace
jiribenes Oct 2, 2024
5cb7ae8
Add internal namespace for set, fix missing brace
jiribenes Oct 4, 2024
3265837
Re-add forgotten unit test for treesets
jiribenes Oct 4, 2024
dac0e5e
Ignore map&set stdlib tests on Chez and LLVM
jiribenes Oct 4, 2024
678e4d6
Reformat treemap source
jiribenes Oct 4, 2024
0f15df3
Rename speech in tests
jiribenes Oct 4, 2024
7f6559c
Add intersection for treeset and treemap, incl. tests
jiribenes Oct 4, 2024
209bfc8
Plug map&set into 'stream'
jiribenes Nov 25, 2024
6bfdbb3
Fix set/unique test
jiribenes Dec 10, 2024
537419e
Ignore added stream tests on Chez & LLVM
jiribenes Dec 10, 2024
62560d1
Fix expected output in stream/map
jiribenes Dec 10, 2024
47f5e30
Fix expected output in stream/map (again)
jiribenes Dec 10, 2024
f534faa
Add a few more useful functions
jiribenes Dec 12, 2024
daf398c
Add a few more tests for weird/complex operations
jiribenes Dec 12, 2024
cdd3e6b
Add map::filter & a caching test
jiribenes Dec 12, 2024
127c982
Use an explicit comparison function
jiribenes Dec 13, 2024
eff01b5
Fix subtle, yet obvious bugs, duh
jiribenes Dec 13, 2024
ed9ca11
Attempt to fix tests
jiribenes Dec 13, 2024
e488a74
More bug fixing
jiribenes Dec 13, 2024
12f7061
Try running 'map' and 'set' tests on Chez and LLVM
jiribenes Dec 13, 2024
fd5b9b3
Change 'internal::prettyPairs' to take show instances
jiribenes Dec 13, 2024
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
4 changes: 3 additions & 1 deletion effekt/jvm/src/test/scala/effekt/StdlibTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ abstract class StdlibChezTests extends StdlibTests {
examplesDir / "stdlib" / "bytearray",
examplesDir / "stdlib" / "io",
examplesDir / "stdlib" / "stream" / "characters.effekt",
examplesDir / "stdlib" / "stream" / "fuse_newlines.effekt"
examplesDir / "stdlib" / "stream" / "fuse_newlines.effekt",
examplesDir / "stdlib" / "stream" / "map.effekt",
)
}
class StdlibChezSchemeMonadicTests extends StdlibChezTests {
Expand All @@ -48,6 +49,7 @@ class StdlibLLVMTests extends StdlibTests {
examplesDir / "stdlib" / "bytearray" / "bytearray.effekt",
examplesDir / "stdlib" / "stream" / "characters.effekt",
examplesDir / "stdlib" / "stream" / "fuse_newlines.effekt",
examplesDir / "stdlib" / "stream" / "map.effekt",
examplesDir / "stdlib" / "io" / "filesystem" / "async_file_io.effekt",
examplesDir / "stdlib" / "io" / "filesystem" / "files.effekt",
examplesDir / "stdlib" / "io" / "filesystem" / "wordcount.effekt",
Expand Down
7 changes: 7 additions & 0 deletions examples/stdlib/map/cache.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Get key1: value1
Get key2: value2
Get key1: newValue1
Get key2: value2
After cleaning:
Get key1: newValue1
Get key2: Not found
49 changes: 49 additions & 0 deletions examples/stdlib/map/cache.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import map

record CacheEntry[V](value: V, timestamp: Int)

effect time(): Int

def main() = {
var currentTime = 0
try {
var cache: Map[String, CacheEntry[String]] = map::empty(compareString)
val maxAge = 8

def cachePut(key: String, value: String): Unit =
cache = cache.put(key, CacheEntry(value, do time()))

def cacheGet(key: String): Option[String] =
cache.get(key) match {
case Some(entry) and do time() - entry.timestamp < maxAge => Some(entry.value)
case _ => None()
}

def cleanExpiredEntries(): Unit = {
val currentTime = do time()
cache = cache.filter { (_, entry) =>
currentTime - entry.timestamp < maxAge
}
}

cachePut("key1", "value1")
cachePut("key2", "value2")

println("Get key1: " ++ cacheGet("key1").getOrElse { "Not found" })
println("Get key2: " ++ cacheGet("key2").getOrElse { "Not found" })

cachePut("key1", "newValue1")

println("Get key1: " ++ cacheGet("key1").getOrElse { "Not found" })
println("Get key2: " ++ cacheGet("key2").getOrElse { "Not found" })

cleanExpiredEntries()

println("After cleaning:")
println("Get key1: " ++ cacheGet("key1").getOrElse { "Not found" })
println("Get key2: " ++ cacheGet("key2").getOrElse { "Not found" })
} with time {
currentTime = currentTime + 1
resume(currentTime)
}
}
7 changes: 7 additions & 0 deletions examples/stdlib/map/counter.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ask: 3
can: 3
you: 3
see: 0
do: 4
Effekt: 2
[Effekt → 2, and → 1, ask → 3, but → 1, can → 3, do → 4, fellow → 2, for → 4, language → 2, man → 1, my → 2, not → 2, of → 2, programmers → 2, programs → 1, so → 1, the → 2, together → 1, we → 1, what → 4, will → 1, world → 1, you → 3, your → 2]
39 changes: 39 additions & 0 deletions examples/stdlib/map/counter.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import map

def counter(words: List[String]): Map[String, Int] = {
var m: Map[String, Int] = map::empty(compareString);

list::foreach(words) { word =>
m = m.putWithKey(word, 1) { (_, n1, n2) => n1 + n2 }
}

m
}

def main() = {
// John F. Kennedy's Inaugural Address, Jan 20, 1961; modified for Effekt
val speech: List[String] = [
"and", "so", "my", "fellow", "Effekt", "programmers",
"ask", "not", "what", "your", "language", "can", "do", "for", "you",
"ask", "what", "you", "can", "do", "for", "your", "language",
"my", "fellow", "programmers", "of", "the", "world",
"ask", "not", "what", "Effekt", "will", "do", "for", "you",
"but", "what", "together", "we", "can", "do", "for", "the", "programs", "of", "man"
]

val ctr: Map[String, Int] = counter(speech)

def test(word: String) = {
val count = ctr.getOrElse(word) { 0 }
println(word ++ ": " ++ count.show)
}

test("ask")
test("can")
test("you")
test("see")
test("do")
test("Effekt")

println(map::internal::prettyPairs(ctr.toList) { s => show(s) } { n => show(n) })
}
7 changes: 7 additions & 0 deletions examples/stdlib/map/map.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[0 → Hello, 1 → World, 2 → Woo!]
[-1 → Hullo, 0 → Hello, 1 → World, 2 → Woo!]
[-10 → EY, -1 → Hullo, 0 → Hello, 1 → World, 2 → Woo!]
[0 → Hello, 2 → Woo!, 42 → Whole new world!, 100 → Big, 1000 → Bigger, 10000 → Biggest!]
[-1 → Huh?!, 0 → Hello, 1 → Foo, 2 → Woo!, 42 → Whole new world!, 100 → Big, 1000 → Bigger, 10000 → Biggest!]
[-1 → Huh?!, 0 → Hello, 1 → Foo, 2 → Woo!, 42 → Whole new world!, 100 → Big, 1000 → Bigger, 10000 → Biggest!]
[0 → Hello, 1 → World, 2 → Woo!]
28 changes: 28 additions & 0 deletions examples/stdlib/map/map.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import map

def main() = {
val l = [(0, "Hello"), (1, "World"), (2, "Woo!")]

val m = map::fromList(l, compareInt)
println(map::internal::prettyPairs(m.toList) { n => show(n) } { s => show(s) })

val m2 = m.put(-1, "Hullo")
println(map::internal::prettyPairs(m2.toList) { n => show(n) } { s => show(s) })

val m3 = m2.put(-10, "EY")
println(map::internal::prettyPairs(m3.toList) { n => show(n) } { s => show(s) })

// ...

val m4 = m.delete(1).put(42, "Whole new world!").put(100, "Big").put(1000, "Bigger").put(10000, "Biggest!")
println(map::internal::prettyPairs(m4.toList) { n => show(n) } { s => show(s) })

val m5 = map::fromList(Cons((1, "Foo"), Cons((-1, "Huh?!"), m4.toList.reverse)), compareInt)
println(map::internal::prettyPairs(m5.toList) { n => show(n) } { s => show(s) })

val m6: Map[Int, String] = m5.toList.fromList(compareInt)
println(map::internal::prettyPairs(m6.toList) { n => show(n) } { s => show(s) })

val nuMap = map::fromList(l.reverse, compareInt)
println(map::internal::prettyPairs(nuMap.toList) { n => show(n) } { s => show(s) })
}
2 changes: 2 additions & 0 deletions examples/stdlib/map/minmax.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Min: 5 -> Five
Max: 30 -> Thirty
16 changes: 16 additions & 0 deletions examples/stdlib/map/minmax.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import map

def show(opt: Option[(Int, String)]): String = opt match {
case Some((k, v)) => k.show ++ " -> " ++ v
case None() => "X"
}

def main() = {
val m = map::fromList([(10, "Ten"), (20, "Twenty"), (5, "Five"), (30, "Thirty")], compareInt)

val min: Option[(Int, String)] = m.getMin
val max: Option[(Int, String)] = m.getMax

println("Min: " ++ min.show)
println("Max: " ++ max.show)
}
8 changes: 8 additions & 0 deletions examples/stdlib/map/setops.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Union (keeping first value):
[1 → apple, 2 → banana, 3 → cherry, 4 → elderberry]

Union (combining values):
[1 → apple, 2 → banana/berry, 3 → cherry/date, 4 → elderberry]

Intersection (combining keys):
[2 → banana-berry, 3 → cherry-date]
20 changes: 20 additions & 0 deletions examples/stdlib/map/setops.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import map

def main() = {
// Create multiple maps
val map1 = map::fromList([(1, "apple"), (2, "banana"), (3, "cherry")], compareInt)
val map2 = map::fromList([(2, "berry"), (3, "date"), (4, "elderberry")], compareInt)

// Test union with different combine strategies
println("Union (keeping first value):")
println(map::internal::prettyPairs(map1.union(map2).toList) { n => show(n) } { s => show(s) })

println("\nUnion (combining values):")
val combinedMap = map1.union(map2, compareInt) { (k, v1, v2) => v1 ++ "/" ++ v2 }
println(map::internal::prettyPairs(combinedMap.toList) { n => show(n) } { s => show(s) })

// Test intersection
println("\nIntersection (combining keys):")
val intersectedMap = map1.intersection(map2, compareInt) { (k, v1, v2) => v1 ++ "-" ++ v2 }
println(map::internal::prettyPairs(intersectedMap.toList) { n => show(n) } { s => show(s) })
}
23 changes: 23 additions & 0 deletions examples/stdlib/set/unique.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ask: true
can: true
you: true
see: false
do: true
Effekt: true

Cons(Effekt, Cons(and, Cons(ask, Cons(but, Cons(can, Cons(do, Cons(fellow, Cons(for, Cons(language, Cons(man, Cons(my, Cons(not, Cons(of, Cons(programmers, Cons(programs, Cons(so, Cons(the, Cons(together, Cons(we, Cons(what, Cons(will, Cons(world, Cons(you, Cons(your, Nil()))))))))))))))))))))))))
sorted
Cons(Effekt, Cons(and, Cons(ask, Cons(but, Cons(can, Cons(do, Cons(fellow, Cons(for, Cons(language, Cons(man, Cons(my, Cons(not, Cons(of, Cons(programmers, Cons(programs, Cons(so, Cons(the, Cons(together, Cons(we, Cons(what, Cons(will, Cons(world, Cons(you, Cons(your, Nil()))))))))))))))))))))))))
sorted

Cons(after, Cons(around, Cons(better, Cons(do, Cons(ever, Cons(faster, Cons(harder, Cons(hour, Cons(is, Cons(it, Cons(make, Cons(makes, Cons(more, Cons(never, Cons(over, Cons(stronger, Cons(than, Cons(the, Cons(us, Cons(work, Cons(world, Nil())))))))))))))))))))))
sorted

lyrics / speech:
Cons(after, Cons(around, Cons(better, Cons(ever, Cons(faster, Cons(harder, Cons(hour, Cons(is, Cons(it, Cons(make, Cons(makes, Cons(more, Cons(never, Cons(over, Cons(stronger, Cons(than, Cons(us, Cons(work, Nil()))))))))))))))))))
speech / lyrics:
Cons(Effekt, Cons(and, Cons(ask, Cons(but, Cons(can, Cons(fellow, Cons(for, Cons(language, Cons(man, Cons(my, Cons(not, Cons(of, Cons(programmers, Cons(programs, Cons(so, Cons(together, Cons(we, Cons(what, Cons(will, Cons(you, Cons(your, Nil())))))))))))))))))))))
speech n lyrics:
Cons(do, Cons(the, Cons(world, Nil())))
speech u lyrics:
Cons(Effekt, Cons(after, Cons(and, Cons(around, Cons(ask, Cons(better, Cons(but, Cons(can, Cons(do, Cons(ever, Cons(faster, Cons(fellow, Cons(for, Cons(harder, Cons(hour, Cons(is, Cons(it, Cons(language, Cons(make, Cons(makes, Cons(man, Cons(more, Cons(my, Cons(never, Cons(not, Cons(of, Cons(over, Cons(programmers, Cons(programs, Cons(so, Cons(stronger, Cons(than, Cons(the, Cons(together, Cons(us, Cons(we, Cons(what, Cons(will, Cons(work, Cons(world, Cons(you, Cons(your, Nil()))))))))))))))))))))))))))))))))))))))))))
103 changes: 103 additions & 0 deletions examples/stdlib/set/unique.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import set

def unique(words: List[String]): Set[String] = {
var s: Set[String] = set::empty(compareString);

list::foreach(words) { word =>
s = s.insert(word)
}

s
}

def main() = {
// John F. Kennedy's Inaugural Address Jan 20 1961; modified for Effekt
val speech: List[String] = [
"and", "so", "my", "fellow", "Effekt", "programmers",
"ask", "not", "what", "your", "language", "can", "do", "for", "you",
"ask", "what", "you", "can", "do", "for", "your", "language",
"my", "fellow", "programmers", "of", "the", "world",
"ask", "not", "what", "Effekt", "will", "do", "for", "you",
"but", "what", "together", "we", "can", "do", "for", "the", "programs", "of", "man"
]

val uniqueSpeech: Set[String] = unique(speech)

def test(word: String) = {
val present = uniqueSpeech.contains(word)
println(word ++ ": " ++ present.show)
}

test("ask")
test("can")
test("you")
test("see")
test("do")
test("Effekt")

// ---
println("")

def testSorted(s: Set[String]) = {
val sorted = s.toList.isSortedBy { (x, y) => x <= y }
if (sorted) {
println("sorted")
} else {
println("unsorted")
}
}

println(uniqueSpeech.toList)
testSorted(uniqueSpeech)

println(set::fromList(speech, compareString).toList)
testSorted(set::fromList(speech, compareString))

// ---
println("")

// Around the World / Harder, Better, Faster, Stronger by Daft Punk (Alive 2007)
val lyrics: List[String] = [
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",

"work", "it", "make", "it", "do", "it", "makes", "us",
"harder", "better", "faster", "stronger",
"more", "than", "hour", "hour", "never",
"ever", "after", "work", "is", "over",

"work", "it", "make", "it", "do", "it", "makes", "us",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world",

"harder", "better", "faster", "stronger",
"around", "the", "world", "around", "the", "world",
"around", "the", "world", "around", "the", "world"
]

val uniqueLyrics = unique(lyrics)

println(uniqueLyrics.toList)
testSorted(uniqueLyrics)

// ---
println("")

println("lyrics / speech:")
println(uniqueLyrics.difference(uniqueSpeech).toList)

println("speech / lyrics:")
println(uniqueSpeech.difference(uniqueLyrics).toList)

println("speech n lyrics:")
println(uniqueLyrics.intersection(uniqueSpeech).toList)

println("speech u lyrics:")
println(uniqueLyrics.union(uniqueSpeech).toList)
}
7 changes: 7 additions & 0 deletions examples/stdlib/stream/map.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
1: H (72)
2: e (101)
3: l (108)
4: l (108)
5: o (111)
[1 → 72, 2 → 101, 3 → 108, 4 → 108, 5 → 111]
Hello
17 changes: 17 additions & 0 deletions examples/stdlib/stream/map.effekt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import stream
import map

def main() = {
val m = map::fromList([(1, 'H'), (2, 'e'), (3, 'l'), (4, 'l'), (5, 'o')], compareInt)

for[(Int, Char)] { each(m) } { case (k, v) =>
println(show(k) ++ ": " ++ show(v) ++ " (" ++ show(v.toInt) ++ ")")
}

val newMap = collectMap[Int, Char](compareInt) { each(m) }
println(map::internal::prettyPairs(newMap.toList) { n => show(n) } { c => show(c) })

val hello: String = collectString { eachValue(m) }
println(hello)
}

10 changes: 10 additions & 0 deletions libraries/common/effekt.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ type Ordering {
Greater()
}

def compareInt(n: Int, m: Int) =
if (n == m) Equal()
else if (n < m) Less()
else Greater()

def compareString(left: String, right: String) =
if (left == right) Equal()
else if (left < right) Less()
else Greater()
Comment on lines +164 to +167
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't work on LLVM, hence #748. Moreover, it's probably worth it to have a few of these compare functions prepared for more types than just Ints and Strings, but that's future work :)


extern pure def genericCompareImpl[R](x: R, y: R): Int =
js "$effekt.compare(${x}, ${y})"

Expand Down
Loading
Loading