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
#![doc(html_root_url = "https://docs.rs/gluon_check/0.11.1")]
extern crate codespan;
extern crate codespan_reporting;
#[macro_use]
extern crate collect_mac;
#[cfg(test)]
extern crate env_logger;
extern crate itertools;
#[macro_use]
extern crate log;
extern crate pretty;
extern crate rpds;
extern crate smallvec;
extern crate stable_deref_trait;
extern crate strsim;
extern crate union_find;
#[macro_use]
extern crate gluon_base as base;
#[macro_use]
extern crate gluon_codegen;
pub mod kindcheck;
pub mod metadata;
mod recursion_check;
pub mod rename;
pub mod substitution;
mod typ;
pub mod typecheck;
pub mod unify;
pub mod unify_type;
mod implicits;
use crate::base::{
fnv::FnvMap,
kind::Kind,
metadata::MetadataEnv,
symbol::Symbol,
types::{translate_type, ArcType, PrimitiveEnv, SharedInterner, TypeEnv, TypeExt},
};
use crate::{substitution::Substitution, typ::RcType};
pub fn check_signature(
env: &TypecheckEnv<Type = ArcType>,
signature: &ArcType,
actual: &ArcType,
) -> bool {
let interner = SharedInterner::default();
let signature = translate_type(&mut &interner, signature);
let actual = translate_type(&mut &interner, actual);
check_signature_(&env, &interner, &signature, &actual)
}
fn check_signature_(
env: &TypeEnv<Type = RcType>,
interner: &SharedInterner<Symbol, RcType>,
signature: &RcType,
actual: &RcType,
) -> bool {
let subs = Substitution::new(Kind::typ(), interner.clone());
let state = unify_type::State::new(env, &subs);
let actual = actual.instantiate_generics(&mut &subs, &mut FnvMap::default());
let result = unify_type::subsumes(&subs, state, signature, &actual);
if let Err((_, ref err)) = result {
warn!("Check signature error: {}", err);
}
result.is_ok()
}
pub trait TypecheckEnv: PrimitiveEnv + MetadataEnv {}
impl<T> TypecheckEnv for T where T: PrimitiveEnv + MetadataEnv {}
#[cfg(test)]
mod tests {
use super::*;
use std::cell::RefCell;
use std::rc::Rc;
use crate::base::kind::{ArcKind, KindEnv};
use crate::base::symbol::{Symbol, SymbolModule, SymbolRef, Symbols};
use crate::base::types::{Alias, TypeEnv};
pub struct MockEnv;
impl KindEnv for MockEnv {
fn find_kind(&self, _type_name: &SymbolRef) -> Option<ArcKind> {
None
}
}
impl TypeEnv for MockEnv {
type Type = RcType;
fn find_type(&self, _id: &SymbolRef) -> Option<&RcType> {
None
}
fn find_type_info(&self, _id: &SymbolRef) -> Option<&Alias<Symbol, RcType>> {
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)
}
}
}