fix: uniqueness for Num types

This commit is contained in:
rvcas 2020-12-21 11:15:05 -05:00
parent aa4b376134
commit d08757f161
4 changed files with 47 additions and 49 deletions

View file

@ -1210,36 +1210,34 @@ fn lift(u: VarId, a: SolvedType) -> SolvedType {
#[inline(always)]
fn float_type(u: VarId) -> SolvedType {
let b_64 = builtin_aliases::binary64_type();
let attr_b_64 = lift(u, b_64);
let fp = builtin_aliases::floatingpoint_type(attr_b_64);
let attr_fb = lift(u, fp);
let num = builtin_aliases::num_type(attr_fb);
SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![
flex(u),
SolvedType::Alias(
Symbol::NUM_F64,
Vec::new(),
Box::new(builtin_aliases::num_type(SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![flex(u), builtin_aliases::float_type()],
))),
),
SolvedType::Alias(Symbol::NUM_F64, Vec::new(), Box::new(num)),
],
)
}
#[inline(always)]
fn int_type(u: VarId) -> SolvedType {
let signed_64 = builtin_aliases::signed64_type();
let attr_signed_64 = lift(u, signed_64);
let integer = builtin_aliases::integer_type(attr_signed_64);
let attr_fb = lift(u, integer);
let num = builtin_aliases::num_type(attr_fb);
SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![
flex(u),
SolvedType::Alias(
Symbol::NUM_I64,
Vec::new(),
Box::new(builtin_aliases::num_type(SolvedType::Apply(
Symbol::ATTR_ATTR,
vec![flex(u), builtin_aliases::int_type()],
))),
),
SolvedType::Alias(Symbol::NUM_I64, Vec::new(), Box::new(num)),
],
)
}

View file

