Dear Laurent, Duncan, Mathieu, Facundo, EdskoI have spent a little while digging into static pointers recently. See my post below. I wonder if you have any comments on my proposal?Do you know anyone else I should consult?Thanks!SimonOn Fri, 7 Nov 2025 at 18:13, Simon Peyton Jones (@simonpj) <gitlab@gitlab.haskell.org> wrote:Simon Peyton Jones created an issue: #26556
Static pointers are not properly implemented. For example:
among others. Moreover, the implementation is very messy, scattered about in
FloatOutand elsewhere.Let's fix it.
Discussion
I embarked on what I thought would be a simple refactor to
- Identify static bindings in the type checker
- Promote them to top level desugarer
thereby avoiding all the terribly painful static-form-floating stuff that been an ongoing source of breakage and irritation.
Sadly it was not as simple as I had thought. Merge request !14994 is my work in progress
At first it seems simple: given
static e
- When typechecking
eensure that all its free variables are top-level defined- When desugaring, move
eto top levelApparently simple!
Complication 1.
emight generate constraints. We don't want to solve those from locally-bound Givens, because they'll be out of scope when we promote to top level.Solution: wrap the constraints in an implication with
SkolInfoofStaticFormSkol; and in the constraint solver zap all Givens when walking inside such an implication. That was done incommit 39d4a24beaa7874a69ffdc1528ca160818829169 Author: Simon Peyton Jones <simon.peytonjones@gmail.com> Date: Tue Sep 30 23:11:19 2025 +0100 Build implication for constraints from (static e) This commit addresses #26466, by buiding an implication for the constraints arising from a (static e) form. The implication has a special ic_info field of StaticFormSkol, which tells the constraint solver to use an empty set of Givens.So that complication wasn't at all bad.
Complication 2. What if we have
f x = let y = reverse "hello" in ...(static (y++y))...The free vars of the
staticare just{y}, andyis morally-top-level. It in turn has no free variables.Sadly (as it turns out) GHC tries to accept this case. When looking at the defn of
y(with nostaticin sight yet) the typechecker marks it at a "static binding", meaning that it too can (and indeed must) be floated to top level.So if the desugarer moves the
staticto the top level, it must moveytoo. And that means it must mark the typechecked binding in some way, so the desugarer can identify it. Not so hard, but there is quite a bit of new plumbing.Complication 3. But what if
y's RHS generates constraints, which use Givens (or solved dictionaries, which are very similar) from its context. E.g.f x = let p = x+1::Int; y = 2+3::Int in ...Now there may be a
d :: Num Intlying around from dealing withp, andymay use it. Oh no! Now that'll be out of scope if we moveyto top level.Plausible solution: use them same mechanism for static bindings as we did for
static eexpressions. That is, build an implication constraint whose SkolInfo says "zap Givens". This turned out to be considerably harder to implement than it was for Complication 1.Complication 4. What if
yis not generalised, perhaps because of the Monomorphism Restriction? e.g.f :: Num a => a -> blah f x = let y = 3+3 in (x+y, static( ..y.. ))Now
yis monomorphic and really does use the dictionary passed tof. So it really cannot appear in thestatic. Somehowyreally isn't static after all. We must reject this program. Not only is it an implementation mess (Complications 1,2,3 are already imposing quite a signficant implemenation burden) but it becomes pretty hard to explain to the programmer just which uses ofstaticare OK and which are not.What a swamp. At this point I threw up my hands and wrote this summary
Proposal
To me the solution is clear: the rule should be
- in
static e, all the free vars ofeshould be bound at top levelThat is a nice simple rule; it is easy to explain and easy to implement. It is also what the user manual says!
In retrospect, by addressing Complication 2 I was trying too hard! (And this extra feature is entirely undocumented.) I thought that I could deal with Complication 2 using the same mechanism as the one that deals with
MonoLocalBinds. But I was wrong.Making this change could perhaps break some programs. They would all be easy to fix, by moving bindings for any free variables to top level. But note that the status quo is not stable: it has bugs e.g #24464, #26545. What we have is at attempt to be clever that is simply wrong.
—
View it on GitLab.
You're receiving this email because of your activity on gitlab.haskell.org. Unsubscribe from this thread · Manage all notifications · Help