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
#[macro_use]
extern crate log;
#[cfg(test)]
extern crate env_logger;
extern crate union_find;
extern crate smallvec;
extern crate gluon_base as base;
pub mod typecheck;
pub mod unify_type;
pub mod unify;
pub mod kindcheck;
mod substitution;
mod rename;
pub mod completion;
pub mod metadata;
use base::types::{ArcType, TypeEnv};
pub fn check_signature(env: &TypeEnv, signature: &ArcType, actual: &ArcType) -> bool {
use base::kind::Kind;
use base::scoped_map::ScopedMap;
use base::fnv::FnvMap;
use substitution::Substitution;
let subs = Substitution::new(Kind::typ());
let state = unify_type::State::new(env, &subs);
let actual = unify_type::instantiate_generic_variables(&mut FnvMap::default(), &subs, actual);
unify_type::merge_signature(&subs, &mut ScopedMap::new(), 0, state, signature, &actual).is_ok()
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use base::kind::{ArcKind, KindEnv};
use base::symbol::{Symbol, Symbols, SymbolModule, SymbolRef};
use base::types::{Alias, ArcType, TypeEnv};
pub struct MockEnv;
impl KindEnv for MockEnv {
fn find_kind(&self, _type_name: &SymbolRef) -> Option<ArcKind> {
None
}
}
impl TypeEnv for MockEnv {
fn find_type(&self, _id: &SymbolRef) -> Option<&ArcType> {
None
}
fn find_type_info(&self, _id: &SymbolRef) -> Option<&Alias<Symbol, ArcType>> {
None
}
fn find_record(&self, _fields: &[Symbol]) -> Option<(ArcType, ArcType)> {
None
}
}
pub fn get_local_interner() -> Rc<RefCell<Symbols>> {
thread_local!(static INTERNER: Rc<RefCell<Symbols>>
= Rc::new(RefCell::new(Symbols::new())));
INTERNER.with(|interner| interner.clone())
}
pub fn intern(s: &str) -> Symbol {
let interner = get_local_interner();
let mut interner = interner.borrow_mut();
if s.starts_with(char::is_lowercase) {
interner.symbol(s)
} else {
SymbolModule::new("test".into(), &mut interner).scoped_symbol(s)
}
}
}