feat: allow optional type in example

This commit is contained in:
Mario Carneiro 2022-09-12 20:14:16 -04:00 committed by Leonardo de Moura
parent b4ed2f2bbb
commit 3bb3efdedc
4 changed files with 7 additions and 4 deletions

View file

@ -137,11 +137,11 @@ def mkDefViewOfOpaque (modifiers : Modifiers) (stx : Syntax) : CommandElabM DefV
def mkDefViewOfExample (modifiers : Modifiers) (stx : Syntax) : DefView :=
-- leading_parser "example " >> declSig >> declVal
let (binders, type) := expandDeclSig stx[1]
let (binders, type) := expandOptDeclSig stx[1]
let id := mkIdentFrom stx `_example
let declId := mkNode ``Parser.Command.declId #[id, mkNullNode]
{ ref := stx, kind := DefKind.example, modifiers := modifiers,
declId := declId, binders := binders, type? := some type, value := stx[2] }
declId := declId, binders := binders, type? := type, value := stx[2] }
def isDefLike (stx : Syntax) : Bool :=
let declKind := stx.getKind

View file

@ -76,7 +76,7 @@ def «opaque» := leading_parser "opaque " >> declId >> ppIndent declSig
def «instance» := leading_parser Term.attrKind >> "instance" >> optNamedPrio >> optional (ppSpace >> declId) >> ppIndent declSig >> declVal >> terminationSuffix
def «axiom» := leading_parser "axiom " >> declId >> ppIndent declSig
/- As `declSig` starts with a space, "example" does not need a trailing space. -/
def «example» := leading_parser "example" >> ppIndent declSig >> declVal
def «example» := leading_parser "example" >> ppIndent optDeclSig >> declVal
def ctor := leading_parser "\n| " >> ppIndent (declModifiers true >> rawIdent >> optDeclSig)
def derivingClasses := sepBy1 (group (ident >> optional (" with " >> Term.structInst))) ", "
def optDeriving := leading_parser optional (ppLine >> atomic ("deriving " >> notSymbol "instance") >> derivingClasses)

View file

@ -2,5 +2,6 @@ structure Foo where
foo : Nat
def baz {x : _} := Foo.mk x -- works fine
example {x : _} := Foo.mk x
def qux {x : _} : Foo := Foo.mk x
example {x : _} : Foo := Foo.mk x

View file

@ -1,2 +1,4 @@
331.lean:6:13-6:14: error: failed to infer binder type
when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed
331.lean:7:13-7:14: error: failed to infer binder type
when the resulting type of a declaration is explicitly provided, all holes (e.g., `_`) in the header are resolved before the declaration body is processed