fetish_lib/
interpreter_and_embedder_state.rs

1extern crate ndarray;
2extern crate ndarray_linalg;
3
4use ndarray::*;
5use ndarray_linalg::*;
6
7use crate::displayable_with_state::*;
8use crate::prior_specification::*;
9use rand::prelude::*;
10use crate::context::*;
11use crate::sampled_embedder_state::*;
12use crate::linalg_utils::*;
13use crate::array_utils::*;
14use crate::type_id::*;
15use crate::params::*;
16use crate::term_pointer::*;
17use crate::term_reference::*;
18use crate::term_application::*;
19use crate::embedder_state::*;
20use crate::interpreter_state::*;
21use crate::newly_evaluated_terms::*;
22
23use crate::term_application_result::*;
24
25///A more convenient, stateful wrapper around an [`InterpreterState`] and [`EmbedderState`]
26///which automatically tracks any [`NewlyEvaluatedTerms`] originating from evaluations,
27///and an interface which allows for using these to update the wrapped [`EmbedderState`]
28///at a caller-chosen time.
29pub struct InterpreterAndEmbedderState<'a> {
30    pub interpreter_state : InterpreterState<'a>,
31    pub embedder_state : EmbedderState<'a>,
32    pub newly_evaluated_terms : NewlyEvaluatedTerms
33}
34
35impl<'a> InterpreterAndEmbedderState<'a> {
36    ///Gets the [`Context`] that this [`InterpreterAndEmbedderState`] exists in.
37    pub fn get_context(&self) -> &Context {
38        self.interpreter_state.get_context()
39    }
40    ///Given a [`TermApplication`], uses the wrapped [`InterpreterState`] to evaluate
41    ///the application, returning a `TermReference` for the result of the evaluation.
42    ///Any newly-evaluated terms which result from evaluation will be added to
43    ///this [`InterpreterAndEmbedderState`]'s `newly_evaluated_terms` member variable.
44    pub fn evaluate(&mut self, term_app : &TermApplication) -> TermReference {
45        let (result_ref, newly_evaluated_terms) = self.interpreter_state.evaluate(term_app);
46        self.newly_evaluated_terms.merge(newly_evaluated_terms);
47        result_ref
48    }
49    ///Convenience method to force the wrapped [`InterpreterState`] to have at least
50    ///one term inhabiting every type, assuming that it doesn't really matter what these are.
51    ///Calling this method will result in every newly-added term being added to the
52    ///wrapped [`NewlyEvaluatedTerms`]
53    pub fn ensure_every_type_has_a_term_on_init(&mut self) {
54        let newly_evaluated_terms = self.interpreter_state.ensure_every_type_has_a_term_on_init();
55        self.newly_evaluated_terms.merge(newly_evaluated_terms);
56    }
57    ///Uses the wrapped [`NewlyEvaluatedTerms`] and [`InterpreterState`] to update the embeddings
58    ///within the wrapped [`EmbedderState`]. Calling this method will not modfiy the wrapped
59    ///[`NewlyEvaluatedTerms`], in case they are still of use after an embedding update in
60    ///your particular use-case.
61    pub fn bayesian_update_step(&mut self) {
62        self.embedder_state.bayesian_update_step(&self.interpreter_state, &self.newly_evaluated_terms);
63    }
64
65    ///Clears out the wrapped [`NewlyEvaluatedTerms`], which typically will indicate that 
66    ///a new cycle of evaluations of terms against the [`InterpreterState`] is about to begin.
67    pub fn clear_newly_received(&mut self) {
68        self.newly_evaluated_terms = NewlyEvaluatedTerms::new();
69    }
70
71    ///Constructs a new [`InterpreterAndEmbedderState`] with the given [`PriorSpecification`]s
72    ///for [`crate::term_model::TermModel`]s and [`crate::elaborator::Elaborator`]s within the given [`Context`].
73    pub fn new(model_prior_specification : &'a dyn PriorSpecification,
74               elaborator_prior_specification : &'a dyn PriorSpecification, 
75               ctxt : &'a Context) -> InterpreterAndEmbedderState<'a> {
76        let interpreter_state = InterpreterState::new(ctxt);
77        let embedder_state = EmbedderState::new(model_prior_specification, elaborator_prior_specification, ctxt);
78        let newly_evaluated_terms = NewlyEvaluatedTerms::new();
79        InterpreterAndEmbedderState {
80            interpreter_state,
81            embedder_state,
82            newly_evaluated_terms
83        }
84    }
85}