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

CoeDep 使用例:正の自然数から自然数への型強制

Open
#1,457 1 comment 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

match 式でありえないケースを自動的に排除できる例でもある。

/-- 正の自然数 -/
abbrev PosNat := { x : Nat // x > 0}

-- def PosNat.ofNat (n : Nat) (h : n > 0 := by omega) : PosNat := ⟨n, h⟩

def PosNat.ofSucc (n : Nat) : PosNat := ⟨n + 1, by omega⟩

instance (n : Nat) : CoeDep Nat (.succ n) PosNat where
  coe := PosNat.ofSucc n

/-- 正の自然数に対して定義された等比数列 -/
def geomseq (r : Nat) (x : PosNat) : Nat :=
  match x with
  | ⟨1, _⟩ => 1
  | ⟨n + 2, h⟩ => r * geomseq r ⟨n + 1, by omega⟩

-- 数値リテラルをそのまま入れると OfNat がないのでエラーになるが…
#check_failure geomseq 3 4

-- Nat → PosNat という依存型強制は定義できている!
#guard
  let four : Nat := 4
  geomseq 3 four = 27

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 by locating the repository’s existing Lean example entry point and checking how similar examples are organized. Use the issue’s PosNat, CoeDep, geomseq, #check_failure, and #guard example as the reference, and consider the work done when the example is incorporated in the project’s expected format and its checks pass.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.