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

`Sort _` の厄介な点、`Sort*` の利点

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

Sort _ のユニバースレベル自動推論について

Lean で Sort _Type _ と書くと、
Lean はその _ の部分、つまりユニバースレベルを自動で決めてくれるの。

でもね、これが思ったより狭く(具体的に)決まっちゃうことがあるの。


例:
import Mathlib.Data.Countable.Defs

example (α : Sort _) (x y : α) (_ : Countable α := inferInstance) : x = y := by
  rfl

このコードでは、
inferInstanceProp.countable を見つけてしまうので、
Sort _αProp に限定されちゃうんだ。

つまり、α : Prop になって、x : αy : α は証明の無差別性(proof irrelevance)により定義的に等しい(=同じもの)と見なされるよ。


もっと古い Lean バージョンの例

Lean 4.8.0 などの昔のバージョンだと、こういう書き方でさらに起こりやすかった:

example {α : Sort _} (x y : α) : x = y := by
  have := α ∧ α -- α を Prop に強制
  rfl

αProp になるように強制しているね。


だからどうすればいいの?

こういう予期しないユニバースレベルの縮小を防ぐために、

  • Sort _Type _ のかわりに、
  • Sort*Type* を使うのがオススメ!

Sort*Type* は、使うたびに新しいユニバースパラメータを強制的に作るので、
ユニバースレベルが不意に狭まるのを防げるよ。

または、

  • ユニバースレベルを明示的に指定してしまうのも手だよ。

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

The issue names no repository file, test, or entry point. First identify where Lean language examples and explanatory pages are maintained, then use the supplied Sort _ and Sort* examples as the proposed scope. The work is done when the explanation and recommendation are placed in the appropriate documentation location and rendered correctly.

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
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.