8000 Effect polymorphism: a foul block escapes through a pure higher-order function · Issue #142 · kexhq/kex · GitHub
[go: up one dir, main page]

Skip to content

Effect polymorphism: a foul block escapes through a pure higher-order function #142

Open

Description

@akoskovacs

Follow-up to #130. That change made foul a per-function marker and enforced it per function. This is the one purity hole it does not close.

The hole

A block literal is attributed to whoever writes it, so a pure function cannot contain an effect. This is correctly rejected today:

foul greet(msg: String) -> Void = IO.printLine(msg)
let apply(f: Block<Void>) -> Void = f()

let indirect() -> Void = apply { greet("hi") }
# error: Cannot call foul function 'greet' from pure context

But Block<Void> is the same type whether the block is pure or foul, so the effect disappears the moment a block becomes a value. A foul function manufactures one, a pure function runs it:

foul mkPrinter(msg: String) -> Block<Void> = { IO.printLine(msg) }

let runTwice(f: Block<Void>) -> Void do
  f()
  f()
end

main do
  runTwice(mkPrinter("hi"))    # prints twice. No error. `runTwice` is `let`.
end

runTwice is declared pure and performs IO twice. Anything that trusts the marker — guards, and any future memoization or reordering of pure calls — is trusting something false.

This is pre-existing, not introduced by #130; module foulness never addressed it either.

Why it cannot just be "mark the combinators foul"

Enumerable.each, map, filter and List.map are all let and all call the block they are given. Marking them foul would poison every pure pipeline — xs.map { |x| x * 2 } would stop being callable from pure code. The hole is load-bearing, which is why the two parts below have to land together rather than in sequence.

Sketch

1. The effect belongs to the block's type

Block<Void>         # pure block
foul Block<Void>    # foul block

mkPrinter : String -> foul Block<Void>, and runTwice(f: Block<Void>) then rejects it at the call site. This plugs the hole on its own, but leaves every combinator needing a pure copy and a foul copy — hence part 2.

2. Effect-transparency, inferred

The rule that avoids threading an <E> effect variable through every signature:

A function that calls one of its Block parameters is effect-transparent. At a call site its foulness is the max of its own declared foulness and the foulness of the blocks passed to it.

Nothing is annotated. each, map, filter, runTwice all stay let, and each call site is judged on what it is handed:

call block call is inside a pure function
xs.map { |x| x * 2 } pure pure legal
xs.each do |x| IO.printLine(x) end foul foul rejected
runTwice(mkPrinter("hi")) foul foul rejected

Row 2 is already an error today, but only because the block body is walked as part of the enclosing function — the right answer for the wrong reason. This derives it from the rule that explains it, and fixes row 3, which is the actual leak.

3. Where the qualifier is still written

Inference cannot see through a stored block, so part 1's qualifier earns its keep in type positions:

record Handler do
  run : foul Block<Void>
end

Implementation notes

  • computeTransitiveEffects already propagates foulness through the call graph. This extends the same propagation from callees to arguments; enrichEffectsFromResolvedCalls is the natural home, being already a post-typecheck pass over resolved calls.
  • Effect-transparency becomes part of a function's public interface, so .kexi must record which parameters are transparent — a per-export bitmask. Additive: KexiExport already carries isFoul and paramNames.

Open questions

  • Currying. ~each partially applied — where does the effect attach when the block has not been supplied yet?
  • Trait dictionaries. A trait method taking a block, dispatched through a dictionary, has no syntactic block argument at the call site. Probably the hardest part of the design.
  • Inference vs. declaration. Inferring transparency means a function's interface can change when its body changes (start calling a block parameter, and callers in pure code break). An explicit marker would be more honest about the interface at the cost of ceremony.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0