Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/bar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: verified bar

on:
push:
pull_request:
# Rerun when the spec reviewer approves, or an approval is dismissed.
pull_request_review:
types: [submitted, dismissed]

jobs:
bar:
# pull-requests: read lets the check confirm the spec approval on GitHub.
permissions:
contents: read
pull-requests: read
# Both lines pin the same commit of Proven-Commons/mission: bar v2, as
# proposed in its pull request #2. Move both to a release tag once
# mission publishes one.
uses: Proven-Commons/mission/.github/workflows/verified-bar.yml@6ba85c27abaaf07905c9316aa96dc29f7b5847a2
with:
bar-ref: 6ba85c27abaaf07905c9316aa96dc29f7b5847a2
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.lake/
__pycache__/
104 changes: 104 additions & 0 deletions Main.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/-
Command line front end.

path clean PATH
path join ELEM...

print the path cleaned, or the elements joined and cleaned, then a
line end. `path join` with no elements, or only empty ones, prints an
empty line. Wrong arguments exit with 2.

path serve

answers requests on standard input, one per line, for the
differential tests. A path is sent as the hex digits of its UTF-8
octets, two per octet, and the empty path is written `-`.

clean HEX prints OK and the hex of the path cleaned
(clean)
join HEX... prints OK and the hex of the elements joined,
for zero or more elements (join)

A malformed request prints `error`: an unknown operation, a `clean`
without exactly one path, hex digits that are not in pairs or not
lower case, or octets that are not UTF-8. A request line may end in
LF or CR LF.

Every answer comes from the proved functions in `Path/`. The hex
coding and the request handling here are covered by the tests, not by
a proof.
-/

import Path

def hexValue (c : Char) : Option Nat :=
if '0' ≤ c ∧ c ≤ '9' then some (c.toNat - '0'.toNat)
else if 'a' ≤ c ∧ c ≤ 'f' then some (c.toNat - 'a'.toNat + 10)
else none

/-- Octets from hex digits in pairs. Tail recursive, so that long
inputs do not use the stack. -/
def hexBytes (cs : List Char) (acc : ByteArray) : Option ByteArray :=
match cs with
| [] => some acc
| a :: b :: rest =>
match hexValue a, hexValue b with
| some hi, some lo => hexBytes rest (acc.push (hi * 16 + lo).toUInt8)
| _, _ => none
| [_] => none

/-- The path a field gives: `-` for the empty path, else the hex of
its UTF-8 octets. -/
def fromHex (f : String) : Option String :=
if f = "-" then some ""
else if f.isEmpty then none
else do
let bs ← hexBytes f.toList ByteArray.empty
String.fromUTF8? bs

def hexDigit (n : Nat) : Char :=
if n < 10 then Char.ofNat ('0'.toNat + n) else Char.ofNat ('a'.toNat + n - 10)

/-- The field for a path: `-` when it is empty. -/
def toHex (s : String) : String :=
if s.isEmpty then "-"
else String.ofList (s.toUTF8.toList.flatMap fun b =>
[hexDigit (b.toNat / 16), hexDigit (b.toNat % 16)])

/-- The answer to one request line, or `none` when it is malformed. -/
def request (line : String) : Option String :=
match line.splitOn " " with
| ["clean", h] => do
let p ← fromHex h
pure s!"OK {toHex (Path.clean p)}"
| "join" :: hs => do
let es ← hs.mapM fromHex
pure s!"OK {toHex (Path.join es)}"
| _ => none

/-- The line without its line ending: LF, or CR LF. -/
def chomp (line : String) : String :=
let cs := line.toList
if cs.getLast? = some '\n' then
let cs := cs.dropLast
String.ofList (if cs.getLast? = some '\r' then cs.dropLast else cs)
else line

def serve : IO Unit := do
let stdin ← IO.getStdin
let stdout ← IO.getStdout
repeat
let line ← stdin.getLine
if line.isEmpty then break
stdout.putStrLn ((request (chomp line)).getD "error")
stdout.flush

def usage : String :=
"usage: path clean PATH\n path join ELEM...\n path serve"