@ -174,17 +174,17 @@ fn constrain_pattern(
}
IntLiteral(_) => {
let (var1, var2, num_uvar, int_uvar, num_type) = unique_int(var_store);
let (num_uvar, num_type) = unique_int(var_store);
state.constraints.push(exists(
vec![num_uvar, int_uvar, var1, var2],
vec![num_uvar],
Constraint::Pattern(pattern.region, PatternCategory::Int, num_type, expected),
));
}
FloatLiteral(_) => {
let (var1, var2, num_uvar, float_uvar, num_type) = unique_float(var_store);
let (num_uvar, num_type) = unique_float(var_store);
state.constraints.push(exists(
vec![num_uvar, float_uvar, var1, var2],
vec![num_uvar],
Constraint::Pattern(pattern.region, PatternCategory::Float, num_type, expected),
));
}
@ -423,54 +423,49 @@ fn unique_unbound_num(
(num_uvar, val_uvar, num_type, num_var)
}
fn unique_num(var_store: &mut VarStore, val_type: Type) -> (Variable, Variable, Type) {
let num_uvar = var_store.fresh();
let val_uvar = var_store.fresh();
let val_utype = attr_type(Bool::variable(val_uvar), val_type);
fn unique_num(val_type: Type, uvar: Variable) -> Type {
let val_utype = attr_type(Bool::variable(uvar), val_type);
let num_utype = num_num(val_utype);
let num_type = attr_type(Bool::variable(num_uvar), num_utype);
let num_type = attr_type(Bool::variable(uvar), num_utype);
(num_uvar, val_uvar, num_type)
num_type
}
fn unique_integer(var_store: &mut VarStore, val_type: Type) -> (Variable, Variable, Type) {
fn unique_integer(var_store: &mut VarStore, val_type: Type) -> (Variable, Type) {
let num_uvar = var_store.fresh();
let val_uvar = var_store.fresh();
let val_utype = attr_type(Bool::variable(val_uvar), val_type);
let val_utype = attr_type(Bool::variable(num_uvar), val_type);
let num_type = num_integer(val_utype);
(num_uvar, val_uvar, num_type)
(num_uvar, num_type)
}
fn unique_int(var_store: &mut VarStore) -> (Variable, Variable, Variable, Variable, Type) {
let (var1, var2, typ) = unique_integer(var_store, num_signed64());
fn unique_int(var_store: &mut VarStore) -> (Variable, Type) {
let (var1, typ) = unique_integer(var_store, num_signed64());
let (var3, var4, typ) = unique_num(var_store, typ);
let typ = unique_num(typ, var1);
(var1, var2, var3, var4, typ)
(var1, typ)
}
fn unique_floatingpoint(var_store: &mut VarStore, val_type: Type) -> (Variable, Variable, Type) {
fn unique_floatingpoint(var_store: &mut VarStore, val_type: Type) -> (Variable, Type) {
let num_uvar = var_store.fresh();
let val_uvar = var_store.fresh();
let val_utype = attr_type(Bool::variable(val_uvar), val_type);
let val_utype = attr_type(Bool::variable(num_uvar), val_type);
let num_type = num_floatingpoint(val_utype);
(num_uvar, val_uvar, num_type)
(num_uvar, num_type)
}
fn unique_float(var_store: &mut VarStore) -> (Variable, Variable, Variable, Variable, Type) {
let (var1, var2, typ) = unique_floatingpoint(var_store, num_binary64());
fn unique_float(var_store: &mut VarStore) -> (Variable, Type) {
let (var1, typ) = unique_floatingpoint(var_store, num_binary64());
let (var3, var4, typ) = unique_num(var_store, typ);
let typ = unique_num(typ, var1);
(var1, var2, var3, var4, typ)
(var1, typ)
}
pub fn constrain_expr(
@ -503,10 +498,10 @@ pub fn constrain_expr(
)
}
Int(var, _) => {
let (var1, var2, num_uvar, int_uvar, num_type) = unique_int(var_store);
let (num_uvar, num_type) = unique_int(var_store);
exists(
vec![*var, num_uvar, int_uvar, var1, var2],
vec![*var, num_uvar],
And(vec![
Eq(
Type::Variable(*var),
@ -519,10 +514,10 @@ pub fn constrain_expr(
)
}
Float(var, _) => {
let (var1, var2, num_uvar, float_uvar, num_type) = unique_float(var_store);
let (num_uvar, num_type) = unique_float(var_store);
exists(
vec![*var, num_uvar, float_uvar, var1, var2],
vec![*var, num_uvar],
And(vec![
Eq(
Type::Variable(*var),

View file

@ -5853,6 +5853,11 @@ pub fn num_argument_to_int_or_float(subs: &Subs, var: Variable) -> IntOrFloat {
// Recurse on the second argument
num_argument_to_int_or_float(subs, attr_args[1])
}
Content::Alias(Symbol::NUM_F64, args, _) | Content::Alias(Symbol::NUM_F32, args, _) => {
debug_assert!(args.is_empty());
IntOrFloat::FloatType
}
other => {
panic!(
"Unrecognized Num type argument for var {:?} with Content: {:?}",

View file

@ -367,7 +367,7 @@ pub fn binary64_type() -> SolvedType {
}
#[inline(always)]
fn binary64_alias_content() -> SolvedType {
pub fn binary64_alias_content() -> SolvedType {
single_private_tag(Symbol::NUM_AT_BINARY64, vec![])
}
@ -597,7 +597,7 @@ pub fn dict_type(key: SolvedType, value: SolvedType) -> SolvedType {
SolvedType::Apply(Symbol::DICT_DICT, vec![key, value])
}
fn single_private_tag(symbol: Symbol, type_arguments: Vec<SolvedType>) -> SolvedType {
pub fn single_private_tag(symbol: Symbol, type_arguments: Vec<SolvedType>) -> SolvedType {
SolvedType::TagUnion(
vec![(TagName::Private(symbol), type_arguments)],
Box::new(SolvedType::EmptyTagUnion),