Skip to main content

lambda_cat/
value.rs

1//! Runtime values produced by evaluation.
2//!
3//! In the pure untyped lambda calculus the only runtime value is a closure:
4//! a function body paired with the environment that was in scope when the
5//! enclosing lambda was evaluated.
6
7use crate::env::Env;
8use crate::syntax::{Expr, VarName};
9
10/// A runtime value.
11///
12/// Spike 1 has a single variant (closures); spike 2 will extend this enum
13/// with literals, references, and any other values the GC must trace.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum Value {
16    /// A function closed over its lexical environment.
17    Closure {
18        /// The parameter the function binds when applied.
19        param: VarName,
20        /// The function body.
21        body: Expr,
22        /// The captured environment at the point of lambda evaluation.
23        env: Env,
24    },
25}
26
27impl Value {
28    /// Build a closure value.
29    #[must_use]
30    pub fn closure(param: VarName, body: Expr, env: Env) -> Self {
31        Self::Closure { param, body, env }
32    }
33}
34
35impl std::fmt::Display for Value {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            Self::Closure { param, body, .. } => {
39                write!(f, "\\{param}. {body}")
40            }
41        }
42    }
43}