Skip to content

Instantly share code, notes, and snippets.

@haller33
Last active December 28, 2022 11:59
Show Gist options
  • Save haller33/561d24a98c7c0cd04124b214eb21da48 to your computer and use it in GitHub Desktop.
Save haller33/561d24a98c7c0cd04124b214eb21da48 to your computer and use it in GitHub Desktop.
monadic return procedure with state using monads
package context_monad
import "core:fmt"
import "core:mem"
// SSA
// https://www.cs.purdue.edu/homes/suresh/502-Fall2008/papers/kelsey-ssa-cps.pdf
a : int = 0
multplo_global :: proc ( base_of : int ) -> proc ( int ) -> int {
a = base_of
return proc ( num_test : int ) -> int {
return a * num_test
}
}
// https://arxiv.org/pdf/1803.10195.pdf
multplo_monadic :: proc ( monad_context : ^int, base_of : int ) -> ( ^int, proc ( ^int, int ) -> ( ^int, int ) ) {
monad_context^ = base_of
return monad_context , proc ( monad_context : ^int, num_test : int ) -> ( ^int, int) {
return monad_context, ((cast(int) monad_context^) * num_test)
}
}
multplo_context :: proc ( base_of : int, m := context.user_ptr ) -> proc ( int, rawptr ) -> int {
(cast (^int) m)^ = base_of
return proc ( num_test : int, m := context.user_ptr ) -> int {
return ((cast(^int) m)^ * num_test)
}
}
// unitMonad :: 0
main :: proc () {
{
data_base : int = 2
unitMonad : int = 0
monadC : ^int
tmp : int
multTwo : proc ( ^int, int ) -> ( ^int, int )
// context.user_ptr = &data
monadC, multTwo = multplo_monadic ( &unitMonad, data_base )
monadC, tmp = multTwo ( monadC, 10 )
fmt.println ( tmp )
}
{
data : int
multTwo : proc ( int, rawptr ) -> ( int )
//
context.user_ptr = (cast(rawptr) &data)
multTwo = multplo_context ( 2 )
tmp := multTwo ( 10, context.user_ptr )
fmt.println ( tmp )
}
}
@haller33
Copy link
Author

the version of Odin that i was using is

$ odin version
odin version dev-2022-12:521ed286

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment