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

IOでタイムアウト付き処理をする

Open
#2,114 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

I have used the following code elsewhere:

import Lean

open Lean

/--
Return type used internally by `withTimeout`. 
-/
inductive TimeoutResult (α : Type) where
  | success (val : α)
  | timeout

/--
Run a computation with a timeout.
-/
def withTimeout (timeout : UInt32) (x : IO α) : IO α := do
  let timeoutTask ← IO.asTask <| IO.sleep timeout >>= fun _ => return TimeoutResult.timeout
  let mainTask ← IO.asTask (prio := .dedicated) <| TimeoutResult.success <$> x
  match ← IO.waitAny [mainTask, timeoutTask] with
  | .ok <| .success a => 
    IO.cancel timeoutTask
    return a
  | .ok <| .timeout => 
    IO.cancel mainTask
    throw <| .userError s!"Operation timed out after {timeout}ms"
  | .error e => 
    IO.cancel mainTask
    IO.cancel timeoutTask
    throw e

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 provides a Lean IO timeout snippet but names no repository file, test, or entry point. First inspect the repository's existing IO examples and issue conventions to determine where this example belongs; done should be a clearly placed, working timeout example with the intended behavior documented.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.