lean-ja / lean-ja/lean-by-example

factorial 階乗の記法が衝突しないようにする例

Open
#1,454 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

コード例 メタプログラミング
Dominant language
Lean
Stars
188
Forks
15
Avg merge
9h 8m
Merged PRs (30d)
6

Description

Zulip: precedence of Nat.factorial

import Lean
import Mathlib.Data.Nat.Factorial.Basic

namespace Nat.Factorial!
open Lean Elab Term Meta

@[scoped term_elab ident]
def elabIdentFactorial : TermElab
  | stx@`($name:ident), expectedType? => do
    let view := extractMacroScopes name.getId
    let .str parent s := view.name | throwUnsupportedSyntax
    unless s.endsWith "!" do throwUnsupportedSyntax
    let normal ← Term.observing <| elabIdent stx expectedType?
    if normal matches .error .. then
      let name' := MacroScopesView.review { view with name := .str parent (s.dropRight 1) }
      let factorial ← Term.observing <| elabTerm (← `(Nat.factorial $(mkIdent name'))) expectedType?
      if factorial matches .ok .. then
        return ← Term.applyResult factorial
    Term.applyResult normal
  | _, _ => throwUnsupportedSyntax

end Factorial!

open scoped Nat.Factorial!

variable (x : Nat)
#check x ! -- x ! : ℕ
#check x!  -- x ! : ℕ
#check List.get! [1,2,3] 0 -- [1, 2, 3].get! 0 : ℕ

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.

Research direction

Start from the shown imports, especially Mathlib.Data.Nat.Factorial.Basic, and inspect how examples in the book are organized. Check the Lean snippet with the open scoped Nat.Factorial! declaration and its three #check examples; done means the example demonstrates factorial notation without conflicting with List.get!.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.