Skip to main content

semi_persistent_egraph/
lit_model.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3//! Literal value model — defines the value representation, literal sorts,
4//! and primitive operations available for computation in rules.
5//!
6//! Clients implement `LitModel` to plug their literal value type into the
7//! e-graph, parser, and rewriting engine.
8
9use crate::literal::LitVal;
10
11/// Descriptor for a literal sort (e.g. "Int" backed by `IBig`).
12pub struct LitSortDesc<V> {
13    /// Name in surface syntax: "Int", "Bool", "Rational", etc.
14    pub name: &'static str,
15    /// Parse a surface-syntax token into a value of this sort.
16    pub parse: fn(&str) -> Option<V>,
17}
18
19impl<V> Clone for LitSortDesc<V> {
20    fn clone(&self) -> Self {
21        *self
22    }
23}
24impl<V> Copy for LitSortDesc<V> {}
25
26/// Descriptor for a primitive operation on literal values.
27pub struct LitOpDesc<V> {
28    /// Name in surface syntax: "+", "*", "not", "<", etc.
29    pub name: &'static str,
30    /// Argument literal sort names.
31    pub arg_sorts: &'static [&'static str],
32    /// Return literal sort name.
33    pub ret_sort: &'static str,
34    /// Evaluate the operation on concrete values.
35    pub eval: fn(&[&V]) -> V,
36}
37
38impl<V> Clone for LitOpDesc<V> {
39    fn clone(&self) -> Self {
40        *self
41    }
42}
43impl<V> Copy for LitOpDesc<V> {}
44
45/// Client-defined literal value model.
46///
47/// Bundles the value representation, literal sorts, primitive operations,
48/// and sort classification. The e-graph, parser, and execution engine are
49/// parameterized over this trait.
50pub trait LitModel: 'static {
51    /// The concrete literal value type (typically an enum).
52    type Value: LitVal;
53
54    /// Available literal sorts.
55    fn sorts(&self) -> &[LitSortDesc<Self::Value>];
56
57    /// Available primitive operations on literal values.
58    fn ops(&self) -> &[LitOpDesc<Self::Value>];
59
60    /// Which literal sort does this value belong to?
61    fn sort_of(val: &Self::Value) -> &'static str;
62
63    /// Try to parse a token as a literal of a specific sort.
64    fn parse_as(&self, sort_name: &str, token: &str) -> Option<Self::Value> {
65        self.sorts()
66            .iter()
67            .find(|s| s.name == sort_name)
68            .and_then(|s| (s.parse)(token))
69    }
70
71    /// Try to parse a token as a literal of any sort.
72    /// Returns `(sort_name, value)` on success.
73    fn parse_any(&self, token: &str) -> Option<(&'static str, Self::Value)> {
74        for sort in self.sorts() {
75            if let Some(v) = (sort.parse)(token) {
76                return Some((sort.name, v));
77            }
78        }
79        None
80    }
81
82    /// Look up a primitive op by name.
83    fn find_op(&self, name: &str) -> Option<&LitOpDesc<Self::Value>> {
84        self.ops().iter().find(|op| op.name == name)
85    }
86
87    /// Is this identifier a literal sort name?
88    fn is_lit_sort(&self, name: &str) -> bool {
89        self.sorts().iter().any(|s| s.name == name)
90    }
91
92    /// Is this literal value truthy? Used for comprehension filter guards.
93    fn is_truthy(val: &Self::Value) -> bool;
94}