Back to list
kim-em

parsing

by kim-em

Advent of Code 2025 solutions in Lean 4 with proofs

0🍴 0📅 Dec 16, 2025

SKILL.md


name: parsing description: Use the lean4-parser library for parsing structured input. Invoke when implementing parsers for AoC puzzles or other text processing tasks in Lean. allowed-tools: Read, Edit, Write

Parsing Skill: lean4-parser Library

When parsing structured input (especially for AoC puzzles), use the lean4-parser library instead of manual string splitting.

Installation

Already configured in lakefile.toml:

[[require]]
name = "Parser"
git = "https://github.com/fgdorais/lean4-parser"
rev = "main"

Import

import Parser
open Parser Char

Quick Reference

Parser Types

-- Use SimpleParser for good error messages
abbrev MyParser := SimpleParser Substring Char

Running Parsers

match myParser.run inputString with
| .ok _ result => -- use result
| .error _ e => -- handle error

Common Combinators

CombinatorDescription
ASCII.parseNatParse natural number
ASCII.parseIntParse signed integer
char cMatch exact character
string sMatch exact string
space, whitespaceWhitespace matching
eolEnd of line (LF or CRLF)
endOfInputAssert at end of input
takeMany pZero or more, collect results
dropMany pZero or more, discard
sepBy p sepItems separated by delimiter
first [p1, p2]Try alternatives
test pCheck without consuming (returns Bool)
optional pZero or one

Example: Parse Numbers from Line

def parseLine : SimpleParser Substring Char (List Nat) := do
  sepBy ASCII.parseNat (dropMany1 (char ' '))

Example: Parse Coordinates

def parseCoord : SimpleParser Substring Char (Int × Int) := do
  let _ ← char '('
  let x ← ASCII.parseInt
  let _ ← char ','
  dropMany space
  let y ← ASCII.parseInt
  let _ ← char ')'
  return (x, y)

Example: Parse Key-Value Pairs

def parseKeyValue : SimpleParser Substring Char (String × Int) := do
  let key ← takeMany1 alpha
  let _ ← char ':'
  dropMany space
  let value ← ASCII.parseInt
  return (⟨key⟩, value)

Example: Parse with Alternatives

def parseDirection : SimpleParser Substring Char Int := first [
  string "up" *> pure 1,
  string "down" *> pure (-1),
  string "left" *> pure 0,
  string "right" *> pure 0
]

Example: Looping Until End

def parseAll : SimpleParser Substring Char (List Int) := do
  let mut results := []
  while !(← test endOfInput) do
    let n ← ASCII.parseInt
    results := results ++ [n]
    dropMany (char ' ' <|> char '\n')
  return results

Documentation

Score

Total Score

50/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon