Lines
100 %
Functions
Branches
//! Trial typing: the parameters no use pins take the numeric dimensions under
//! which the body evaluates, when every fitting assignment agrees on them.
use crate::ast::{Expr, LambdaParams, WasmType};
use crate::compiler::expr::eval_value;
use crate::error::Error;
use crate::runtime::SymbolTable;
use super::PLACEHOLDER;
const DIMENSIONS: [WasmType; 3] = [PLACEHOLDER, WasmType::Ratio, WasmType::Commodity];
/// `3^MAX_FREE` probes per lambda; more unpinned parameters get no trial pin.
const MAX_FREE: usize = 4;
/// The joint assignments of the unpinned parameters under which the body
/// evaluates: one dimension per unpinned parameter, in order.
struct Fits {
free: Vec<usize>,
assignments: Vec<Vec<WasmType>>,
}
impl Fits {
/// `None` when no verdict can stand: nothing to try, too many unpinned
/// parameters, or a probe out of its resource budget.
fn of(
symbols: &SymbolTable,
(params, body): (&LambdaParams, &Expr),
pins: &[Option<WasmType>],
) -> Option<Self> {
let free: Vec<usize> = (0..pins.len()).filter(|&i| pins[i].is_none()).collect();
if free.is_empty() || free.len() > MAX_FREE {
return None;
let callee = Expr::Lambda(params.clone(), Box::new(body.clone()));
let mut assignments = Vec::new();
for assignment in assignments_of(free.len()) {
if fits(symbols, &callee, arguments(pins, &free, &assignment))? {
assignments.push(assignment);
Some(Self { free, assignments })
fn dimensions(&self, slot: usize) -> Vec<WasmType> {
let mut seen = Vec::new();
for assignment in &self.assignments {
if !seen.contains(&assignment[slot]) {
seen.push(assignment[slot]);
seen
/// For each parameter no use pins, the dimensions some fitting joint assignment
/// gives it, empty for a pinned parameter; `None` for every parameter when no
/// verdict stands.
pub(super) fn fitting_dimensions(
lambda: (&LambdaParams, &Expr),
) -> Option<Vec<Vec<WasmType>>> {
let fits = Fits::of(symbols, lambda, pins)?;
let mut dimensions = vec![Vec::new(); pins.len()];
for (slot, &index) in fits.free.iter().enumerate() {
dimensions[index] = fits.dimensions(slot);
Some(dimensions)
fn assignments_of(count: usize) -> Vec<Vec<WasmType>> {
(0..count).fold(vec![Vec::new()], |prefixes, _| {
prefixes
.iter()
.flat_map(|prefix| {
DIMENSIONS.map(|ty| {
let mut next = prefix.clone();
next.push(ty);
next
})
.collect()
fn arguments(pins: &[Option<WasmType>], free: &[usize], assignment: &[WasmType]) -> Vec<Expr> {
let mut tried = pins.to_vec();
for (&slot, &ty) in free.iter().zip(assignment) {
tried[slot] = Some(ty);
tried.into_iter().flatten().map(Expr::WasmRuntime).collect()
/// `None` when the probe ran out of its resource budget: no verdict to stand on.
fn fits(symbols: &SymbolTable, callee: &Expr, arguments: Vec<Expr>) -> Option<bool> {
let call = Expr::List(
[Expr::symbol("FUNCALL"), callee.clone()]
.into_iter()
.chain(arguments)
.collect(),
);
match symbols
.speculative_clone()
.within_shared_recursion(|scratch| eval_value(scratch, &call))
{
Err(Error::Resource(_)) => None,
verdict => Some(verdict.is_ok()),