Skip to content

Record/tuple patterns and new pattern match compiler and coverage checker #430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
620 changes: 330 additions & 290 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ inherits = "release"
debug = true

[workspace]
resolver = "2"
members = [
'./fathom',
]
6 changes: 3 additions & 3 deletions doc/roadmap.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
- [ ] refinement types
- [x] match expressions
- [x] single-layer pattern matching
- [ ] multi-layer pattern matching
- [x] multi-layer pattern matching
- [ ] dependent pattern matching
- [ ] patterns
- [x] patterns
- [x] wildcard patterns
- [x] named patterns
- [x] annotated patterns
- [x] numeric literal patterns
- [ ] record literal patterns
- [x] record literal patterns
- [ ] invertible format descriptions

## Implementation
Expand Down
42 changes: 23 additions & 19 deletions fathom/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[package]
name = "fathom"
version = "0.1.0"
authors = ["YesLogic Pty. Ltd. <info@yeslogic.com>"]
repository = "https://github.com/yeslogic/fathom"
edition = "2021"
name = "fathom"
version = "0.1.0"
authors = ["YesLogic Pty. Ltd. <info@yeslogic.com>"]
repository = "https://github.com/yeslogic/fathom"
edition = "2021"
rust-version = "1.67.0"
publish = false
publish = false

description = "A language for declaratively specifying binary data formats"
readme = "../README.md"
license = "Apache-2.0"
readme = "../README.md"
license = "Apache-2.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[test]]
name = "source_tests"
name = "source_tests"
harness = false

[dependencies]
Expand All @@ -23,25 +23,29 @@ clap = { version = "4.0", features = ["derive"] }
codespan-reporting = "0.11.1"
fxhash = "0.2"
itertools = "0.10"
lalrpop-util = "0.19.5"
lasso = { version = "0.6.0", features = ["multi-threaded", "ahasher", "inline-more"] }
lalrpop-util = "0.20.0"
levenshtein = "1.0.5"
logos = "0.12"
lasso = { version = "0.6.0", features = [
"multi-threaded",
"ahasher",
"inline-more",
] }
once_cell = { version = "1.17.0", features = ["parking_lot"] }
pretty = "0.11.2"
rpds = "0.12.0"
scoped-arena = "0.4.1"
termsize = "0.1.6"

[build-dependencies]
lalrpop = { git = "https://github.com/kmeakin/lalrpop", branch = "raw-identifiers" }
lalrpop = "0.20.0"

[dev-dependencies]
diff = "0.1.12"
globwalk = "0.8"
itertools = "0.10.1"
diff = "0.1.12"
globwalk = "0.8"
itertools = "0.10.1"
libtest-mimic = "0.6.0"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5"
trycmd = "0.14.10"
walkdir = "2.3.2"
serde = { version = "1.0", features = ["derive"] }
toml = "0.5"
trycmd = "0.14.10"
walkdir = "2.3.2"
172 changes: 171 additions & 1 deletion fathom/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use std::fmt;

use crate::env::{Index, Level};
use scoped_arena::Scope;

use crate::env::{EnvLen, Index, Level};
use crate::source::Span;
use crate::symbol::Symbol;

Expand Down Expand Up @@ -206,6 +208,10 @@ pub enum Term<'arena> {
}

