feat: add inner var to IntLiteral and FloatLiteral

This commit is contained in:
rvcas 2020-12-29 20:52:54 -05:00
parent d62471c73a
commit 127c4e1bcc
12 changed files with 92 additions and 74 deletions

View file

@ -1242,9 +1242,8 @@ 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 inner_type = lift(u, flex(u));
let fp = builtin_aliases::floatingpoint_type(inner_type.clone());
let attr_fb = lift(u, fp);
let num = builtin_aliases::num_type(attr_fb);
@ -1252,16 +1251,19 @@ fn float_type(u: VarId) -> SolvedType {
Symbol::ATTR_ATTR,
vec![
flex(u),
SolvedType::Alias(Symbol::NUM_F64, Vec::new(), Box::new(num)),
SolvedType::Alias(
Symbol::NUM_FLOAT,
vec![("range".into(), inner_type)],
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 inner_type = lift(u, flex(u));
let integer = builtin_aliases::integer_type(inner_type.clone());
let attr_fb = lift(u, integer);
let num = builtin_aliases::num_type(attr_fb);
@ -1269,7 +1271,11 @@ fn int_type(u: VarId) -> SolvedType {
Symbol::ATTR_ATTR,
vec![
flex(u),
SolvedType::Alias(Symbol::NUM_I64, Vec::new(), Box::new(num)),
SolvedType::Alias(
Symbol::NUM_INT,
vec![("range".into(), inner_type)],
Box::new(num),
),
],
)
}

View file

@ -735,8 +735,8 @@ fn pattern_to_vars_by_symbol(
}
NumLiteral(_, _)
| IntLiteral(_)
| FloatLiteral(_)
| IntLiteral(_, _)
| FloatLiteral(_, _)
| StrLiteral(_)
| Underscore
| MalformedPattern(_, _)
@ -864,7 +864,9 @@ fn canonicalize_pending_def<'a>(
}
}
Alias { name, ann, vars } => {
Alias {
name, ann, vars, ..
} => {
let symbol = name.value;
let can_ann = canonicalize_annotation(env, scope, &ann.value, ann.region, var_store);
@ -1407,7 +1409,9 @@ fn to_pending_def<'a>(
}
}
Alias { name, vars, ann } => {
Alias {
name, vars, ann, ..
} => {
let region = Region::span_across(&name.region, &ann.region);
match scope.introduce(

View file

@ -363,8 +363,8 @@ fn fix_values_captured_in_closure_pattern(
}
Identifier(_)
| NumLiteral(_, _)
| IntLiteral(_)
| FloatLiteral(_)
| IntLiteral(_, _)
| FloatLiteral(_, _)
| StrLiteral(_)
| Underscore
| Shadowed(_, _)

View file

@ -25,9 +25,9 @@ pub enum Pattern {
ext_var: Variable,
destructs: Vec<Located<RecordDestruct>>,
},
IntLiteral(i64),
IntLiteral(Variable, i64),
NumLiteral(Variable, i64),
FloatLiteral(f64),
FloatLiteral(Variable, f64),
StrLiteral(Box<str>),
Underscore,
@ -86,8 +86,8 @@ pub fn symbols_from_pattern_help(pattern: &Pattern, symbols: &mut Vec<Symbol>) {
}
NumLiteral(_, _)
| IntLiteral(_)
| FloatLiteral(_)
| IntLiteral(_, _)
| FloatLiteral(_, _)
| StrLiteral(_)
| Underscore
| MalformedPattern(_, _)
@ -191,7 +191,7 @@ pub fn canonicalize_pattern<'a>(
let problem = MalformedPatternProblem::MalformedFloat;
malformed_pattern(env, problem, region)
}
Ok(float) => Pattern::FloatLiteral(float),
Ok(float) => Pattern::FloatLiteral(var_store.fresh(), float),
},
ptype => unsupported_pattern(env, ptype, region),
},
@ -224,9 +224,9 @@ pub fn canonicalize_pattern<'a>(
}
Ok(int) => {
if *is_negative {
Pattern::IntLiteral(-int)
Pattern::IntLiteral(var_store.fresh(), -int)
} else {
Pattern::IntLiteral(int)
Pattern::IntLiteral(var_store.fresh(), int)
}
}
},
@ -463,8 +463,8 @@ fn add_bindings_from_patterns(
}
}
NumLiteral(_, _)
| IntLiteral(_)
| FloatLiteral(_)
| IntLiteral(_, _)
| FloatLiteral(_, _)
| StrLiteral(_)
| Underscore
| Shadowed(_, _)

