Skip to content

Commit

Permalink
chore: add explicit discard
Browse files Browse the repository at this point in the history
  • Loading branch information
leodemoura committed Dec 8, 2020
1 parent 071c0fd commit ae5aa51
Show file tree
Hide file tree
Showing 20 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion src/Init/Control/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ universes u v w

infixr:100 " <&> " => Functor.mapRev

def Functor.discard {f : Type u → Type v} {α : Type u} [Functor f] (x : f α) : f PUnit :=
@[inline] def Functor.discard {f : Type u → Type v} {α : Type u} [Functor f] (x : f α) : f PUnit :=
Functor.mapConst PUnit.unit x

export Functor (discard)
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Compiler/IR/EmitC.lean
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def toStringArgs (ys : Array Arg) : List String :=
def emitSimpleExternalCall (f : String) (ps : Array Param) (ys : Array Arg) : M Unit := do
emit f; emit "("
-- We must remove irrelevant arguments to extern calls.
ys.size.foldM
discard <| ys.size.foldM
(fun i (first : Bool) =>
if ps[i].ty.isIrrelevant then
pure first
Expand Down
3 changes: 1 addition & 2 deletions src/Lean/Elab/App.lean
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,7 @@ private def finalize : M Expr := do
| some expectedType =>
trace[Elab.app.finalize]! "expected type: {expectedType}"
-- Try to propagate expected type. Ignore if types are not definitionally equal, caller must handle it.
isDefEq expectedType eType
pure ()
discard <| isDefEq expectedType eType
synthesizeAppInstMVars
pure e

Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/Binders.lean
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ private def propagateExpectedType (fvar : Expr) (fvarType : Expr) (s : State) :
let expectedType ← whnfForall expectedType
match expectedType with
| Expr.forallE _ d b _ =>
isDefEq fvarType d
discard <| isDefEq fvarType d
let b := b.instantiate1 fvar
pure { s with expectedType? := some b }
| _ => pure { s with expectedType? := none }
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/BuiltinNotation.lean
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private def elabCDot (stx : Syntax) (expectedType? : Option Expr) : TermElabM Ex
let m ← elabTerm m (← mkArrow (mkSort levelOne) (mkSort levelOne))
let ω ← mkFreshExprMVar (mkSort levelOne)
let stWorld ← mkAppM `STWorld #[ω, m]
mkInstMVar stWorld
discard <| mkInstMVar stWorld
mkAppM `StateRefT' #[ω, σ, m]

end Lean.Elab.Term
8 changes: 3 additions & 5 deletions src/Lean/Elab/Match.lean
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ partial def collect : Syntax → M Syntax
/- Recall that
def namedPattern := check... >> tparser! "@" >> termParser -/
let id := stx[0]
processVar id
discard <| processVar id
let pat := stx[2]
let pat ← collect pat
`(namedPattern $id $pat)
Expand Down Expand Up @@ -521,8 +521,7 @@ def getPatternVars (patternStx : Syntax) : TermElabM (Array PatternVar) := do
def getPatternsVars (patterns : Array Syntax) : TermElabM (Array PatternVar) := do
let collect : CollectPatternVars.M Unit := do
for pattern in patterns do
CollectPatternVars.collect (← liftMacroM $ expandMacros pattern)
pure ()
discard <| CollectPatternVars.collect (← liftMacroM $ expandMacros pattern)
let (_, s) ← collect.run {}
pure s.vars

Expand Down Expand Up @@ -552,8 +551,7 @@ private partial def withPatternVars {α} (pVars : Array PatternVar) (k : Array P
decls.forM fun decl => match decl with
| PatternVarDecl.anonymousVar mvarId fvarId => do
let type ← inferType (mkFVar fvarId)
mkFreshExprMVarWithId mvarId type
pure ()
discard <| mkFreshExprMVarWithId mvarId type
| _ => pure ()
k decls
loop 0 #[]
Expand Down
3 changes: 1 addition & 2 deletions src/Lean/Elab/StructInst.lean
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,7 @@ private def propagateExpectedType (type : Expr) (numFields : Nat) (expectedType?
| none => pure ()
| some typeBody =>
unless typeBody.hasLooseBVars do
isDefEq expectedType typeBody
pure ()
discard <| isDefEq expectedType typeBody
private def mkCtorHeader (ctorVal : ConstructorVal) (expectedType? : Option Expr) : TermElabM CtorHeaderResult := do
let lvls ← ctorVal.lparams.mapM fun _ => mkFreshLevelMVar
Expand Down
5 changes: 2 additions & 3 deletions src/Lean/Elab/Structure.lean
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ private def withUsed {α} (scopeVars : Array Expr) (params : Array Expr) (fieldI

private def levelMVarToParamFVar (fvar : Expr) : StateRefT Nat TermElabM Unit := do
let type ← inferType fvar
Term.levelMVarToParam' type
pure ()
discard <| Term.levelMVarToParam' type

private def levelMVarToParamFVars (fvars : Array Expr) : StateRefT Nat TermElabM Unit :=
fvars.forM levelMVarToParamFVar
Expand Down Expand Up @@ -445,7 +444,7 @@ private def addDefaults (lctx : LocalContext) (defaultAuxDecls : Array (Name ×
defaultAuxDecls.forM fun (declName, type, value) => do
/- The identity function is used as "marker". -/
let value ← mkId value
mkAuxDefinition declName type value (zeta := true)
discard <| mkAuxDefinition declName type value (zeta := true)
setReducibleAttribute declName

private def elabStructureView (view : StructView) : TermElabM Unit := do
Expand Down
9 changes: 6 additions & 3 deletions src/Lean/Elab/Tactic/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def getMainTag : TacticM Name := do
def ensureHasNoMVars (e : Expr) : TacticM Unit := do
let e ← instantiateMVars e
let pendingMVars ← getMVars e
Term.logUnassignedUsingErrorInfos pendingMVars
discard <| Term.logUnassignedUsingErrorInfos pendingMVars
if e.hasExprMVar then
throwError! "tactic failed, resulting expression contains metavariables{indentExpr e}"

Expand Down Expand Up @@ -244,8 +244,11 @@ def try? {α} (tactic : TacticM α) : TacticM (Option α) := do

-- TODO: rename?
def «try» {α} (tactic : TacticM α) : TacticM Bool := do
try tactic; pure true
catch _ => pure false
try
discard tactic
pure true
catch _ =>
pure false

/--
Use `parentTag` to tag untagged goals at `newGoals`.
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/Tactic/ElabTerm.lean
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def elabTermWithHoles (stx : Syntax) (expectedType? : Option Expr) (tagSuffix :
else
let naturalMVarIds ← newMVarIds.filterM fun mvarId => return (← getMVarDecl mvarId).kind.isNatural
let syntheticMVarIds ← newMVarIds.filterM fun mvarId => return !(← getMVarDecl mvarId).kind.isNatural
Term.logUnassignedUsingErrorInfos naturalMVarIds
discard <| Term.logUnassignedUsingErrorInfos naturalMVarIds
pure syntheticMVarIds.toList
tagUntaggedGoals (← getMainTag) tagSuffix newMVarIds
pure (val, newMVarIds)
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Elab/Term.lean
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ def resolveName (n : Name) (preresolved : List (Name × List String)) (explicitL
private def mkFreshTypeMVarFor (expectedType? : Option Expr) : TermElabM Expr := do
let typeMVar ← mkFreshTypeMVar MetavarKind.synthetic
match expectedType? with
| some expectedType => isDefEq expectedType typeMVar; pure ()
| some expectedType => discard <| isDefEq expectedType typeMVar
| _ => pure ()
return typeMVar

Expand Down
3 changes: 1 addition & 2 deletions src/Lean/Meta/Check.lean
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ whether terms produced by tactics and `isDefEq` are type correct.
namespace Lean.Meta

private def ensureType (e : Expr) : MetaM Unit := do
getLevel e
pure ()
discard <| getLevel e

def throwLetTypeMismatchMessage {α} (fvarId : FVarId) : MetaM α := do
let lctx ← getLCtx
Expand Down
5 changes: 2 additions & 3 deletions src/Lean/Meta/ExprDefEq.lean
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ private partial def isDefEqArgs (f : Expr) (args₁ args₂ : Array Expr) : Meta
let a₂ := args₂[i]
let info := finfo.paramInfo[i]
if info.instImplicit then
trySynthPending a₁
trySynthPending a₂
pure ()
discard <| trySynthPending a₁
discard <| trySynthPending a₂
withAtLeastTransparency TransparencyMode.default $ Meta.isExprDefEqAux a₁ a₂
else
pure false
Expand Down
6 changes: 2 additions & 4 deletions src/Lean/Meta/Instances.lean
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ builtin_initialize
descr := "type class instance"
add := fun declName args attrKind => do
if args.hasArgs then throwError "invalid attribute 'instance', unexpected argument"
addInstance declName attrKind |>.run {} {}
return ()
discard <| addInstance declName attrKind |>.run {} {}
}

@[export lean_is_instance]
Expand Down Expand Up @@ -123,8 +122,7 @@ builtin_initialize
else
throwErrorAt args "too many arguments at attribute 'defaultInstance', numeral expected"
unless kind == AttributeKind.global do throwError "invalid attribute 'defaultInstance', must be global"
addDefaultInstance declName prio |>.run {} {}
pure ()
discard <| addDefaultInstance declName prio |>.run {} {}
}

def getDefaultInstancesPriorities [Monad m] [MonadEnv m] : m PrioritySet :=
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Meta/Match/Match.lean
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ def mkMatcher (matcherName : Name) (matchType : Expr) (numDiscrs : Nat) (lhss :
let matcher ← mkAuxDefinition matcherName type val (compile := generateMatcherCode (← getOptions))
trace[Meta.Match.debug]! "matcher levels: {matcher.getAppFn.constLevels!}, uElim: {uElimGen}"
let uElimPos? ← getUElimPos? matcher.getAppFn.constLevels! uElimGen
isLevelDefEq uElimGen uElim
discard <| isLevelDefEq uElimGen uElim
addMatcherInfo matcherName { numParams := matcher.getAppNumArgs, numDiscrs := numDiscrs, altNumParams := minors.map Prod.snd, uElimPos? := uElimPos? }
setInlineAttribute matcherName
trace[Meta.Match.debug]! "matcher: {matcher}"
Expand Down
3 changes: 1 addition & 2 deletions src/Lean/Meta/RecursorInfo.lean
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ builtin_initialize recursorAttribute : ParametricAttribute Nat ←
descr := "user defined recursor, numerical parameter specifies position of the major premise",
getParam := fun _ stx => ofExcept $ syntaxToMajorPos stx,
afterSet := fun declName majorPos => do
(mkRecursorInfoCore declName (some majorPos)).run'
pure ()
discard <| mkRecursorInfoCore declName (some majorPos) |>.run'
}

def getMajorPos? (env : Environment) (declName : Name) : Option Nat :=
Expand Down
3 changes: 1 addition & 2 deletions src/Lean/Meta/UnificationHint.lean
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ builtin_initialize
descr := "unification hint"
add := fun declName args kind => do
if args.hasArgs then throwError "invalid attribute 'unificationHint', unexpected argument"
addUnificationHint declName kind |>.run
pure ()
discard <| addUnificationHint declName kind |>.run
}

def tryUnificationHints (t s : Expr) : MetaM Bool := do
Expand Down
4 changes: 2 additions & 2 deletions src/Lean/Parser/Extension.lean
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ def categoryParserFnImpl (catName : Name) : ParserFn := fun ctx s =>
def addToken (env : Environment) (tk : Token) : Except String Environment := do
-- Recall that `ParserExtension.addEntry` is pure, and assumes `addTokenConfig` does not fail.
-- So, we must run it here to handle exception.
addTokenConfig (parserExtension.getState env).tokens tk
pure $ parserExtension.addEntry env <| ParserExtension.Entry.token tk
discard <| addTokenConfig (parserExtension.getState env).tokens tk
return parserExtension.addEntry env <| ParserExtension.Entry.token tk

def addSyntaxNodeKind (env : Environment) (k : SyntaxNodeKind) : Environment :=
parserExtension.addEntry env <| ParserExtension.Entry.kind k
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/ParserCompiler/Attribute.lean
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def registerCombinatorAttribute (name : Name) (descr : String)
let env ← getEnv
match attrParamSyntaxToIdentifier args with
| some parserDeclName => do
getConstInfo parserDeclName
discard <| getConstInfo parserDeclName
setEnv <| ext.addEntry env (parserDeclName, decl)
| none => throwError! "invalid [{name}] argument, expected identifier"
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lean/Server.lean
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def initAndRunServer (i o : FS.Stream) : IO Unit := do
{ capabilities := mkLeanServerCapabilities,
serverInfo? := some { name := "Lean 4 server",
version? := "0.0.1" } : InitializeResult }
readLspNotificationAs "initialized" InitializedParams
discard <| readLspNotificationAs "initialized" InitializedParams
mainLoop ()
let Message.notification "exit" none ← readLspMessage
| throw (userError "Expected an Exit Notification.")
Expand Down

0 comments on commit ae5aa51

Please sign in to comment.