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

outParam が予想外の挙動をする例

Open
#483 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


universe u v w

namespace Ex

/-- `outParam` なしで定義したダメな例 -/
class BadHMul (α : Type u) (β : Type v) (γ : Type w) where
  hMul : α → β → γ

instance : BadHMul Nat Nat Nat where
  hMul := Nat.mul

-- 返り値の型がわからないのでエラーになる
#check_failure BadHMul.hMul 1 2

-- 返り値の型を指定すればエラーにならない
#check (BadHMul.hMul 1 2 : Nat)

class HMul (α : Type u) (β : Type v) (γ : outParam (Type w)) where
  hMul : α → β → γ

instance : HMul Nat Nat (List Nat) where
  hMul x y := []

#check (HMul.hMul 1 4 : List Nat)

instance : HMul Nat Nat Nat where
  hMul := Nat.mul

-- 返り値の型を自動で推論してくれて,エラーにならない
#eval HMul.hMul 4 3 -- 12

-- なぜか List Nat に変換する方だと解釈してくれない
-- outParam を付けたことで, Nat → Nat → Nat が優先される???
-- 怪奇現象
#check_failure (HMul.hMul 1 4 : List Nat) -- 4

instance : HMul Nat String Unit where
  hMul x y := ()

instance : HMul Nat String (List Nat) where
  hMul x y := []

-- これはいける
-- そして Unit が優先される(インスタンスの宣言順序は無関係)
#eval HMul.hMul 3 "hello"

instance : HMul Nat Nat Unit where
  hMul x y := ()

-- これは掛け算が勝つ
#guard HMul.hMul 2 3 = 6

end Ex

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 running the supplied Lean example and reproducing each reported success or failure, especially the outParam cases. Trace how instance and result-type inference selects HMul instances, then document an explanation of the observed behavior and clarify what correction or example change is needed.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.