Skip to content
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
2 changes: 1 addition & 1 deletion kklib/include/kklib/ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static inline kk_unit_t kk_ref_set_borrow(kk_ref_t r, kk_box_t value, kk_context
}

// In Koka we can constrain the argument of f to be a local-scope reference.
static inline kk_box_t kk_ref_modify(kk_ref_t r, kk_function_t f, kk_context_t* ctx) {
static inline kk_box_t kk_ref_access(kk_ref_t r, kk_function_t f, kk_context_t* ctx) {
return kk_function_call(kk_box_t,(kk_function_t,kk_ref_t,kk_context_t*),f,(f,r,ctx),ctx);
}

Expand Down
16 changes: 12 additions & 4 deletions lib/std/core/types.kk
Original file line number Diff line number Diff line change
Expand Up @@ -351,15 +351,23 @@ pub inline extern ref/(!)<h,a,e>( ref : ref<h,a>, ?hdiv : hdiv<h,a,e>) : <read<h
cs inline "#1.Value"
js inline "#1.value"

// Modify the value of a reference.
// This is especially useful when the reference contains a vector, because
// Access the value of a reference, by evaluating a function with the reference as a local-var.
// Assigning to this var within the block using `:=` will modify the underlying reference.
//
// e.g. `counter.access(fn(i) { i := i + 1 })`
//
// This function is especially useful when the reference contains a vector, because
// getting the vector into a local variable and then setting it back into the reference later
// would mean that we hold on to a copy (and we can't update the vector in place then!).
// In contrast, this function passes the ownership to the given function.
pub inline extern modify<h,a,b,e>( ref : ref<h,a>, f : forall<s> local-var<s,a> -> <local<s>|e> b, ?hdiv : hdiv<h,a,e>) : <read<h>,write<h>|e> b
c inline "kk_ref_modify(#1,#2,kk_context())"
pub inline extern access<h,a,b,e>( ref : ref<h,a>, f : forall<s> local-var<s,a> -> <local<s>|e> b, ?hdiv : hdiv<h,a,e>) : <read<h>,write<h>|e> b
c inline "kk_ref_access(#1,#2,kk_context())"
js inline "((#2)(#1))"

// Deprecated: alias for `access`
pub inline extern modify<h,a,b,e>( ref : ref<h,a>, f : forall<s> local-var<s,a> -> <local<s>|e> b, ?hdiv : hdiv<h,a,e>) : <read<h>,write<h>|e> b
c "kk_ref_access"

// If a heap effect is unobservable, the heap effect can be erased by using the `run` fun.
// See also: _State in Haskell, by Simon Peyton Jones and John Launchbury_.
pub extern run<e,a>( action : forall<h> () -> <alloc<h>,read<h>,write<h> | e> a ) : e a
Expand Down
4 changes: 2 additions & 2 deletions test/lib/ref-modify.kk → test/lib/ref-access.kk
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fun main() {
val r = ref(0)
r.modify( fn(l) l := 1 )
r.access( fn(l) l := 1 )
(!r).println
}
}
File renamed without changes.