Skip to content

Static pointers: the static keyword and Mhs.StaticPtr - #12

Draft
dmjio wants to merge 5 commits into
jsvalfrom
static-ptr
Draft

dmjio wants to merge 5 commits into
jsvalfrom
static-ptr

Conversation

@dmjio

@dmjio dmjio commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

Adds static pointers (augustss#498), so that miso's LynxJS backend (-fnative) can be built with MicroHs.

import Mhs.StaticPtr

inc :: Int -> Int
inc = (+ 1)

p :: StaticPtr (Int -> Int)
p = static inc

main :: IO ()
main = do
  print (deRefStaticPtr p 1)                  -- 2
  putStrLn (staticPtrName p)                  -- Main.p$static0
  case unsafeLookupStaticPtr (staticPtrName p) of
    Just q  -> print (deRefStaticPtr q (10 :: Int) :: Int)   -- 11
    Nothing -> putStrLn "not found"

How it works

  • No parser change. static is an ordinary identifier. When the program has no static of its own in scope, the type checker uses Mhs.StaticPtr._static :: a -> StaticPtr a (through Mhs.Builtin).
  • Lifting (Desugar.liftStatic). Every static e in a definition f becomes a top level definition f$staticN = _mkStaticPtr "M.f$staticN" e. The name is the key: it is the same in every run of the same program.
  • Closedness. It is an error if e uses a variable bound in the enclosing definition (this includes a dictionary from the context of the enclosing function), or if static is not applied to an argument.
  • The table (Main.addStaticPtrs). When linking, Mhs.StaticPtr._staticPtrTable is replaced by the list of all the $static definitions, the same way MicroHs.Embed.packages is filled in.

Compared to GHC

  • No Typeable constraint.
  • No restriction on the type of e, except that it cannot need a dictionary from the enclosing function. Polymorphic pointers work (see tests/StaticPtrPoly.hs); bad :: Show a => a -> StaticPtr (a -> String); bad _ = static show is rejected as not closed.
  • A closed local (let/where) binding can only be used in e when the desugarer inlines it (used once, or a variable or literal). GHC accepts all closed local bindings.
  • The key is a String name (staticPtrName), not a Fingerprint: base has no GHC.Fingerprint (it is in ghc-compat, which is not included by default). A GHC.StaticPtr with StaticKey = Fingerprint can be a small module on top of this one that hashes the name (base has System.IO.MD5). Its keys would still not match GHC's, which come from the unit id, the module and a counter; static keys only mean something within one build of one program anyway.

Tested

  • tests/StaticPtr.hs, added to all.tests; all.tests and err.tests pass with a compiler built from this branch.
  • tests/StaticPtrPoly.hs, also in all.tests: polymorphic static pointers (static length :: StaticPtr ([a] -> Int), static id, static Just) used at several types, static inside a polymorphic function, and a polymorphic pointer looked up by name.
  • miso 1.15.0.0 with the native flag installs with mcabal, and sample-app-native compiles and links with emcc (-sWASM=0 -sENVIRONMENT=shell).
  • A staticKey / unsafeLookupStaticPtr round trip through a GHC.StaticPtr shim that hashes the name, as JavaScript only, under node. (miso's committed ffi/mhs/GHC/StaticPtr.hs is still the stub, where unsafeLookupStaticPtr always returns Nothing.)

Not tested: a program running in a Lynx host.

Not done

  • No README entry.
  • No lynx target in mhs.conf.in.

'static e' has type StaticPtr t when the closed expression e has type t.
The desugarer moves e to a top level definition of its own, whose name
identifies the pointer in every run of the program, and the linker puts
all of them in a table, so a name can be looked up again
(unsafeLookupStaticPtr).

'static' is only a keyword when the program has no 'static' of its own
in scope.  There is no Typeable constraint.

See augustss#498.

Co-Authored-By: Claude <noreply@anthropic.com>
@dmjio dmjio changed the title Static pointers: the static keyword and Mhs.StaticPtr Static pointers: the static keyword and Mhs.StaticPtr Nov 12, 2026
dmjio and others added 4 commits November 12, 2026 17:02
Co-Authored-By: Claude <noreply@anthropic.com>
generated/mhs.c and generated/base.pkg regenerated (mhs.c at a fixpoint).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
generated/mhs.c and generated/base.pkg regenerated (mhs.c at a fixpoint).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
static length, id, Just at more than one type, static inside a
polymorphic function, and a polymorphic pointer looked up by name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant