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
19 changes: 19 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,25 @@ jobs:
target/debug/rulean frobnicate || code=$?
test "$code" = 64

# Every test of the Lean Kernel Arena must parse (milestone M1). The job downloads the Arena, so it depends on arena.lean-lang.org and is not a required check.
arena:
name: arena parse
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- run: rustup show active-toolchain || rustup toolchain install
- uses: Swatinem/rust-cache@d1527c8f67ef9af357290475d4af4b0fab8f12e0 # v2.9.3
with:
save-if: false
- run: cargo build --locked --release --bin rulean
- run: scripts/arena-fetch.sh _tmp/arena
- run: scripts/arena-parse.sh _tmp/arena/tests
- name: the parser tests on the Arena
run: RULEAN_ARENA="$PWD/_tmp/arena/tests" cargo test --locked --release -p rulean-format --test arena

# The repository rules of the spec: the dependency rule (03.3), the no-C policy (13.3) and the Aeneas subset (10.6). `cargo xtask all` also scans the vendored source of each dependency.
policy:
name: policy
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ All changes that a user can see are in this file. The format follows Keep a Chan
- `docs/bench/baseline.md` records the first baseline run. The verdicts match the Arena on all 225 tests. The cachegrind counts are not within 2% of the Arena counts, so the 2% limit applies only to `perf`.
- `rulean-core` has the environment and the type checker of engine R, as a transcription of `type_checker.cpp`, `inductive_reduce_rec` and `quot_reduce_rec` of the official kernel v4.35.0. The checker infers types in the infer-only mode and in the check mode, reduces terms to weak head normal form in the official order (beta, zeta, projections, recursors, quotients, the `Nat` extension and delta), and decides definitional equality with lazy delta reduction, eta, structure eta, proof irrelevance, unit-like types and the `Nat` and `String` literal extensions. An error is a sticky fault, and a depth bound stops deep recursion. The `Nat` operations check the size bound of 128 MiB.
- `rulean-core` has the declaration checks of spec 05.9 and 06.5, as a transcription of `environment.cpp` and `quot.cpp` of the official kernel v4.35.0: axioms, definitions, theorems, opaque constants, mutual blocks of unsafe or partial definitions and the quotient constants. An unsafe definition may refer to itself, and a safe declaration may not refer to an unsafe or partial constant. The first quotient declaration adds the four reference constants. Each exported quotient declaration must be equal to its reference constant. A rejected declaration does not change the environment.
- `rulean check --parse-only` parses an export into the term store and prints the counts, the store memory and the time. Without `--parse-only`, `check` still declines until M2. A parse reject exits with code 4, and a decline exits with code 2.
- `scripts/arena-parse.sh` runs `rulean check --parse-only` on every Arena test, and the new `arena parse` CI job runs it on the downloaded Arena.

### Changed

Expand Down
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/rulean-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ layer = "hardened"
name = "rulean"
path = "src/main.rs"

[dependencies]
rulean-format = { path = "../rulean-format" }
rulean-store = { path = "../rulean-store" }

[lints]
workspace = true
132 changes: 132 additions & 0 deletions crates/rulean-cli/src/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//! The `check` command (spec 09.2).
//!
//! Milestone M1 has only `--parse-only`. It reads the export into the term store and reports what it read, but it checks no declaration. Without `--parse-only`, the command declines until milestone M2.

use std::io::Read;
use std::time::Instant;

use rulean_format::ErrorKind;
use rulean_store::Store;

use crate::exit::Code;

/// The options of `check`.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Options {
pub input: Option<String>,
pub parse_only: bool,
}

/// Reads the options. The result is an error text for a usage error.
pub fn options(args: &[String]) -> Result<Options, String> {
let mut o = Options::default();
for a in args {
match a.as_str() {
"--parse-only" => o.parse_only = true,
s if s.starts_with("--") => return Err(format!("unknown option `{s}`")),
s => {
if o.input.is_some() {
return Err(format!("a second input `{s}`"));
}
o.input = Some(s.to_owned());
}
}
}
if o.input.is_none() {
return Err("no input".to_owned());
}
Ok(o)
}

/// Runs `check` with the arguments after the command name.
pub fn run(args: &[String]) -> Code {
let o = match options(args) {
Ok(o) => o,
Err(e) => {
eprintln!("rulean check: {e}");
return Code::Usage;
}
};
if !o.parse_only {
eprintln!(
"rulean: the `check` command checks nothing yet. Milestone M2 adds the type checker. Use --parse-only to parse the input."
);
return Code::Decline;
}
let path = o.input.unwrap_or_default();
let input = match read_input(&path) {
Ok(b) => b,
Err(e) => {
eprintln!("rulean: cannot read {path}: {e}");
return Code::Input;
}
};
let start = Instant::now();
let mut store = Store::new();
match rulean_format::parse(&mut store, &input) {
Ok(decls) => {
let ms = start.elapsed().as_millis();
let mem = store.memory();
println!(
"parsed {path}: {} declarations, {} expression nodes, {} names, {} levels, {} bytes of store, {ms} ms",
decls.len(),
store.expr_total(),
store.name_count(),
store.level_count(),
mem.total()
);
Code::Accept
}
Err(e) => {
eprintln!("rulean: {path}: line {}: {}", e.line, e.kind.text());
code_of(e.kind)
}
}
}

/// The exit code of a parse error (spec 09.3).
pub fn code_of(kind: ErrorKind) -> Code {
match kind {
ErrorKind::StoreFull => Code::Resource,
k if k.is_decline() => Code::Decline,
_ => Code::Input,
}
}

fn read_input(path: &str) -> std::io::Result<Vec<u8>> {
if path == "-" {
let mut b = Vec::new();
std::io::stdin().lock().read_to_end(&mut b)?;
Ok(b)
} else {
std::fs::read(path)
}
}

#[cfg(test)]
mod tests {
use super::*;

fn strs(a: &[&str]) -> Vec<String> {
a.iter().map(|s| (*s).to_owned()).collect()
}

#[test]
fn option_rules() {
let o = options(&strs(&["x.ndjson", "--parse-only"])).expect("valid");
assert_eq!(o.input.as_deref(), Some("x.ndjson"));
assert!(o.parse_only);
assert!(options(&strs(&[])).is_err());
assert!(options(&strs(&["a", "b"])).is_err());
assert!(options(&strs(&["a", "--fast"])).is_err());
}

#[test]
fn error_codes() {
assert_eq!(code_of(ErrorKind::BadJson), Code::Input);
assert_eq!(code_of(ErrorKind::UndefinedIndex), Code::Input);
assert_eq!(code_of(ErrorKind::UnknownVersion), Code::Decline);
assert_eq!(code_of(ErrorKind::IndexLimit), Code::Decline);
assert_eq!(code_of(ErrorKind::StoreFull), Code::Resource);
}
}
2 changes: 0 additions & 2 deletions crates/rulean-cli/src/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ pub enum Code {
/// rulean does not support this input.
Decline,
/// A resource limit was reached and no declaration was rejected.
#[allow(dead_code, reason = "used from milestone M1")]
Resource,
/// The input is malformed.
#[allow(dead_code, reason = "used from milestone M1")]
Input,
/// Every declaration was accepted, but a theorem uses an axiom outside the allowed list.
#[allow(dead_code, reason = "used from milestone M2")]
Expand Down
8 changes: 6 additions & 2 deletions crates/rulean-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::process::ExitCode;

mod check;
mod exit;

use exit::Code;
Expand All @@ -13,6 +14,7 @@ usage: rulean <command> [options]

commands:
check <INPUT> check an export (.ndjson or \"-\" for standard input)
--parse-only: parse the export and check nothing
version print the version and the build facts
help print this text

Expand All @@ -37,7 +39,7 @@ fn run(args: &[String]) -> Code {
println!("{}", version_text());
Code::Accept
}
"check" => not_written("check", "M2"),
"check" => check::run(&args[1..]),
"olean" => not_written("olean", "M4"),
"pack" => not_written("pack", "M4"),
"diff" => not_written("diff", "M3"),
Expand Down Expand Up @@ -88,7 +90,9 @@ mod tests {

#[test]
fn commands_that_are_not_written_decline() {
for command in ["check", "olean", "pack", "diff", "explain"] {
assert_eq!(run_strs(&["check", "x.ndjson"]), Code::Decline);
assert_eq!(run_strs(&["check"]), Code::Usage);
for command in ["olean", "pack", "diff", "explain"] {
assert_eq!(run_strs(&[command]), Code::Decline, "{command}");
}
}
Expand Down
32 changes: 32 additions & 0 deletions scripts/arena-parse.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
# Runs `rulean check --parse-only` on every test of the Lean Kernel Arena. All tests must parse: the bad tests are bad for the type checker, not for the parser.
#
# Usage: scripts/arena-parse.sh TESTS [RULEAN]
#
# TESTS is the tests directory of scripts/arena-fetch.sh. RULEAN is the rulean binary, and the default is target/release/rulean. The script prints one line for each test that does not parse, and exits with 1 if there is one.

set -euo pipefail

if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "usage: $0 TESTS [RULEAN]" >&2
exit 64
fi
tests="$1"
rulean="${2:-target/release/rulean}"

total=0
failed=0
while IFS= read -r -d '' f; do
total=$((total + 1))
if ! out=$("$rulean" check --parse-only "$f" 2>&1); then
failed=$((failed + 1))
echo "FAIL $out"
fi
done < <(find "$tests" -name '*.ndjson' -print0 | sort -z)

if [ "$total" -eq 0 ]; then
echo "$0: no .ndjson files in $tests" >&2
exit 1
fi
echo "$((total - failed)) of $total Arena tests parse"
test "$failed" -eq 0
Loading