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

`#test` コマンドの活用例:プロパティベーステストを役立てる

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

高階関数やパラメトリック多相な関数のテストであっても、期待される式をそのまま書けてとっても便利

import Plausible

instance : Monad List where
  pure := List.singleton
  bind := List.flatMap

namespace Map
  /- ## map の再帰を使う定義と、使わない定義を比較する -/

  variable {α β : Type}

  /-- do 構文による map の実装 -/
  def List.doMap (f : α → β) (xs : List α) : List β := do
    let x ← xs
    return f x

  -- テスト
  #test
    ∀ {α β : Type} (f : α → β) (xs : List α),
    List.doMap f xs = xs.map f

end Map

universe u

instance instAlternative : Alternative List.{u} where
  failure := @List.nil
  orElse l l' := List.append l (l' ())

namespace Filter

  /- ## filter の再帰を使う定義と、使わない定義を比較する -/

  variable {α : Type}

  /-- do 構文による filter の実装 -/
  def List.doFilter (p : α → Bool) (xs : List α) : List α := do
    let x ← xs
    guard <| p x
    return x

  -- テスト
  #test
    ∀ {α : Type} (p : α → Bool) (xs : List α),
    List.doFilter p xs = List.filter p xs

end Filter

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 with the #test command and Plausible import shown in the issue. Locate the relevant Lean-by-example documentation section, then verify that the property-based testing examples for List.doMap and List.doFilter work as documented. Done means the usage example is included in the appropriate documentation and both displayed tests are valid.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.