forked from koka-lang/koka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/unique-vectors'
- Loading branch information
Showing
5 changed files
with
257 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,62 @@ | ||
// Update function | ||
fun update(xs: list<int>): console list<int> | ||
println(if xs.unsafe-is-unique then "unique a" else "not unique a") | ||
xs.map fn(x) | ||
x + 1 | ||
|
||
// This is unsafe, since the behavior that depends on this check needs to be observationally equivalent | ||
inline fip extern unsafe-is-unique( ^v : list<a> ) : bool | ||
c inline "kk_datatype_is_unique(#1, kk_context())" | ||
|
||
effect choice | ||
ctl choice() : int | ||
|
||
// https://github.com/koka-lang/koka/issues/544#issuecomment-2155184608 Anton's example | ||
fun antons-example() | ||
// Nondeterministic choice, should copy vectors | ||
var a := vector-init-total(3, fn(i) 0) | ||
a[1] := 1 | ||
val xs = | ||
with handler | ||
return(x) [x] | ||
ctl choice() | ||
resume(0) ++ resume(1) | ||
a.map(fn(x) abs(x - choice())) | ||
xs.map(fn(v) v.map(show).join(",")).println | ||
() | ||
|
||
fun main() | ||
println("Non-fip copy of vector and items") | ||
val v = [[1], [2], [3], [4]].vector | ||
val xs = v | ||
// Copies the vector and the items are no longer unique | ||
v.map(update).map(fn(x) x.show).join("").println | ||
xs.map(fn(x) x.show).join("").println | ||
|
||
// Items are unique, since the vector is unique | ||
println("Fip updates to items in vector") | ||
val v1 = [[1], [2], [3], [4]].vector | ||
v1.map(update).map(fn(x) x.show).join("").println | ||
|
||
println("Fip updates to vector, but only one items") | ||
val v2 = [[1], [2], [3], [4]].vector | ||
val v3 = v2.set(0, [-10]) | ||
val v4 = v3.update(1, fn(l) l.update()) | ||
v4.map(fn(x) x.show).join(",").println | ||
|
||
println("Non-fip copies of vector in multiple resumptions") | ||
antons-example() | ||
|
||
// Fip update to item in local vector variable | ||
println("Fip update to item in local vector variable") | ||
var a1 := vector-init-total(3, fn(i) [i]) | ||
a1[1] := [5] | ||
update(std/core/types/@byref(a1), 2, update) | ||
a1.map(fn(x) x.show).join("").println | ||
|
||
println("Non-fip update to item in local vector variable") | ||
var a2 := vector-init-total(3, fn(i) [i]) | ||
val a3 = a2 | ||
update(std/core/types/@byref(a2), 2, update) | ||
a2.map(fn(x) x.show).join("").println | ||
a3.map(fn(x) x.show).join("").println |
This file contains 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,25 @@ | ||
Non-fip copy of vector and items | ||
not unique a | ||
not unique a | ||
not unique a | ||
not unique a | ||
[2][3][4][5] | ||
[1][2][3][4] | ||
Fip updates to items in vector | ||
unique a | ||
unique a | ||
unique a | ||
unique a | ||
[2][3][4][5] | ||
Fip updates to vector, but only one items | ||
unique a | ||
[-10],[3],[3],[4] | ||
Non-fip copies of vector in multiple resumptions | ||
["0,1,0","0,1,1","0,0,0","0,0,1","1,1,0","1,1,1","1,0,0","1,0,1"] | ||
Fip update to item in local vector variable | ||
unique a | ||
[0][5][3] | ||
Non-fip update to item in local vector variable | ||
not unique a | ||
[0][1][3] | ||
[0][1][2] |