Zulip Chat Archive

Stream: lean4

Topic: How to prevent a function to be treated as an "option" type


Oscar Matemb ⚛️ (Jun 30 2024 at 00:45):

I am unsure how to modify the function's type from an option to the expected type. I am getting a type mismatch error for it

def takeTurnAux (game : MancalaGame) (player : Nat) (cupIdx : Nat) : MancalaGame :=
  let newGame := distributeStones game player cupIdx
  newGame
partial def takeTurn (game : MancalaGame) (player : Nat) : IO MancalaGame := do
  let stdin  IO.getStdin
  let stdout  IO.getStdout
  stdout.putStrLn s!"Player {player + 1}, it's your turn. Select a cup from 1 to 6:\n"
  let input  stdin.getLine -- /
  let trimmedInput := input.trim  -- /
  match parseNat trimmedInput with
  | some n =>
    if n >= 1  n <= 6 then
      let cupIdx := n - 1
      pure (takeTurnAux game player cupIdx) -- /
    else do
      IO.println "Invalid input. Please enter a number between 1 and 6."
      takeTurn game player -- /
  | none => do
    IO.println "Invalid input. Please enter a number between 1 and 6."
    takeTurn game player --/

Kim Morrison (Jun 30 2024 at 01:20):

What is the error? Without a #mwe it is hard to help.


Last updated: May 02 2025 at 03:31 UTC