Quick agent write-up to satisfy curiosity for now (some of it is talking about in-progress stuff, nothing final here. I’m changing it to more closely match the thesis, and then we can discuss this later in the process in a proposal where that discussion belongs)
Function bodies across modules in the Dependent Haskell fork: export rules and an open question about instances
Type-level computation needs a function’s body: to check that pred (Succ n) reduces to n, the type checker must see the definition of pred. So a module boundary has to answer two questions for each function: is its type visible to importers, and is its body?
Starting point: Eisenberg’s thesis, §4.4.6
If a user includes pred in an import/export list, only the name pred and its type are involved. On the other hand, writing pred(..) (with a literal (..) in the source code) in the import/export list also includes pred’s implementation.
The syntax echoes Bool vs Bool(..) for constructors. Proposal #378 does not discuss this.
What we implement
- In a
-XDependentTypes module with an export list, f exports the name and the type of f, and f (..) also exports its body. In an importer, a call f x to a function whose body is not exported is stuck: it is a type equal only to itself.
- A module without an export list exports all bodies, following Haskell’s “no export list exports everything” (similar to Lean’s
@[expose] public section).
- A module without
-XDependentTypes exports all bodies as before (the Opt-In Principle).
- Import lists do not hide bodies. Here we deviate from the thesis.
import M (f (..)) is accepted and means the same as import M (f). Reasons:
- one interface file serves every importer;
- neither Idris 2 nor Lean 4 controls bodies on the import side (Lean’s
import all only adds);
- with the thesis rule,
import Prelude ((.)) would stop (.) from reducing in types, which we found too surprising.
This matches Idris 2 (export = type only, public export = type and body) and Lean 4 (def bodies are private unless marked @[expose]). Agda goes the other way (bodies are visible unless inside abstract or opaque).
Planned
- An exported body cannot use a hidden name. If
f (..)'s body calls a function g whose body is not exported, the defining module gets an error: export g (..), or export f without (..). This is the Idris 2 rule (“public export definitions cannot use private or export names”), and Lean 4 has the same idea (“No information from the private scope can be used in the public scope”). Today the fork instead publishes such helper bodies silently, which leaks bodies the author never exported.
- No computation with a hidden body. A closed call such as
f 4 of a function whose body is not exported will stay stuck in importers. Today it can still be evaluated by running the compiled code, which reveals the body.
Open question: instances
Haskell exports every instance, and an instance has no syntax to hide one method. So what should happen here?
module M (Bit (..), Turn (..)) where
class Turn a where
forward :: a -> a
turn :: Bit -> Bit -- not exported
turn Off = On
turn On = Off
instance Turn Bit where
forward = turn
Should forward Off reduce to On in a module that imports M?
- Error, following Lean 4, where an
instance always exposes its body: the instance uses the hidden turn, so you must export turn (..). Lean lets you mark a single instance field private; Haskell has no equivalent.
- Withhold: the method body is not exported because it uses a hidden name, so
forward Off is stuck in importers.
(Idris 2’s documentation does not cover the visibility of implementations.) We are measuring how much existing code each option breaks. We’d be glad to hear opinions or prior art, especially from Idris, Lean and Agda users.
References: