Workflow: Calling stochastic functions for instances #2489
Replies: 1 comment 1 reply
-
This might make things messy, because nulls are a frequent source of errors. ExampleWe have a similar pattern in SquiggleProject/ProjectItem, where fields It leads to this awkward code: async run(context: ReducerContext, externals: ImmutableMap<string, Value>) {
if (this.output) {
return;
}
this.buildAst();
if (!this.ast) {
// buildAst() guarantees that the ast is set
throw new Error("Internal logic error");
} (this is repeated for each step) This is because the information that the field is now set is not reflected in how the code organized, and not specified in types. So when you set a field, you have to remember that yourself and analyze the code flow in your head to make sure that there are no bugs. When you put the code for generating each field in methods, you don't have any guarantees about the order in which the methods will be called. So if your data shapes go through Alternative approachAs usual: inline it? :) I mean something like this:
Here, the order of statements expresses the dependencies between steps, so types are guaranteed to be correct. This is how I would write this code even in JS, instead of mutating the original person object. |
Beta Was this translation helpful? Give feedback.
-
One pattern that's emerged in the recent Virus model, is one in which we kept adding specific parameters to a "Room". As a simple example, imagine that I'm generating a person:
In some cases, I might want to generate a gender, like:
Later, I might want to estimate lifespan. This would ideally use the
age
andgender
functions. But it would be annoying to track these. Right now, I could do something like,This gets really messy, because we now have a
person
object that changes in params based on it's previous calls, and we need to keep track of what's been called before calling other things. Here, we'd need to callgetGender
before we callexpectedLifepan
.In JS, we could solve this with something like a class with some caching/memoization.
GPT4 recommend this in OCaml. Having some Some type would be useful for this.
GPT4 recommends to either use the mutability or laziness features of OCAML. Without those, it seems tough.
I'm not really sure how to best this with an immutable language with Squiggle at this point. I imagine that Structs could help a lot here, especially if there are many params, and each/most can be None/undefined.
Memoization (#1606) could likely do the job, though might be tricky to get right.
Beta Was this translation helpful? Give feedback.
All reactions