impl<'arena> Term<'arena> {
pub fn error(span: impl Into<Span>) -> Self {
Self::Prim(span.into(), Prim::ReportedError)
}

/// Get the source span of the term.
pub fn span(&self) -> Span {
match self {
Expand Down Expand Up @@ -280,6 +286,155 @@ impl<'arena> Term<'arena> {
pub fn is_error(&self) -> bool {
matches!(self, Term::Prim(_, Prim::ReportedError))
}

// TODO: Add a new `Weaken` variant to `core::Term` instead of eagerly
// traversing the term? See [Andras Kovacs’ staged language](https://github.com/AndrasKovacs/staged/blob/9e381eb162f44912d70fb843c4ca6567b0d1683a/demo/Syntax.hs#L52) for an example
pub fn shift(&self, scope: &'arena Scope<'arena>, amount: EnvLen) -> Term<'arena> {
self.shift_inner(scope, Index::last(), amount)
}

/// Increment all `LocalVar`s greater than or equal to `min` by `amount`
fn shift_inner(
&self,
scope: &'arena Scope<'arena>,
mut min: Index,
amount: EnvLen,
) -> Term<'arena> {
// Skip traversing and rebuilding the term if it would make no change. Increases
// sharing.
if amount == EnvLen::new() {
return self.clone();
}

match self {
Term::LocalVar(span, var) if *var >= min => Term::LocalVar(*span, *var + amount),
Term::LocalVar(..)
| Term::ItemVar(..)
| Term::MetaVar(..)
| Term::InsertedMeta(..)
| Term::Prim(..)
| Term::ConstLit(..)
| Term::Universe(..) => self.clone(),
Term::Ann(span, expr, r#type) => Term::Ann(
*span,
scope.to_scope(expr.shift_inner(scope, min, amount)),
scope.to_scope(r#type.shift_inner(scope, min, amount)),
),
Term::Let(span, name, def_type, def_expr, body) => Term::Let(
*span,
*name,
scope.to_scope(def_type.shift_inner(scope, min, amount)),
scope.to_scope(def_expr.shift_inner(scope, min, amount)),
scope.to_scope(body.shift_inner(scope, min.prev(), amount)),
),
Term::FunType(span, plicity, name, input, output) => Term::FunType(
*span,
*plicity,
*name,
scope.to_scope(input.shift_inner(scope, min, amount)),
scope.to_scope(output.shift_inner(scope, min.prev(), amount)),
),
Term::FunLit(span, plicity, name, body) => Term::FunLit(
*span,
*plicity,
*name,
scope.to_scope(body.shift_inner(scope, min.prev(), amount)),
),
Term::FunApp(span, plicity, fun, arg) => Term::FunApp(
*span,
*plicity,
scope.to_scope(fun.shift_inner(scope, min, amount)),
scope.to_scope(arg.shift_inner(scope, min, amount)),
),
Term::RecordType(span, labels, types) => Term::RecordType(
*span,
labels,
scope.to_scope_from_iter(types.iter().map(|r#type| {
let ret = r#type.shift_inner(scope, min, amount);
min = min.prev();
ret
})),
),
Term::RecordLit(span, labels, exprs) => Term::RecordLit(
*span,
labels,
scope.to_scope_from_iter(
exprs
.iter()
.map(|expr| expr.shift_inner(scope, min, amount)),
),
),
Term::RecordProj(span, head, label) => Term::RecordProj(
*span,
scope.to_scope(head.shift_inner(scope, min, amount)),
*label,
),
Term::ArrayLit(span, terms) => Term::ArrayLit(
*span,
scope.to_scope_from_iter(
terms
.iter()
.map(|term| term.shift_inner(scope, min, amount)),
),
),
Term::FormatRecord(span, labels, terms) => Term::FormatRecord(
*span,
labels,
scope.to_scope_from_iter(terms.iter().map(|term| {
let ret = term.shift_inner(scope, min, amount);
min = min.prev();
ret
})),
),
Term::FormatCond(span, name, format, pred) => Term::FormatCond(
*span,
*name,
scope.to_scope(format.shift_inner(scope, min, amount)),
scope.to_scope(pred.shift_inner(scope, min.prev(), amount)),
),
Term::FormatOverlap(span, labels, terms) => Term::FormatOverlap(
*span,
labels,
scope.to_scope_from_iter(terms.iter().map(|term| {
let ret = term.shift_inner(scope, min, amount);
min = min.prev();
ret
})),
),
Term::ConstMatch(span, scrut, branches, default) => Term::ConstMatch(
*span,
scope.to_scope(scrut.shift_inner(scope, min, amount)),
scope.to_scope_from_iter(
branches
.iter()
.map(|(r#const, term)| (*r#const, term.shift_inner(scope, min, amount))),
),
default.map(|(name, term)| {
(
name,
scope.to_scope(term.shift_inner(scope, min.prev(), amount)) as &_,
)
}),
),
}
}

/// Returns `true` if `self` can be evaluated in a single step.
/// Used as a heuristic to prevent increase in runtime when expanding
/// pattern matches
pub fn is_atomic(&self) -> bool {
match self {
Term::ItemVar(_, _)
| Term::LocalVar(_, _)
| Term::MetaVar(_, _)
| Term::InsertedMeta(_, _, _)
| Term::Universe(_)
| Term::Prim(_, _)
| Term::ConstLit(_, _) => true,
Term::RecordProj(_, head, _) => head.is_atomic(),
_ => false,
}
}
}

macro_rules! def_prims {
Expand Down Expand Up @@ -600,6 +755,21 @@ pub enum Const {
Ref(usize),
}

impl Const {
/// Return the number of inhabitants of `self`.
/// `None` represents infinity
pub fn num_inhabitants(&self) -> Option<u128> {
match self {
Const::Bool(_) => Some(2),
Const::U8(_, _) | Const::S8(_) => Some(1 << 8),
Const::U16(_, _) | Const::S16(_) => Some(1 << 16),
Const::U32(_, _) | Const::S32(_) => Some(1 << 32),
Const::U64(_, _) | Const::S64(_) => Some(1 << 64),
Const::F32(_) | Const::F64(_) | Const::Pos(_) | Const::Ref(_) => None,
}
}
}

impl PartialEq for Const {
fn eq(&self, other: &Const) -> bool {
match (*self, *other) {
Expand Down
9 changes: 9 additions & 0 deletions fathom/src/core/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum Value<'arena> {
}

impl<'arena> Value<'arena> {
pub const ERROR: Self = Self::Stuck(Head::Prim(Prim::ReportedError), Vec::new());

pub fn prim(prim: Prim, params: impl IntoIterator<Item = ArcValue<'arena>>) -> Value<'arena> {
let params = params
.into_iter()
Expand All @@ -77,6 +79,13 @@ impl<'arena> Value<'arena> {
}
}

pub fn match_record_type(&self) -> Option<&Telescope<'arena>> {
match self {
Value::RecordType(_, telescope) => Some(telescope),
_ => None,
}
}

pub fn is_error(&self) -> bool {
matches!(self, Value::Stuck(Head::Prim(Prim::ReportedError), _))
}
Expand Down
19 changes: 19 additions & 0 deletions fathom/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! [`SharedEnv`] to increase the amount of sharing at the expense of locality.

use std::fmt;
use std::ops::Add;

/// Underlying variable representation.
type RawVar = usize;
Expand Down Expand Up @@ -56,6 +57,13 @@ impl Index {
}
}

impl Add<EnvLen> for Index {
type Output = Self;
fn add(self, rhs: EnvLen) -> Self::Output {
Self(self.0 + rhs.0) // FIXME: check overflow?
}
}

impl fmt::Debug for Index {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Index(")?;
Expand Down Expand Up @@ -126,6 +134,13 @@ pub fn levels() -> impl Iterator<Item = Level> {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct EnvLen(RawVar);

impl Add<Index> for EnvLen {
type Output = Self;
fn add(self, rhs: Index) -> Self::Output {
Self(self.0 + rhs.0) // FIXME: check overflow?
}
}

impl EnvLen {
/// Construct a new, empty environment.
pub fn new() -> EnvLen {
Expand All @@ -152,6 +167,10 @@ impl EnvLen {
Level(self.0)
}

pub fn next(&self) -> EnvLen {
Self(self.0 + 1) // FIXME: check overflow?
}

/// Push an entry onto the environment.
pub fn push(&mut self) {
self.0 += 1;
Expand Down
Loading