leanprover / leanprover/lean4

`private` and `protected` interact strangely

Open
#1,861 5 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

P-low
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

import Lean
open Lean
macro "test" x:ident : command => do
  let [(name, _)] ← Macro.resolveGlobalName x.getId | Macro.throwUnsupported
  `(protected theorem $(mkIdent (name.mkStr "ext")):ident : True := trivial)

inductive A : Type
test A
#print A.ext -- ok

private inductive B : Type
test B 
-- invalid scope
-- protected declarations must be in a namespace
-- invalid 'end', insufficient scopes
#print B.ext -- fail

namespace Ns
private inductive C : Type
test C 
-- invalid scope
-- invalid 'end', insufficient scopes
#print C.ext -- fail, but...
#print Ns.ext -- ok

This is a simplified version of an issue with the ext attribute from mathlib, which attempts to create a protected declaration in the same namespace as the input type name. (It would also be nice to implement this as a macro; I think I know how to work around this as an elab.) When the type is private, this essentially amounts to elaborating a command like:

protected theorem _private.Test.0.B.ext : True := trivial

and the declaration parser gets very confused by this identifier. Ideally, this would just work and create a private declaration which is also protected, in the sense that you can refer to it by the name B.ext but not ext. I believe the knock-on error about invalid scope is because the declaration elaborator expanded to a namespace 0 which isn't valid.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.