-
Notifications
You must be signed in to change notification settings - Fork 198
Unique vectors #550
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
Closed
Closed
Unique vectors #550
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d82170f
some updates to vectors to allow for reuse in unique mapping
TimWhiting e95389f
update vector test, reorganize and add a few more vector APIs, and ad…
TimWhiting 128e1bd
add some inline annotations
TimWhiting 633e3f5
add more test cases, address review comments
TimWhiting 1120792
update an old reference in a comment
TimWhiting 96135db
update the test
TimWhiting 9e33531
add some erroring tests
TimWhiting 837709f
Only allow maximally divergent functions for in-place update of local…
TimWhiting 03f181e
improve doc comments
TimWhiting 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
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
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
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,129 @@ | ||
| // 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 | ||
|
|
||
| fun update-trace(xs: list<int>): list<int> | ||
| trace(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] | ||
| std/core/types/@byref(a1).update(2, update-trace) | ||
| 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 | ||
| std/core/types/@byref(a2).update(2, update-trace) | ||
| a2.map(fn(x) x.show).join("").println | ||
| a3.map(fn(x) x.show).join("").println | ||
|
|
||
|
|
||
| // TODO: Needs to be fixed!!! | ||
| extend type exception-info | ||
| ExnL(l: list<int>) | ||
|
|
||
| fun update-throw(x: list<int>) | ||
| println(if x.unsafe-is-unique then "unique a" else "not unique a") | ||
| throw-exn(Exception("err", ExnL(x))) | ||
|
|
||
| fun update-throw2(x: list<int>) | ||
| println(if x.unsafe-is-unique then "unique a" else "not unique a") | ||
| throw(x.show) | ||
|
|
||
| // The nice thing about multiple references is that it will always be copied - so there is no issue | ||
| // The problem with exceptions, is the reference count is too low | ||
| // | ||
| // fun test-error1() | ||
| // println("Non-fip update to item in local vector variable") | ||
| // var a2 := vector-init-total(3, fn(i) [i]) | ||
| // val a3 = a2 | ||
| // try { | ||
| // update(std/core/types/@byref(a2), 2, update-throw) | ||
| // a2.map(fn(x) x.show).join("").println | ||
| // } fn(err) { | ||
| // match err.info | ||
| // ExnL(l) -> println(if l.unsafe-is-unique then "unique a" else "not unique a") | ||
| // a2.foreach(fn(x) x.show.trace) | ||
| // } | ||
|
|
||
| // fun test-error2() | ||
| // println("Non-fip update to item in local vector variable") | ||
| // val a4 = vector-init-total(3, fn(i) [i]) | ||
| // var a5 := a4 | ||
| // try { | ||
| // update(std/core/types/@byref(a5), 2, update-throw) | ||
| // a5.map(fn(x) x.show).join("").println | ||
| // } fn(err) { | ||
| // match err.info | ||
| // ExnL(l) -> println(if l.unsafe-is-unique then "unique a" else "not unique a") | ||
| // a5.foreach(fn(x) x.show.trace) | ||
| // } | ||
|
|
||
| // fun test-error3() | ||
| // println("Non-fip update to item in local vector variable") | ||
| // val a6 = vector-init-total(3, fn(i) [i]) | ||
| // var a7 := a6 | ||
| // try { | ||
| // update(std/core/types/@byref(a7), 2, update-throw) | ||
| // a7.map(fn(x) x.show).join("").println | ||
| // } fn(err) { | ||
| // match err.info | ||
| // ExnL(l) -> println(if l.unsafe-is-unique then "unique a" else "not unique a") | ||
| // a7.foreach(fn(x) x.show.trace) | ||
| // } | ||
|
|
||
| // This works for some reason? | ||
| // fun test-error4() | ||
| // println("Non-fip update to item in local vector variable") | ||
| // val a6 = vector-init-total(3, fn(i) [i]) | ||
| // var a7 := a6 | ||
| // update(std/core/types/@byref(a7), 2, update-throw) |
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.