Skip to content

Commit

Permalink
Make a function so that you can construct a goal atom from a single P…
Browse files Browse the repository at this point in the history
…redicateSym.

PiperOrigin-RevId: 712821513
  • Loading branch information
Mangle Team authored and copybara-github committed Jan 7, 2025
1 parent fa01a88 commit 7beb0d1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions rust/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ impl<'a> std::fmt::Display for Atom<'a> {
}
}

static ANY_VAR_TERM: BaseTerm = BaseTerm::Variable("_");

pub fn new_query<'dest>(bump: &'dest Bump, predicate: &'dest PredicateSym) -> Atom<'dest> {
let args: Vec<_> = match predicate.arity {
Some(arity) => (0..arity).map(|_i| &ANY_VAR_TERM).collect(),
None => Vec::new(),
};

let args = &*bump.alloc_slice_copy(&args);

Atom {
sym: *predicate,
args,
}
}

pub fn copy_predicate_sym<'dest>(bump: &'dest Bump, p: PredicateSym) -> PredicateSym<'dest> {
PredicateSym {
name: bump.alloc_str(p.name),
Expand Down Expand Up @@ -454,4 +470,30 @@ mod tests {
assert_that!(term, displays_as(eq(s)));
}
}

#[test]
fn new_query_works() {
let bump = Bump::new();

let pred = PredicateSym {
name: "foo",
arity: Some(1),
};
let query = new_query(&bump, &pred);
assert_that!(query, displays_as(eq("foo(_)")));

let pred = PredicateSym {
name: "foo",
arity: Some(2),
};
let query = new_query(&bump, &pred);
assert_that!(query, displays_as(eq("foo(_, _)")));

let pred = PredicateSym {
name: "none",
arity: None,
};
let query = new_query(&bump, &pred);
assert_that!(query, displays_as(eq("none()")));
}
}

0 comments on commit 7beb0d1

Please sign in to comment.