Skip to content

Closures with Fix1 for type stability #69

@cscherrer

Description

@cscherrer

Hi @thautwarm ,

In a new version of Soss, I'm trying to really nail down type stability. Using GG I was having lots of trouble getting constructions like

For(n -> Normal+ β * x[n], σ), 1:N)

to be type-stable.

After lots of exploring, I realized this could be solved by putting this function outside of GG:

f(ctx, n) = Normal(ctx.α + ctx.β * ctx.x[n], ctx.σ)
f(ctx::NamedTuple) = Base.Fix1(f, ctx)

Then within GG, it could be called as

For(f((;α,β,σ,x)), 1:N)

That works great, but it's tricky to automate, since the f needs to be made available separately within the module, and I'd rather not use eval to put it there.

I eventually found this to work:

For(let f = ctx -> Base.Fix1(ctx) do ctx, n
            Normal(ctx.α + ctx.β * ctx.x[n], ctx.σ)
        end
        f((; α, β, σ, x))
    end, 1:N)

So now, I think I can just rewrite things this way before calling GG. But I wanted to check with you before doing this, since it seems plausible the current Closure approach could be updated to something similar, and maybe it would help other cases with type stability. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions