{
  "$type": "site.standard.document",
  "bskyPostRef": {
    "cid": "bafyreih2ccnlbdxbkhw6itkcvdyjrcedmw447kkeoch4mquhui6zpmuydu",
    "uri": "at://did:plc:pi6woz4d47bkuws673w2il2r/app.bsky.feed.post/3mgmwh4z3nlt2"
  },
  "path": "/t/synthesizing-runtimerep-indexed-type-class-instances-in-a-ghc-plugin-to-simulate-monomorphization/13774#post_3",
  "publishedAt": "2026-03-09T09:36:36.000Z",
  "site": "https://discourse.haskell.org",
  "textContent": "simonpj:\n\n> Make a copy of the source code for `f`, specifically for `Double#`, and compile that. Now we have two versions of `f`. That’s fine but it’s hard to know which types to specialise `f` for\n\nI’ve been thinking about this recently and I am exploring this option. Instead of generating all possible options in advance, we could generate the code only at call sides.\n\nA further problem, then, is that the function may be called at the same type in different places. You’d want to share specializations. I want to see if we could do two passes:\n\n  1. Compile (maybe just up to type checking?) the package while keeping the runtime rep polymorphic functions as holes (in the sense of backpack). Also collect info on all calls.\n  2. Generate a module with all specialisations for the calls we found and instantiate the hole with that module.\n\n\n\nThis does only work if we can rule out existential quantification and polymorphic recursion. To address fhat, I am also thinking about extending the type system to let us specify that a certain type variable must be monomorphised. That could be used to rule out problematic polymorphism.\n\nThis also could be nice for certain other type classes like `PrimMonad` or even `Monad` in general, which often see huge benefits from specialization, so you want to specialise those even if it leads to larger binaries and longer compile times.",
  "title": "Synthesizing `RuntimeRep`-indexed type class instances in a GHC plugin to simulate monomorphization"
}