When you update a record in OCaml does the object get reallocated


I think so. It wouldn't be clever enough to know that I no longer need the object anymore all the time and compile it to a mutation appropriately

That's why Haskell people use state monads isn't it? There must be special GHC tricks to recognize that past state is no longer live so it can therefore compile to mutating the state object

Look I really don't want to think about the efficiency of state monads in OCaml

@ionchy If you need efficiency, genuine state is always available. Also record fields can be marked mutable

@jordyd I think I will write "if you want efficiency there are solutions. don't ask me"

@ionchy As far as I know State (from transformers) has no special magic. ST on the other hand does (it's basically a safe wrapper around IO), but they are designed for somewhat different things.

@ionchy Realistically the way allocations get optimized out is probably that ghc replaces e.g. a traversal over a state monad with plain tail recursion with unboxed arguments.

Sign in to participate in the conversation

A Mastodon instance for programming language theorists and mathematicians. Or just anyone who wants to hang out.