mangle_analysis/lib.rs
1// Copyright 2024 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use mangle_ast as ast;
16
17mod simple_program;
18
19pub use simple_program::{SimpleProgram, SimpleStratifiedProgram};
20
21type PredicateSet<'a> = std::collections::HashSet<&'a ast::PredicateSym<'a>>;
22
23/// Represents a program.
24/// `extensional_preds` and `intensional_preds` are disjoint.
25pub trait Program<'p> {
26 /// Returns predicates for extensional DB.
27 /// May return an empty iterator.
28 fn extensional_preds(&'p self) -> impl Iterator<Item = &'p ast::PredicateSym<'p>>;
29
30 /// Returns predicates for intensional DB.
31 /// May return an empty iterator.
32 fn intensional_preds(&'p self) -> impl Iterator<Item = &'p ast::PredicateSym<'p>>;
33
34 /// Maps predicates of intensional DB to rules.
35 /// May return an empty iterator.
36 fn rules(&'p self, sym: &'p ast::PredicateSym<'p>)
37 -> impl Iterator<Item = &'p ast::Clause<'p>>;
38}
39
40// A stratified program is a program that can be separated in
41// dependency layers (strata) such that if index(p) < index(q)
42// then p does not depend on q.
43pub trait StratifiedProgram<'p>: Program<'p> {
44 /// Returns an iterator of strata, in dependency order.
45 /// TODO: consider Iterator<Iterator<PredicateSet>>.
46 fn strata(&'p self) -> impl Iterator<Item = &'p PredicateSet<'p>>;
47
48 /// Returns the stratum (index into strata list).
49 fn pred_to_index(&'p self, sym: &ast::PredicateSym) -> Option<usize>;
50}