mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
fix some uniqueness issues
This commit is contained in:
parent
22319aa116
commit
cba841601b
5 changed files with 45 additions and 157 deletions
|
@ -9,7 +9,7 @@ use roc_can::operator;
|
||||||
use roc_can::scope::Scope;
|
use roc_can::scope::Scope;
|
||||||
use roc_collections::all::{ImMap, ImSet, MutMap, MutSet, SendMap, SendSet};
|
use roc_collections::all::{ImMap, ImSet, MutMap, MutSet, SendMap, SendSet};
|
||||||
use roc_constrain::expr::constrain_expr;
|
use roc_constrain::expr::constrain_expr;
|
||||||
use roc_constrain::module::{constrain_imported_values, load_builtin_aliases, Import};
|
use roc_constrain::module::{constrain_imported_values, Import};
|
||||||
use roc_fmt::annotation::{Formattable, Newlines, Parens};
|
use roc_fmt::annotation::{Formattable, Newlines, Parens};
|
||||||
use roc_gen::llvm::build::{build_proc, build_proc_header, OptLevel};
|
use roc_gen::llvm::build::{build_proc, build_proc_header, OptLevel};
|
||||||
use roc_module::ident::Ident;
|
use roc_module::ident::Ident;
|
||||||
|
@ -524,11 +524,6 @@ pub fn uniq_expr_with(
|
||||||
let (_introduced_rigids, constraint) =
|
let (_introduced_rigids, constraint) =
|
||||||
constrain_imported_values(imports, constraint, &mut var_store);
|
constrain_imported_values(imports, constraint, &mut var_store);
|
||||||
|
|
||||||
// load builtin types
|
|
||||||
let mut constraint = load_builtin_aliases(stdlib.aliases, constraint, &mut var_store);
|
|
||||||
|
|
||||||
constraint.instantiate_aliases(&mut var_store);
|
|
||||||
|
|
||||||
let subs2 = Subs::new(var_store.into());
|
let subs2 = Subs::new(var_store.into());
|
||||||
|
|
||||||
Ok((
|
Ok((
|
||||||
|
|
|
@ -20,7 +20,6 @@ pub enum Mode {
|
||||||
pub struct StdLib {
|
pub struct StdLib {
|
||||||
pub mode: Mode,
|
pub mode: Mode,
|
||||||
pub types: MutMap<Symbol, (SolvedType, Region)>,
|
pub types: MutMap<Symbol, (SolvedType, Region)>,
|
||||||
pub aliases: MutMap<Symbol, BuiltinAlias>,
|
|
||||||
pub applies: MutSet<Symbol>,
|
pub applies: MutSet<Symbol>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +27,6 @@ pub fn standard_stdlib() -> StdLib {
|
||||||
StdLib {
|
StdLib {
|
||||||
mode: Mode::Standard,
|
mode: Mode::Standard,
|
||||||
types: types(),
|
types: types(),
|
||||||
aliases: roc_types::builtin_aliases::aliases(),
|
|
||||||
applies: vec![
|
applies: vec![
|
||||||
Symbol::LIST_LIST,
|
Symbol::LIST_LIST,
|
||||||
Symbol::SET_SET,
|
Symbol::SET_SET,
|
||||||
|
|
|
@ -3,7 +3,8 @@ use roc_collections::all::{default_hasher, MutMap};
|
||||||
use roc_module::ident::TagName;
|
use roc_module::ident::TagName;
|
||||||
use roc_module::symbol::Symbol;
|
use roc_module::symbol::Symbol;
|
||||||
use roc_region::all::{Located, Region};
|
use roc_region::all::{Located, Region};
|
||||||
use roc_types::solved_types::{BuiltinAlias, SolvedBool, SolvedType};
|
use roc_types::builtin_aliases;
|
||||||
|
use roc_types::solved_types::{SolvedBool, SolvedType};
|
||||||
use roc_types::subs::VarId;
|
use roc_types::subs::VarId;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
@ -58,7 +59,6 @@ pub fn uniq_stdlib() -> StdLib {
|
||||||
use crate::std::Mode;
|
use crate::std::Mode;
|
||||||
|
|
||||||
let types = types();
|
let types = types();
|
||||||
let aliases = aliases();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
debug_assert!({
|
debug_assert!({
|
||||||
|
@ -103,7 +103,6 @@ pub fn uniq_stdlib() -> StdLib {
|
||||||
StdLib {
|
StdLib {
|
||||||
mode: Mode::Uniqueness,
|
mode: Mode::Uniqueness,
|
||||||
types,
|
types,
|
||||||
aliases,
|
|
||||||
applies: vec![
|
applies: vec![
|
||||||
Symbol::ATTR_ATTR,
|
Symbol::ATTR_ATTR,
|
||||||
Symbol::LIST_LIST,
|
Symbol::LIST_LIST,
|
||||||
|
@ -116,132 +115,6 @@ pub fn uniq_stdlib() -> StdLib {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn aliases() -> MutMap<Symbol, BuiltinAlias> {
|
|
||||||
let mut aliases = MutMap::default();
|
|
||||||
|
|
||||||
let mut add_alias = |symbol, alias| {
|
|
||||||
debug_assert!(
|
|
||||||
!aliases.contains_key(&symbol),
|
|
||||||
"Duplicate alias definition for {:?}",
|
|
||||||
symbol
|
|
||||||
);
|
|
||||||
|
|
||||||
// TODO instead of using Region::zero for all of these,
|
|
||||||
// instead use the Region where they were defined in their
|
|
||||||
// source .roc files! This can give nicer error messages.
|
|
||||||
aliases.insert(symbol, alias);
|
|
||||||
};
|
|
||||||
|
|
||||||
let single_private_tag = |symbol, targs| {
|
|
||||||
SolvedType::TagUnion(
|
|
||||||
vec![(TagName::Private(symbol), targs)],
|
|
||||||
Box::new(SolvedType::EmptyTagUnion),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
// NOTE: `a` must be the first variable bound here!
|
|
||||||
let_tvars! { a, err, star };
|
|
||||||
|
|
||||||
// Num : Num Integer
|
|
||||||
add_alias(
|
|
||||||
Symbol::NUM_NUM,
|
|
||||||
BuiltinAlias {
|
|
||||||
region: Region::zero(),
|
|
||||||
vars: vec![Located::at(Region::zero(), "a".into())],
|
|
||||||
typ: single_private_tag(Symbol::NUM_AT_NUM, vec![flex(a)]),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Integer : [ @Integer ]
|
|
||||||
add_alias(
|
|
||||||
Symbol::NUM_INTEGER,
|
|
||||||
BuiltinAlias {
|
|
||||||
region: Region::zero(),
|
|
||||||
vars: Vec::new(),
|
|
||||||
typ: single_private_tag(Symbol::NUM_AT_INTEGER, Vec::new()),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// FloatingPoint : [ @FloatingPoint ]
|
|
||||||
add_alias(
|
|
||||||
Symbol::NUM_FLOATINGPOINT,
|
|
||||||
BuiltinAlias {
|
|
||||||
region: Region::zero(),
|
|
||||||
vars: Vec::new(),
|
|
||||||
typ: single_private_tag(Symbol::NUM_AT_FLOATINGPOINT, Vec::new()),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Int : Num Integer
|
|
||||||
add_alias(
|
|
||||||
Symbol::NUM_INT,
|
|
||||||
BuiltinAlias {
|
|
||||||
region: Region::zero(),
|
|
||||||
vars: Vec::new(),
|
|
||||||
typ: SolvedType::Apply(
|
|
||||||
Symbol::NUM_NUM,
|
|
||||||
vec![lift(
|
|
||||||
star,
|
|
||||||
SolvedType::Apply(Symbol::NUM_INTEGER, Vec::new()),
|
|
||||||
)],
|
|
||||||
),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Float : Num FloatingPoint
|
|
||||||
add_alias(
|
|
||||||
Symbol::NUM_FLOAT,
|
|
||||||
BuiltinAlias {
|
|
||||||
region: Region::zero(),
|
|
||||||
vars: Vec::new(),
|
|
||||||
typ: SolvedType::Apply(
|
|
||||||
Symbol::NUM_NUM,
|
|
||||||
vec![lift(
|
|
||||||
star,
|
|
||||||
SolvedType::Apply(Symbol::NUM_FLOATINGPOINT, Vec::new()),
|
|
||||||
)],
|
|
||||||
),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Bool : [ True, False ]
|
|
||||||
add_alias(
|
|
||||||
Symbol::BOOL_BOOL,
|
|
||||||
BuiltinAlias {
|
|
||||||
region: Region::zero(),
|
|
||||||
vars: Vec::new(),
|
|
||||||
typ: SolvedType::TagUnion(
|
|
||||||
vec![
|
|
||||||
(TagName::Global("True".into()), Vec::new()),
|
|
||||||
(TagName::Global("False".into()), Vec::new()),
|
|
||||||
],
|
|
||||||
Box::new(SolvedType::EmptyTagUnion),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Result a e : [ Ok a, Err e ]
|
|
||||||
add_alias(
|
|
||||||
Symbol::RESULT_RESULT,
|
|
||||||
BuiltinAlias {
|
|
||||||
region: Region::zero(),
|
|
||||||
vars: vec![
|
|
||||||
Located::at(Region::zero(), "a".into()),
|
|
||||||
Located::at(Region::zero(), "e".into()),
|
|
||||||
],
|
|
||||||
typ: SolvedType::TagUnion(
|
|
||||||
vec![
|
|
||||||
(TagName::Global("Ok".into()), vec![flex(a)]),
|
|
||||||
(TagName::Global("Err".into()), vec![flex(err)]),
|
|
||||||
],
|
|
||||||
Box::new(SolvedType::EmptyTagUnion),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
aliases
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||||
let mut types = HashMap::with_capacity_and_hasher(NUM_BUILTIN_IMPORTS, default_hasher());
|
let mut types = HashMap::with_capacity_and_hasher(NUM_BUILTIN_IMPORTS, default_hasher());
|
||||||
|
|
||||||
|
@ -1206,7 +1079,17 @@ fn lift(u: VarId, a: SolvedType) -> SolvedType {
|
||||||
fn float_type(u: VarId) -> SolvedType {
|
fn float_type(u: VarId) -> SolvedType {
|
||||||
SolvedType::Apply(
|
SolvedType::Apply(
|
||||||
Symbol::ATTR_ATTR,
|
Symbol::ATTR_ATTR,
|
||||||
vec![flex(u), SolvedType::Apply(Symbol::NUM_FLOAT, Vec::new())],
|
vec![
|
||||||
|
flex(u),
|
||||||
|
SolvedType::Alias(
|
||||||
|
Symbol::NUM_FLOAT,
|
||||||
|
Vec::new(),
|
||||||
|
Box::new(builtin_aliases::num_type(SolvedType::Apply(
|
||||||
|
Symbol::ATTR_ATTR,
|
||||||
|
vec![flex(u), builtin_aliases::floatingpoint_type()],
|
||||||
|
))),
|
||||||
|
),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1214,7 +1097,17 @@ fn float_type(u: VarId) -> SolvedType {
|
||||||
fn int_type(u: VarId) -> SolvedType {
|
fn int_type(u: VarId) -> SolvedType {
|
||||||
SolvedType::Apply(
|
SolvedType::Apply(
|
||||||
Symbol::ATTR_ATTR,
|
Symbol::ATTR_ATTR,
|
||||||
vec![flex(u), SolvedType::Apply(Symbol::NUM_INT, Vec::new())],
|
vec![
|
||||||
|
flex(u),
|
||||||
|
SolvedType::Alias(
|
||||||
|
Symbol::NUM_INT,
|
||||||
|
Vec::new(),
|
||||||
|
Box::new(builtin_aliases::num_type(SolvedType::Apply(
|
||||||
|
Symbol::ATTR_ATTR,
|
||||||
|
vec![flex(u), builtin_aliases::integer_type()],
|
||||||
|
))),
|
||||||
|
),
|
||||||
|
],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1222,7 +1115,7 @@ fn int_type(u: VarId) -> SolvedType {
|
||||||
fn bool_type(u: VarId) -> SolvedType {
|
fn bool_type(u: VarId) -> SolvedType {
|
||||||
SolvedType::Apply(
|
SolvedType::Apply(
|
||||||
Symbol::ATTR_ATTR,
|
Symbol::ATTR_ATTR,
|
||||||
vec![flex(u), SolvedType::Apply(Symbol::BOOL_BOOL, Vec::new())],
|
vec![flex(u), builtin_aliases::bool_type()],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1238,10 +1131,15 @@ fn str_type(u: VarId) -> SolvedType {
|
||||||
fn num_type(u: VarId, a: VarId) -> SolvedType {
|
fn num_type(u: VarId, a: VarId) -> SolvedType {
|
||||||
SolvedType::Apply(
|
SolvedType::Apply(
|
||||||
Symbol::ATTR_ATTR,
|
Symbol::ATTR_ATTR,
|
||||||
vec![
|
vec![flex(u), builtin_aliases::num_type(attr_type(u, a))],
|
||||||
flex(u),
|
)
|
||||||
SolvedType::Apply(Symbol::NUM_NUM, vec![attr_type(u, a)]),
|
}
|
||||||
],
|
|
||||||
|
#[inline(always)]
|
||||||
|
fn num_type_help(u: VarId, a: SolvedType) -> SolvedType {
|
||||||
|
SolvedType::Apply(
|
||||||
|
Symbol::ATTR_ATTR,
|
||||||
|
vec![flex(u), builtin_aliases::num_type(a)],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1249,10 +1147,7 @@ fn num_type(u: VarId, a: VarId) -> SolvedType {
|
||||||
fn result_type(u: VarId, a: SolvedType, e: SolvedType) -> SolvedType {
|
fn result_type(u: VarId, a: SolvedType, e: SolvedType) -> SolvedType {
|
||||||
SolvedType::Apply(
|
SolvedType::Apply(
|
||||||
Symbol::ATTR_ATTR,
|
Symbol::ATTR_ATTR,
|
||||||
vec![
|
vec![flex(u), builtin_aliases::result_type(a, e)],
|
||||||
flex(u),
|
|
||||||
SolvedType::Apply(Symbol::RESULT_RESULT, vec![a, e]),
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -301,12 +301,12 @@ pub fn pre_constrain_imports(
|
||||||
None => {
|
None => {
|
||||||
let is_valid_alias = stdlib.applies.contains(&symbol)
|
let is_valid_alias = stdlib.applies.contains(&symbol)
|
||||||
// This wasn't a builtin value or Apply; maybe it was a builtin alias.
|
// This wasn't a builtin value or Apply; maybe it was a builtin alias.
|
||||||
|| stdlib.aliases.contains_key(&symbol);
|
|| roc_types::builtin_aliases::aliases().contains_key(&symbol);
|
||||||
|
|
||||||
if !is_valid_alias {
|
if !is_valid_alias {
|
||||||
panic!(
|
panic!(
|
||||||
"Could not find {:?} in builtin types {:?} or aliases {:?}",
|
"Could not find {:?} in builtin types {:?} or builtin aliases",
|
||||||
symbol, stdlib.types, stdlib.aliases
|
symbol, stdlib.types,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use crate::builtins::{num_floatingpoint, num_integer, num_num};
|
||||||
use crate::expr::{exists, exists_with_aliases, Info};
|
use crate::expr::{exists, exists_with_aliases, Info};
|
||||||
use roc_can::annotation::IntroducedVariables;
|
use roc_can::annotation::IntroducedVariables;
|
||||||
use roc_can::constraint::Constraint::{self, *};
|
use roc_can::constraint::Constraint::{self, *};
|
||||||
|
@ -420,31 +421,30 @@ fn unique_unbound_num(
|
||||||
let val_type = Type::Variable(inner_var);
|
let val_type = Type::Variable(inner_var);
|
||||||
let val_utype = attr_type(Bool::variable(val_uvar), val_type);
|
let val_utype = attr_type(Bool::variable(val_uvar), val_type);
|
||||||
|
|
||||||
let num_utype = Type::Apply(Symbol::NUM_NUM, vec![val_utype]);
|
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(num_uvar), num_utype);
|
||||||
|
|
||||||
(num_uvar, val_uvar, num_type, num_var)
|
(num_uvar, val_uvar, num_type, num_var)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unique_num(var_store: &mut VarStore, symbol: Symbol) -> (Variable, Variable, Type) {
|
fn unique_num(var_store: &mut VarStore, val_type: Type) -> (Variable, Variable, Type) {
|
||||||
let num_uvar = var_store.fresh();
|
let num_uvar = var_store.fresh();
|
||||||
let val_uvar = var_store.fresh();
|
let val_uvar = var_store.fresh();
|
||||||
|
|
||||||
let val_type = Type::Apply(symbol, Vec::new());
|
|
||||||
let val_utype = attr_type(Bool::variable(val_uvar), val_type);
|
let val_utype = attr_type(Bool::variable(val_uvar), val_type);
|
||||||
|
|
||||||
let num_utype = Type::Apply(Symbol::NUM_NUM, vec![val_utype]);
|
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(num_uvar), num_utype);
|
||||||
|
|
||||||
(num_uvar, val_uvar, num_type)
|
(num_uvar, val_uvar, num_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unique_int(var_store: &mut VarStore) -> (Variable, Variable, Type) {
|
fn unique_int(var_store: &mut VarStore) -> (Variable, Variable, Type) {
|
||||||
unique_num(var_store, Symbol::NUM_INTEGER)
|
unique_num(var_store, num_integer())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn unique_float(var_store: &mut VarStore) -> (Variable, Variable, Type) {
|
fn unique_float(var_store: &mut VarStore) -> (Variable, Variable, Type) {
|
||||||
unique_num(var_store, Symbol::NUM_FLOATINGPOINT)
|
unique_num(var_store, num_floatingpoint())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn constrain_expr(
|
pub fn constrain_expr(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue