fetish_lib/
context.rs

1use crate::type_id::*;
2use crate::space_info::*;
3use crate::func_impl::*;
4use crate::function_space_info::*;
5use crate::feature_space_info::*;
6use crate::primitive_directory::*;
7use crate::primitive_term_pointer::*;
8
9///Stores interpreter-global context information, such as the
10///collection of all types in the language, the collection of all
11///primitives, and the definitions of their associated featurization
12///maps. See [`TypeInfoDirectory`], [`SpaceInfoDirectory`], [`PrimitiveDirectory`]
13///for these individual components.
14pub struct Context {
15    pub type_info_directory : TypeInfoDirectory,
16    pub space_info_directory : SpaceInfoDirectory,
17    pub primitive_directory : PrimitiveDirectory
18}
19
20impl Context {
21    //Primitive information
22    
23    ///Given a [`PrimitiveTermPointer`], yields the [`FuncImpl`] it references.
24    pub fn get_primitive(&self, primitive_term_pointer : PrimitiveTermPointer) -> &dyn FuncImpl {
25        self.primitive_directory.get_primitive(primitive_term_pointer)
26    }
27
28    //Space information
29    
30    ///Gets a reference to the [`FeatureSpaceInfo`] for the given [`TypeId`].
31    pub fn get_feature_space_info(&self, type_id : TypeId) -> &FeatureSpaceInfo {
32        self.space_info_directory.get_feature_space_info(type_id)
33    }
34
35    ///Gets the [`FunctionSpaceInfo`] for functions going from the given `arg_type_id` to the
36    ///given `ret_type_id`.
37    pub fn build_function_space_info(&self, arg_type_id : TypeId, ret_type_id : TypeId) -> FunctionSpaceInfo {
38        let arg_feat_info = self.get_feature_space_info(arg_type_id);
39        let ret_feat_info = self.get_feature_space_info(ret_type_id);
40        FunctionSpaceInfo {
41            in_feat_info : arg_feat_info,
42            out_feat_info : ret_feat_info
43        }
44    }
45    ///Gets the [`FunctionSpaceInfo`] for the given function [`TypeId`].
46    pub fn get_function_space_info(&self, func_type_id : TypeId) -> FunctionSpaceInfo {
47        let func_type = self.get_type(func_type_id);
48        match (func_type) {
49            Type::FuncType(arg_type_id, ret_type_id) => {
50                let arg_feat_info = self.get_feature_space_info(arg_type_id);
51                let ret_feat_info = self.get_feature_space_info(ret_type_id);
52                FunctionSpaceInfo {
53                    in_feat_info : arg_feat_info,
54                    out_feat_info : ret_feat_info
55                }
56            },
57            Type::VecType(_) => {
58                panic!();
59            }
60        }
61    }
62
63    //Type Information
64    ///Given the argument and result types for a function type, returns the
65    ///[`TypeId`] of the corresponding function type, assuming that it exists.
66    pub fn get_func_type_id(&self, arg_type_id : TypeId, ret_type_id : TypeId) -> TypeId {
67        self.type_info_directory.get_func_type_id(arg_type_id, ret_type_id)
68    }
69    ///Given a [`TypeId`], yields the [`Type`] struct describing the type.
70    pub fn get_type(&self, id : TypeId) -> Type {
71        self.type_info_directory.get_type(id)
72    }
73    ///Given the argument and result types for a function type, returns true iff
74    ///the function type actually exists in the [`TypeInfoDirectory`].
75    pub fn has_func_type(&self, arg_type_id : TypeId, ret_type_id : TypeId) -> bool {
76        self.type_info_directory.has_func_type(arg_type_id, ret_type_id)
77    }
78    ///Given a target type, yields the collection of all pairs `(func_type_id, arg_type_id)`
79    ///in the [`TypeInfoDirectory`] for which the return type is the given target.
80    pub fn get_application_type_ids(&self, id : TypeId) -> Vec::<(TypeId, TypeId)> {
81        self.type_info_directory.get_application_type_ids(id)
82    }
83    ///Returns the total number of types registered in the [`TypeInfoDirectory`].
84    pub fn get_total_num_types(&self) -> usize {
85        self.type_info_directory.get_total_num_types()
86    }
87    ///Given the [`TypeId`] of a vector type, yields the dimensionality of the
88    ///corresponding vector space.
89    pub fn get_dimension(&self, vec_type_id : TypeId) -> usize {
90        self.type_info_directory.get_dimension(vec_type_id)
91    }
92    ///Given the [`TypeId`] of a function type, yields the [`TypeId`] of the
93    ///function's argument type.
94    pub fn get_arg_type_id(&self, func_type_id : TypeId) -> TypeId {
95        self.type_info_directory.get_arg_type_id(func_type_id)
96    }
97    ///Given the [`TypeId`] of a function type, yields the [`TypeId`] of the
98    ///function's return type.
99    pub fn get_ret_type_id(&self, func_type_id : TypeId) -> TypeId {
100        self.type_info_directory.get_ret_type_id(func_type_id)
101    }
102    ///Given a [`TypeId`], returns `true` if the underlying [`Type`] is
103    ///a vector type, and `false` if it's a function type instead.
104    pub fn is_vector_type(&self, id : TypeId) -> bool {
105        self.type_info_directory.is_vector_type(id)
106    }
107}