View file

@ -14,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, num_int(), region);
let expected_literal = ForReason(reason, num_int(Type::Variable(num_var)), region);
exists(
vec![num_var],
@ -29,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, num_float(), region);
let expected_literal = ForReason(reason, num_float(Type::Variable(num_var)), region);
exists(
vec![num_var],
@ -72,11 +72,11 @@ pub fn str_type() -> Type {
}
#[inline(always)]
pub fn num_float() -> Type {
pub fn num_float(range: Type) -> Type {
Type::Alias(
Symbol::NUM_F64,
vec![],
Box::new(num_num(num_floatingpoint(num_binary64()))),
Symbol::NUM_FLOAT,
vec![("range".into(), range.clone())],
Box::new(num_num(num_floatingpoint(range))),
)
}
@ -108,11 +108,11 @@ pub fn num_binary64() -> Type {
}
#[inline(always)]
pub fn num_int() -> Type {
pub fn num_int(range: Type) -> Type {
Type::Alias(
Symbol::NUM_I64,
vec![],
Box::new(num_num(num_integer(num_signed64()))),
Symbol::NUM_INT,
vec![("range".into(), range.clone())],
Box::new(num_num(num_integer(range))),
)
}

View file

@ -57,8 +57,8 @@ fn headers_from_annotation_help(
| MalformedPattern(_, _)
| UnsupportedPattern(_)
| NumLiteral(_, _)
| IntLiteral(_)
| FloatLiteral(_)
| IntLiteral(_, _)
| FloatLiteral(_, _)
| StrLiteral(_) => true,
RecordDestructure { destructs, .. } => match annotation.value.shallow_dealias() {
@ -154,20 +154,20 @@ pub fn constrain_pattern(
));
}
IntLiteral(_) => {
IntLiteral(var, _) => {
state.constraints.push(Constraint::Pattern(
region,
PatternCategory::Int,
builtins::num_int(),
builtins::num_int(Type::Variable(*var)),
expected,
));
}
FloatLiteral(_) => {
FloatLiteral(var, _) => {
state.constraints.push(Constraint::Pattern(
region,
PatternCategory::Float,
builtins::num_float(),
builtins::num_float(Type::Variable(*var)),
expected,
));
}

View file

@ -1,4 +1,4 @@
use crate::builtins::{num_binary64, num_floatingpoint, num_integer, num_num, num_signed64};
use crate::builtins::{num_floatingpoint, num_integer, num_num};
use crate::expr::{exists, Info};
use roc_can::annotation::IntroducedVariables;
use roc_can::constraint::Constraint::{self, *};
@ -173,18 +173,18 @@ fn constrain_pattern(
));
}
IntLiteral(_) => {
let (a, b, c, num_type) = unique_int(var_store);
IntLiteral(inner_var, _) => {
let (a, b, c, num_type) = unique_int(*inner_var, var_store);
state.constraints.push(exists(
vec![a, b, c],
vec![a, b, c, *inner_var],
Constraint::Pattern(pattern.region, PatternCategory::Int, num_type, expected),
));
}
FloatLiteral(_) => {
let (a, b, c, num_type) = unique_float(var_store);
FloatLiteral(inner_var, _) => {
let (a, b, c, num_type) = unique_float(*inner_var, var_store);
state.constraints.push(exists(
vec![a, b, c],
vec![a, b, c, *inner_var],
Constraint::Pattern(pattern.region, PatternCategory::Float, num_type, expected),
));
}
@ -419,14 +419,17 @@ fn unique_unbound_num(inner_var: Variable, var_store: &mut VarStore) -> (Variabl
(inner_uvar, num_uvar, num_type)
}
fn unique_int(var_store: &mut VarStore) -> (Variable, Variable, Variable, Type) {
fn unique_int(
inner_var: Variable,
var_store: &mut VarStore,
) -> (Variable, Variable, Variable, Type) {
let num_uvar1 = var_store.fresh();
let num_uvar2 = var_store.fresh();
let num_uvar3 = var_store.fresh();
let signed_64 = num_signed64();
let attr_signed_64 = attr_type(Bool::variable(num_uvar1), signed_64);
let integer = num_integer(attr_signed_64);
let inner_type = Type::Variable(inner_var);
let attr_inner_type = attr_type(Bool::variable(num_uvar1), inner_type);
let integer = num_integer(attr_inner_type);
let attr_int = attr_type(Bool::variable(num_uvar2), integer);
let num = num_num(attr_int);
let attr_num = attr_type(Bool::variable(num_uvar3), num);
@ -434,14 +437,17 @@ fn unique_int(var_store: &mut VarStore) -> (Variable, Variable, Variable, Type)
(num_uvar1, num_uvar2, num_uvar3, attr_num)
}
fn unique_float(var_store: &mut VarStore) -> (Variable, Variable, Variable, Type) {
fn unique_float(
inner_var: Variable,
var_store: &mut VarStore,
) -> (Variable, Variable, Variable, Type) {
let num_uvar1 = var_store.fresh();
let num_uvar2 = var_store.fresh();
let num_uvar3 = var_store.fresh();
let binary_64 = num_binary64();
let attr_binary_64 = attr_type(Bool::variable(num_uvar1), binary_64);
let fp = num_floatingpoint(attr_binary_64);
let inner_type = Type::Variable(inner_var);
let attr_inner_type = attr_type(Bool::variable(num_uvar1), inner_type);
let fp = num_floatingpoint(attr_inner_type);
let attr_fp = attr_type(Bool::variable(num_uvar2), fp);
let num = num_num(attr_fp);
let attr_num = attr_type(Bool::variable(num_uvar3), num);
@ -479,7 +485,7 @@ pub fn constrain_expr(
)
}
Int(var, _) => {
let (a, b, c, num_type) = unique_int(var_store);
let (a, b, c, num_type) = unique_int(*var, var_store);
exists(
vec![*var, a, b, c],
@ -495,7 +501,7 @@ pub fn constrain_expr(
)
}
Float(var, _) => {
let (a, b, c, num_type) = unique_float(var_store);
let (a, b, c, num_type) = unique_float(*var, var_store);
exists(
vec![*var, a, b, c],
@ -532,7 +538,9 @@ pub fn constrain_expr(
),
)
}
Record { record_var, fields } => {
Record {
record_var, fields, ..
} => {
// NOTE: canonicalization guarantees at least one field
// zero fields generates an EmptyRecord
let mut field_types = SendMap::default();

View file

@ -1380,7 +1380,7 @@ fn pattern_to_when<'a>(
(symbol, Located::at_zero(wrapped_body))
}
IntLiteral(_) | NumLiteral(_, _) | FloatLiteral(_) | StrLiteral(_) => {
IntLiteral(_, _) | NumLiteral(_, _) | FloatLiteral(_, _) | StrLiteral(_) => {
// These patters are refutable, and thus should never occur outside a `when` expression
// They should have been replaced with `UnsupportedPattern` during canonicalization
unreachable!("refutable pattern {:?} where irrefutable pattern is expected. This should never happen!", pattern.value)
@ -5468,8 +5468,8 @@ pub fn from_can_pattern<'a>(
match can_pattern {
Underscore => Pattern::Underscore,
Identifier(symbol) => Pattern::Identifier(*symbol),
IntLiteral(v) => Pattern::IntLiteral(*v),
FloatLiteral(v) => Pattern::FloatLiteral(f64::to_bits(*v)),
IntLiteral(_, v) => Pattern::IntLiteral(*v),
FloatLiteral(_, v) => Pattern::FloatLiteral(f64::to_bits(*v)),
StrLiteral(v) => Pattern::StrLiteral(v.clone()),
Shadowed(region, ident) => Pattern::Shadowed(*region, ident.clone()),
UnsupportedPattern(region) => Pattern::UnsupportedPattern(*region),

View file

@ -362,12 +362,12 @@ impl<'a> Layout<'a> {
}
Structure(flat_type) => layout_from_flat_type(env, flat_type),
Alias(Symbol::NUM_I64, args, _) => {
debug_assert!(args.is_empty());
Alias(Symbol::NUM_INT, args, _) => {
debug_assert!(args.len() == 1);
Ok(Layout::Builtin(Builtin::Int64))
}
Alias(Symbol::NUM_F64, args, _) => {
debug_assert!(args.is_empty());
Alias(Symbol::NUM_FLOAT, args, _) => {
debug_assert!(args.len() == 1);
Ok(Layout::Builtin(Builtin::Float64))
}
Alias(_, _, var) => Self::from_var(env, var),
@ -763,11 +763,11 @@ fn layout_from_flat_type<'a>(
match flat_type {
Apply(symbol, args) => {
match symbol {
Symbol::NUM_I64 => {
Symbol::NUM_INT => {
debug_assert_eq!(args.len(), 0);
Ok(Layout::Builtin(Builtin::Int64))
}
Symbol::NUM_F64 => {
Symbol::NUM_FLOAT => {
debug_assert_eq!(args.len(), 0);
Ok(Layout::Builtin(Builtin::Float64))
}

View file

@ -1624,8 +1624,8 @@ fn to_diff<'b>(
let right = to_doc(alloc, Parens::Unnecessary, type2);
let is_int = |t: &ErrorType| match t {
ErrorType::Type(Symbol::NUM_I64, _) => true,
ErrorType::Alias(Symbol::NUM_I64, _, _) => true,
ErrorType::Type(Symbol::NUM_INT, _) => true,
ErrorType::Alias(Symbol::NUM_INT, _, _) => true,
ErrorType::Type(Symbol::NUM_NUM, args) => match &args.get(0) {
Some(ErrorType::Type(Symbol::NUM_INTEGER, _)) => true,
@ -1640,8 +1640,8 @@ fn to_diff<'b>(
_ => false,
};
let is_float = |t: &ErrorType| match t {
ErrorType::Type(Symbol::NUM_F64, _) => true,
ErrorType::Alias(Symbol::NUM_F64, _, _) => true,
ErrorType::Type(Symbol::NUM_FLOAT, _) => true,
ErrorType::Alias(Symbol::NUM_FLOAT, _, _) => true,
ErrorType::Type(Symbol::NUM_NUM, args) => match &args.get(0) {
Some(ErrorType::Type(Symbol::NUM_FLOATINGPOINT, _)) => true,

View file

@ -14,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 int_type = builtin_type(Symbol::NUM_I64, vec![]);
let int_type = builtin_type(Symbol::NUM_INT, vec![]);
let expected_literal = ForReason(reason, int_type, region);
exists(
@ -30,7 +30,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 float_type = builtin_type(Symbol::NUM_F64, vec![]);
let float_type = builtin_type(Symbol::NUM_FLOAT, vec![]);
let expected_literal = ForReason(reason, float_type, region);
exists(

View file

@ -752,9 +752,9 @@ fn annotate_usage_pattern(pattern: &Pattern, usage: &mut VarUsage) {
match pattern {
Identifier(_)
| IntLiteral(_)
| IntLiteral(_, _)
| NumLiteral(_, _)
| FloatLiteral(_)
| FloatLiteral(_, _)
| StrLiteral(_)
| Underscore
| Shadowed(_, _)