expand aliases used in constraint gen

This commit is contained in:
Folkert 2020-10-29 22:31:14 +01:00
parent 2478ae05b1
commit 8a50d48ce2
2 changed files with 52 additions and 3 deletions

View file

@ -2,6 +2,7 @@ use roc_can::constraint::Constraint::{self, *};
use roc_can::constraint::LetConstraint;
use roc_can::expected::Expected::{self, *};
use roc_collections::all::SendMap;
use roc_module::ident::TagName;
use roc_module::symbol::Symbol;
use roc_region::all::Region;
use roc_types::subs::Variable;
@ -13,7 +14,7 @@ use roc_types::types::Type::{self, *};
pub fn int_literal(num_var: Variable, expected: Expected<Type>, region: Region) -> Constraint {
let num_type = Variable(num_var);
let reason = Reason::IntLiteral;
let expected_literal = ForReason(reason, Type::Apply(Symbol::NUM_INT, vec![]), region);
let expected_literal = ForReason(reason, num_int(), region);
exists(
vec![num_var],
@ -28,7 +29,7 @@ pub fn int_literal(num_var: Variable, expected: Expected<Type>, region: Region)
pub fn float_literal(num_var: Variable, expected: Expected<Type>, region: Region) -> Constraint {
let num_type = Variable(num_var);
let reason = Reason::FloatLiteral;
let expected_literal = ForReason(reason, Type::Apply(Symbol::NUM_FLOAT, vec![]), region);
let expected_literal = ForReason(reason, num_float(), region);
exists(
vec![num_var],
@ -70,3 +71,51 @@ pub fn list_type(typ: Type) -> Type {
pub fn str_type() -> Type {
builtin_type(Symbol::STR_STR, Vec::new())
}
#[inline(always)]
pub fn num_float() -> Type {
Type::Alias(
Symbol::NUM_FLOAT,
vec![],
Box::new(num_num(num_floatingpoint())),
)
}
#[inline(always)]
pub fn num_floatingpoint() -> Type {
let alias_content = Type::TagUnion(
vec![(TagName::Private(Symbol::NUM_AT_FLOATINGPOINT), vec![])],
Box::new(Type::EmptyTagUnion),
);
Type::Alias(Symbol::NUM_FLOATINGPOINT, vec![], Box::new(alias_content))
}
#[inline(always)]
pub fn num_int() -> Type {
Type::Alias(Symbol::NUM_INT, vec![], Box::new(num_num(num_integer())))
}
#[inline(always)]
pub fn num_integer() -> Type {
let alias_content = Type::TagUnion(
vec![(TagName::Private(Symbol::NUM_AT_INTEGER), vec![])],
Box::new(Type::EmptyTagUnion),
);
Type::Alias(Symbol::NUM_INTEGER, vec![], Box::new(alias_content))
}
#[inline(always)]
pub fn num_num(typ: Type) -> Type {
let alias_content = Type::TagUnion(
vec![(TagName::Private(Symbol::NUM_AT_NUM), vec![typ.clone()])],
Box::new(Type::EmptyTagUnion),
);
Type::Alias(
Symbol::NUM_NUM,
vec![("range".into(), typ)],
Box::new(alias_content),
)
}