blob: a4475e8308bdcdbe696a1fd6b727b38c0c6bd11f [file]
/-
Copyright (c) 2021 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Leonardo de Moura
-/
module
prelude
public import Lean.Parser.Term
meta import Lean.Parser.Term
meta import Lean.Parser.Do
import Init.Syntax
public section
namespace Lean.Elab.Term
/--
Recall that
```
def typeSpec := leading_parser " : " >> termParser
def optType : Parser := optional typeSpec
```
-/
def expandOptType (ref : Syntax) (optType : Syntax) : Syntax :=
if optType.isNone then
mkHole ref
else
optType[0][1]
open Lean.Parser.Term
/-- Helper function for `expandEqnsIntoMatch` -/
def getMatchAltsNumPatterns (matchAlts : Syntax) : Nat :=
let alt0 := matchAlts[0][0]
let pats := alt0[1][0].getSepArgs
pats.size
open TSyntax.Compat in
/--
Expand a match alternative such as `| 0 | 1 => rhs` to an array containing `| 0 => rhs` and `| 1 => rhs`.
-/
def expandMatchAlt (stx : TSyntax ``matchAlt) : Array (TSyntax ``matchAlt) :=
-- Not using syntax quotations here to keep source location
-- of the pattern sequence (`$term,*`) intact
let patss := stx.raw[1].getSepArgs
if patss.size ≤ 1 then
#[stx]
else
patss.map fun pats => stx.raw.setArg 1 (mkNullNode #[pats])
def shouldExpandMatchAlt : TSyntax ``matchAlt → Bool
| `(matchAltExpr| | $[$patss,*]|* => $_) => patss.size > 1
| _ => false
def expandMatchAlts? (stx : Syntax) : MacroM (Option Syntax) := do
match stx with
| `(match $[$gen]? $[$motive]? $discrs,* with $alts:matchAlt*) =>
expand alts >>= fun alts? => alts?.mapM fun alts =>
`(match $[$gen]? $[$motive]? $discrs,* with $alts:matchAlt*)
| `(doElem| match $[$dep?]? $[$gen]? $[$motive]? $discrs,* with $alts:matchAlt*) =>
expand alts >>= fun alts? => alts?.mapM fun alts =>
`(doElem| match $[$dep?]? $[$gen]? $[$motive]? $discrs,* with $alts:matchAlt*)
| _ => return none
where
expand (alts : Array (TSyntax ``matchAlt)) : MacroM (Option (Array (TSyntax ``matchAlt))) := do
if alts.any shouldExpandMatchAlt then
let alts ← alts.foldlM (init := #[]) fun alts alt => return alts ++ expandMatchAlt alt
return some alts
else
return none
open TSyntax.Compat in
def clearInMatchAlt (stx : TSyntax ``matchAlt) (vars : Array Ident) : TSyntax ``matchAlt :=
stx.1.modifyArg 3 fun rhs => Unhygienic.run do
let mut rhs := rhs
for v in vars do
rhs ← `(clear% $v; $rhs)
return rhs
def clearInMatch (stx : Syntax) (vars : Array Ident) : MacroM Syntax := do
if vars.isEmpty then return stx
match stx with
| `(match $[$gen]? $[$motive]? $discrs,* with $alts:matchAlt*) =>
let alts := alts.map (clearInMatchAlt · vars)
`(match $[$gen]? $[$motive]? $discrs,* with $alts:matchAlt*)
| _ => return stx
end Lean.Elab.Term