knave-expand 0.14.22

The reading, scoping and expansion stages of knave
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use std::rc::Rc;

use rustc_hash::{FxHashMap, FxHashSet};

use super::Expander;
use super::budget_arena::BudgetId;
use super::problems::ExpandProblems;
use super::recur_check::RecurCheck;
use super::try_check::TryCheck;
use crate::core::{BindingId, Declaration, Expr};
use crate::diagnostic::Diagnostic;
use crate::interpret::{Interpreter, Value};
use crate::reader::Form;
use crate::scope::Phase;
use crate::source::Span;

#[derive(serde::Serialize, serde::Deserialize)]
enum GlobalState {
    Pending { value: Form, budget: BudgetId },
    Lowering,
    Lowered(Rc<Expr>),
    Failed,
    FailedDefinition(Declaration),
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
pub struct GlobalTable {
    states: FxHashMap<BindingId, GlobalState>,
    values: FxHashMap<BindingId, Value>,
    evaluating: FxHashSet<BindingId>,
    compile_time_checks: FxHashMap<BindingId, CompileTimeCheck>,
    failed_definition_count: usize,
}

#[derive(serde::Serialize, serde::Deserialize)]
pub struct LoweredGlobals(Vec<BindingId>);

#[derive(Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum CompileTimeCheck {
    InProgress,
    Passed,
    Failed,
}

impl GlobalTable {
    #[cfg(test)]
    pub fn holds_no_value(&self) -> bool {
        self.values.is_empty()
    }

    pub fn take_values(&mut self) -> Vec<(BindingId, Value)> {
        let mut values: Vec<(BindingId, Value)> =
            std::mem::take(&mut self.values).into_iter().collect();
        values.sort_by_key(|(binding, _)| *binding);
        values
    }

    pub fn add_pending(&mut self, binding: BindingId, value: Form, budget: BudgetId) {
        self.states
            .insert(binding, GlobalState::Pending { value, budget });
    }

    pub fn compile_time_check(&self, binding: BindingId) -> Option<CompileTimeCheck> {
        self.compile_time_checks.get(&binding).copied()
    }

    pub fn record_compile_time_check(&mut self, binding: BindingId, check: CompileTimeCheck) {
        self.compile_time_checks.insert(binding, check);
    }

    pub fn macro_helpers(&self) -> FxHashSet<BindingId> {
        self.compile_time_checks
            .iter()
            .filter(|(_, check)| **check == CompileTimeCheck::Passed)
            .map(|(binding, _)| *binding)
            .collect()
    }

    pub fn record_failed_definition(&mut self, binding: BindingId, declaration: Declaration) {
        self.states
            .insert(binding, GlobalState::FailedDefinition(declaration));
        self.failed_definition_count += 1;
    }

    pub fn forget_failed_definition(&mut self, binding: BindingId) {
        self.states.remove(&binding);
        self.failed_definition_count -= 1;
    }

    pub fn has_failed_definitions(&self) -> bool {
        self.failed_definition_count > 0
    }

    pub fn is_failed_macro_definition(&self, binding: BindingId) -> bool {
        matches!(
            self.states.get(&binding),
            Some(GlobalState::FailedDefinition(Declaration::Macro))
        )
    }

    pub fn is_failed_definition(&self, binding: BindingId) -> bool {
        matches!(
            self.states.get(&binding),
            Some(GlobalState::FailedDefinition(_))
        )
    }

    pub fn has_failed(&self, binding: BindingId) -> bool {
        matches!(self.states.get(&binding), Some(GlobalState::Failed))
    }

    pub fn take_lowered<'m>(
        &mut self,
        definitions: impl Iterator<Item = (BindingId, &'m Rc<Expr>)>,
    ) -> LoweredGlobals {
        let states = std::mem::take(&mut self.states);
        let mut lowered: Vec<BindingId> = definitions
            .filter(|(binding, value)| {
                states
                    .get(binding)
                    .is_some_and(|state| state.is_lowered_to(value))
            })
            .map(|(binding, _)| binding)
            .collect();
        assert_eq!(
            lowered.len(),
            states.len(),
            "every global to take is lowered to the expression of its definition"
        );
        lowered.sort_unstable();
        LoweredGlobals(lowered)
    }

    pub fn restore_lowered<'m>(
        &mut self,
        lowered: &LoweredGlobals,
        definitions: impl Iterator<Item = (BindingId, &'m Rc<Expr>)>,
    ) {
        for (binding, value) in definitions.filter(|(binding, _)| lowered.contains(*binding)) {
            self.states
                .insert(binding, GlobalState::Lowered(Rc::clone(value)));
        }
    }
}

impl GlobalState {
    fn is_lowered_to(&self, value: &Rc<Expr>) -> bool {
        matches!(self, Self::Lowered(expr) if Rc::ptr_eq(expr, value))
    }
}

impl LoweredGlobals {
    fn contains(&self, binding: BindingId) -> bool {
        self.0.binary_search(&binding).is_ok()
    }
}

impl Expander<'_> {
    pub(super) fn lowered_global(
        &mut self,
        binding: BindingId,
        span: Span,
    ) -> Result<Rc<Expr>, Diagnostic> {
        self.decode_saved_global(binding);
        match self
            .tables
            .globals
            .states
            .insert(binding, GlobalState::Lowering)
        {
            Some(GlobalState::Pending { value, budget }) => {
                self.spending(budget, |expander| expander.lower_global(binding, &value))
            }
            Some(GlobalState::Lowered(expr)) => {
                self.tables
                    .globals
                    .states
                    .insert(binding, GlobalState::Lowered(Rc::clone(&expr)));
                Ok(expr)
            }
            Some(failed @ (GlobalState::Failed | GlobalState::FailedDefinition(_))) => {
                self.tables.globals.states.insert(binding, failed);
                Err(ExpandProblems::broken_definition(
                    self.tables.bindings.get(binding).name(),
                    span,
                ))
            }
            Some(GlobalState::Lowering) | None => Err(ExpandProblems::compile_time_cycle(
                self.tables.bindings.get(binding).name(),
                span,
            )),
        }
    }

    fn decode_saved_global(&mut self, binding: BindingId) {
        let states = &mut self.tables.globals.states;
        if !states.contains_key(&binding)
            && let Some(value) = self.saved_definitions.value_of(binding)
        {
            states.insert(binding, GlobalState::Lowered(value));
        }
    }

    pub(super) fn evaluate_global(
        &mut self,
        binding: BindingId,
        span: Span,
    ) -> Result<Value, Diagnostic> {
        if let Some(value) = self.tables.globals.values.get(&binding) {
            return Ok(value.clone());
        }
        if let Some(value) = self.saved_globals.value_of(binding) {
            self.tables.globals.values.insert(binding, value.clone());
            return Ok(value);
        }
        if !self.tables.globals.evaluating.insert(binding) {
            return Err(ExpandProblems::compile_time_cycle(
                self.tables.bindings.get(binding).name(),
                span,
            ));
        }
        let result = self.lowered_global(binding, span).and_then(|expr| {
            let steps = self.form_budgets.current().steps();
            Interpreter::new(self, span, steps).evaluate(&expr)
        });
        self.tables.globals.evaluating.remove(&binding);
        let value = result?;
        self.tables.globals.values.insert(binding, value.clone());
        Ok(value)
    }

    fn lower_global(&mut self, binding: BindingId, form: &Form) -> Result<Rc<Expr>, Diagnostic> {
        let outer_phase = std::mem::replace(&mut self.phase, Phase::Runtime);
        let errors_before = self.recovered.mark();
        let lowered = self.with_fresh_type_note(|expander| expander.lower_expr(form));
        self.phase = outer_phase;
        match lowered {
            Ok(expr) => {
                if let Some(first_error) = self.recovered.take_first_since(errors_before) {
                    self.tables
                        .globals
                        .states
                        .insert(binding, GlobalState::Failed);
                    return Err(first_error);
                }
                RecurCheck::outside().value(&expr)?;
                TryCheck::outside().value(&expr)?;
                let expr = Rc::new(expr);
                self.tables
                    .globals
                    .states
                    .insert(binding, GlobalState::Lowered(Rc::clone(&expr)));
                Ok(expr)
            }
            Err(diagnostic) => {
                self.tables
                    .globals
                    .states
                    .insert(binding, GlobalState::Failed);
                Err(diagnostic)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::core::{
        BindingArena, BindingInfo, BindingKind, ExprKind, Leaf, Literal, ModuleId, ModulePath,
    };
    use crate::expand::PreludeState;
    use crate::modules::ModuleSources;
    use crate::source::{SourceId, SourceMap};

    struct SavedGlobalFixture;

    impl SavedGlobalFixture {
        const ENTRY: &str = "global-user";

        fn with_loaded_expander(run: impl FnOnce(&mut Expander<'_>, SourceId)) {
            let state = PreludeState::load(PreludeState::build().save().leak())
                .expect("the saved prelude loads");
            let mut sources = SourceMap::default();
            let entry = sources.add(Self::ENTRY, "");
            let module_sources = ModuleSources::new(PathBuf::new(), PathBuf::new());
            let mut expander = Expander::with_prelude(&mut sources, module_sources, state);
            expander.load_source_module(ModulePath::new(Self::ENTRY), &[entry]);
            run(&mut expander, entry);
        }

        fn find(bindings: &BindingArena, name: &str, kind: fn(BindingKind) -> bool) -> BindingId {
            let mut found = bindings.ids().filter(|binding| {
                let info = bindings.get(*binding);
                info.name() == name && kind(info.kind())
            });
            found.next().expect("the prelude binds the name")
        }
    }

    #[test]
    fn evaluating_a_saved_global_decodes_its_value_without_lowering_it() {
        SavedGlobalFixture::with_loaded_expander(|expander, entry| {
            let is_global = |kind| matches!(kind, BindingKind::Global(_));
            let helper = SavedGlobalFixture::find(&expander.tables.bindings, "map-list", is_global);
            expander
                .evaluate_global(helper, Span::new(entry, 0..0))
                .unwrap_or_else(|_| panic!("map-list evaluates"));
            assert!(!expander.tables.globals.states.contains_key(&helper));
            assert!(expander.tables.globals.values.contains_key(&helper));
        });
    }

    struct Definitions {
        bindings: BindingArena,
        values: Vec<(BindingId, Rc<Expr>)>,
    }

    impl Definitions {
        fn new() -> Self {
            Self {
                bindings: BindingArena::default(),
                values: Vec::new(),
            }
        }

        fn define(&mut self, name: &str) -> BindingId {
            let kind = BindingKind::Global(ModuleId::new(0));
            let binding = self.bindings.add(BindingInfo::new(name.into(), kind, None));
            let span = Span::new(SourceId::new(0), 0..0);
            let value = Expr::new(ExprKind::Leaf(Leaf::Literal(Literal::Bool(true))), span);
            self.values.push((binding, Rc::new(value)));
            binding
        }

        fn lower(&self, table: &mut GlobalTable, binding: BindingId) {
            table.states.insert(
                binding,
                GlobalState::Lowered(Rc::clone(self.value(binding))),
            );
        }

        fn value(&self, binding: BindingId) -> &Rc<Expr> {
            &self
                .values
                .iter()
                .find(|(defined, _)| *defined == binding)
                .expect("the binding is defined")
                .1
        }

        fn newest_first(&self) -> impl Iterator<Item = (BindingId, &Rc<Expr>)> {
            self.values
                .iter()
                .rev()
                .map(|(binding, value)| (*binding, value))
        }
    }

    #[test]
    fn a_restore_brings_back_only_the_lowered_globals() {
        let mut definitions = Definitions::new();
        let lowered = [definitions.define("fuel"), definitions.define("range")];
        let method = definitions.define("charge");
        let mut built = GlobalTable::default();
        lowered
            .iter()
            .for_each(|binding| definitions.lower(&mut built, *binding));
        let taken = built.take_lowered(definitions.newest_first());
        let mut restored = GlobalTable::default();
        restored.restore_lowered(&taken, definitions.newest_first());
        assert!(lowered.iter().all(|binding| {
            restored.states[binding].is_lowered_to(definitions.value(*binding))
        }));
        assert!(!restored.states.contains_key(&method));
    }

    #[test]
    fn a_take_leaves_no_state_behind() {
        let mut definitions = Definitions::new();
        let binding = definitions.define("fuel");
        let mut table = GlobalTable::default();
        definitions.lower(&mut table, binding);
        table.take_lowered(definitions.newest_first());
        assert!(table.states.is_empty());
    }

    #[test]
    #[should_panic(
        expected = "every global to take is lowered to the expression of its definition"
    )]
    fn a_global_lowered_to_another_expression_stops_the_take() {
        let mut definitions = Definitions::new();
        let binding = definitions.define("fuel");
        let mut table = GlobalTable::default();
        let copy = Expr::clone(definitions.value(binding));
        table
            .states
            .insert(binding, GlobalState::Lowered(Rc::new(copy)));
        table.take_lowered(definitions.newest_first());
    }
}