def main (args : List String) : IO UInt32 := do
match args with
| ["clean", p] => IO.println (Path.clean p); return 0
| "join" :: es => IO.println (Path.join es); return 0
| ["serve"] => serve; return 0
| _ => IO.eprintln usage; return 2
1 change: 1 addition & 0 deletions Path.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import Path.Basic
89 changes: 89 additions & 0 deletions Path/Basic.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/-
Lexical path cleaning, as Go's `path.Clean` and `path.Join` do it
(go1.24.7, src/path/path.go). The loop below follows Go's line by
line. Go writes into a buffer and keeps its length `w`; here the
buffer is a list, kept reversed so that writing and removing at the
end take one step, and `w` is its length. Go reads the input by
index; here the loop takes the input apart from the front, so it
reads each character once.
-/

namespace Path

/-- The part of Go's `..` case that backtracks. Go first removes one
character, then removes more while the output is longer than
`dotdot` and the character it last removed is not a slash. `out`
is the output, reversed, and `w` its length. -/
def backtrack (dotdot : Nat) : List Char → Nat → List Char × Nat
| c :: out, w + 1 => if w > dotdot ∧ c ≠ '/' then backtrack dotdot out w else (out, w)
| out, w => (out, w)

/-- Go's `..` case. If the output is longer than `dotdot`, it can
backtrack. Otherwise, in a path that is not rooted, it writes `..`,
after a slash when the output is not empty, and that is now where
`..` must stop. Gives the output, its length and the new `dotdot`. -/
def parent (rooted : Bool) (dotdot : Nat) (out : List Char) (w : Nat) :
List Char × Nat × Nat :=
if w > dotdot then
let (out, w) := backtrack dotdot out w
(out, w, dotdot)
else if !rooted then
let (out, w) := if w > 0 then ('/' :: out, w + 1) else (out, w)
('.' :: '.' :: out, w + 2, w + 2)
else (out, w, dotdot)

mutual
/-- Go's loop, at the start of a path element. -/
def loop (rooted : Bool) (dotdot : Nat) (out : List Char) (w : Nat) :
List Char → List Char × Nat
| [] => (out, w)
-- empty path element
| '/' :: rest => loop rooted dotdot out w rest
-- . element
| ['.'] => (out, w)
| '.' :: rest@('/' :: _) => loop rooted dotdot out w rest
-- .. element: remove to last /
| ['.', '.'] =>
let (out, w, _) := parent rooted dotdot out w
(out, w)
| '.' :: '.' :: rest@('/' :: _) =>
let (out, w, dotdot) := parent rooted dotdot out w
loop rooted dotdot out w rest
-- real path element: add a slash if needed, then copy the element
| c :: rest =>
let (out, w) :=
if rooted && w != 1 || !rooted && w != 0 then ('/' :: out, w + 1) else (out, w)
copy rooted dotdot (c :: out) (w + 1) rest

/-- The rest of a real path element, up to the next slash. Go stops
before the slash and its loop then skips it; here `copy` skips it. -/
def copy (rooted : Bool) (dotdot : Nat) (out : List Char) (w : Nat) :
List Char → List Char × Nat
| [] => (out, w)
| '/' :: rest => loop rooted dotdot out w rest
| c :: rest => copy rooted dotdot (c :: out) (w + 1) rest
end

/-- `Clean` on the characters of a path. -/
def cleanChars (path : List Char) : List Char :=
match path with
| [] => ['.']
| c :: rest =>
let rooted := c == '/'
let (out, w) := if rooted then loop true 1 ['/'] 1 rest else loop false 0 [] 0 path
-- Turn empty string into "."
if w == 0 then ['.'] else out.reverse

/-- The path cleaned by lexical processing alone, as Go's `path.Clean`
cleans it. -/
def clean (path : String) : String :=
String.ofList (cleanChars path.toList)

/-- Go's `path.Join`: the elements, the empty ones before the first
other one left out, separated by slashes, and cleaned. If there are
no elements, or all are empty, it gives the empty string. -/
def join (elem : List String) : String :=
if elem.all (·.isEmpty) then ""
else clean ("/".intercalate (elem.dropWhile (·.isEmpty)))

end Path
6 changes: 6 additions & 0 deletions Proofs.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Proofs.Rules
import Proofs.Loop
import Proofs.Clean
import Proofs.Join
import Proofs.Unique
import Proofs.Main
Loading