add some aliases to file.rs

This commit is contained in:
Folkert 2022-03-16 18:18:45 +01:00
parent 7f63e09272
commit f24bfeea99
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C

View File

@ -14,7 +14,7 @@ use roc_constrain::module::{
ExposedModuleTypes,
};
use roc_error_macros::internal_error;
use roc_module::ident::{Ident, ModuleName, QualifiedModuleName};
use roc_module::ident::{Ident, ModuleName, QualifiedModuleName, TagName};
use roc_module::symbol::{
IdentIds, Interns, ModuleId, ModuleIds, PQModuleName, PackageModuleIds, PackageQualified,
Symbol,
@ -36,7 +36,7 @@ use roc_solve::solve;
use roc_target::TargetInfo;
use roc_types::solved_types::Solved;
use roc_types::subs::{Subs, VarStore, Variable};
use roc_types::types::Alias;
use roc_types::types::{Alias, AliasCommon};
use std::collections::hash_map::Entry::{Occupied, Vacant};
use std::collections::{HashMap, HashSet};
use std::io;
@ -4097,3 +4097,210 @@ fn to_missing_platform_report(module_id: ModuleId, other: PlatformPath) -> Strin
buf
}
/// Builtin aliases that are not covered by type checker optimizations
///
/// Types like `F64` and `I32` are hardcoded into Subs and therefore we don't define them here.
/// All that remains are the generic number types (Num, Int, Float) and Result
fn default_aliases() -> roc_solve::solve::Aliases {
use roc_types::types::Type;
let mut solve_aliases = roc_solve::solve::Aliases::default();
let mut var_store = VarStore::default();
{
let symbol = Symbol::NUM_NUM;
let tvar = var_store.fresh();
let typ = Type::TagUnion(
vec![(
TagName::Private(Symbol::NUM_AT_NUM),
vec![Type::Variable(tvar)],
)],
Box::new(Type::EmptyTagUnion),
);
let alias = Alias {
region: Region::zero(),
type_variables: vec![Loc::at_zero(("range".into(), tvar))],
lambda_set_variables: Default::default(),
recursion_variables: Default::default(),
typ,
kind: roc_types::types::AliasKind::Structural,
};
solve_aliases.insert(symbol, alias);
}
// FloatingPoint range : [ @FloatingPoint range ]
{
let symbol = Symbol::NUM_FLOATINGPOINT;
let tvar = var_store.fresh();
let typ = Type::TagUnion(
vec![(
TagName::Private(Symbol::NUM_AT_FLOATINGPOINT),
vec![Type::Variable(tvar)],
)],
Box::new(Type::EmptyTagUnion),
);
let alias = Alias {
region: Region::zero(),
type_variables: vec![Loc::at_zero(("range".into(), tvar))],
lambda_set_variables: Default::default(),
recursion_variables: Default::default(),
typ,
kind: roc_types::types::AliasKind::Structural,
};
solve_aliases.insert(symbol, alias);
}
// Int range : Num (Integer range)
{
let symbol = Symbol::NUM_INT;
let tvar = var_store.fresh();
let typ = Type::DelayedAlias(AliasCommon {
symbol: Symbol::NUM_NUM,
type_arguments: vec![(
"range".into(),
Type::DelayedAlias(AliasCommon {
symbol: Symbol::NUM_INTEGER,
type_arguments: vec![("range".into(), Type::Variable(tvar))],
lambda_set_variables: vec![],
}),
)],
lambda_set_variables: vec![],
});
let alias = Alias {
region: Region::zero(),
type_variables: vec![Loc::at_zero(("range".into(), tvar))],
lambda_set_variables: Default::default(),
recursion_variables: Default::default(),
typ,
kind: roc_types::types::AliasKind::Structural,
};
solve_aliases.insert(symbol, alias);
}
{
let symbol = Symbol::NUM_FLOAT;
let tvar = var_store.fresh();
let typ = Type::DelayedAlias(AliasCommon {
symbol: Symbol::NUM_NUM,
type_arguments: vec![(
"range".into(),
Type::DelayedAlias(AliasCommon {
symbol: Symbol::NUM_FLOATINGPOINT,
type_arguments: vec![("range".into(), Type::Variable(tvar))],
lambda_set_variables: vec![],
}),
)],
lambda_set_variables: vec![],
});
let alias = Alias {
region: Region::zero(),
type_variables: vec![Loc::at_zero(("range".into(), tvar))],
lambda_set_variables: Default::default(),
recursion_variables: Default::default(),
typ,
kind: roc_types::types::AliasKind::Structural,
};
solve_aliases.insert(symbol, alias);
}
{
let symbol = Symbol::NUM_INTEGER;
let tvar = var_store.fresh();
let typ = Type::TagUnion(
vec![(
TagName::Private(Symbol::NUM_AT_INTEGER),
vec![Type::Variable(tvar)],
)],
Box::new(Type::EmptyTagUnion),
);
let alias = Alias {
region: Region::zero(),
type_variables: vec![Loc::at_zero(("range".into(), tvar))],
lambda_set_variables: Default::default(),
recursion_variables: Default::default(),
typ,
kind: roc_types::types::AliasKind::Structural,
};
solve_aliases.insert(symbol, alias);
}
{
let symbol = Symbol::RESULT_RESULT;
let tvar1 = var_store.fresh();
let tvar2 = var_store.fresh();
let typ = Type::TagUnion(
vec![
(TagName::Global("Ok".into()), vec![Type::Variable(tvar1)]),
(TagName::Global("Err".into()), vec![Type::Variable(tvar2)]),
],
Box::new(Type::EmptyTagUnion),
);
let alias = Alias {
region: Region::zero(),
type_variables: vec![
Loc::at_zero(("ok".into(), tvar1)),
Loc::at_zero(("err".into(), tvar2)),
],
lambda_set_variables: Default::default(),
recursion_variables: Default::default(),
typ,
kind: roc_types::types::AliasKind::Structural,
};
solve_aliases.insert(symbol, alias);
}
let mut unit_function = |alias_name: Symbol, at_tag_name: Symbol| {
let typ = Type::TagUnion(
vec![(TagName::Private(at_tag_name), vec![])],
Box::new(Type::EmptyTagUnion),
);
let alias = Alias {
region: Region::zero(),
type_variables: vec![],
lambda_set_variables: Default::default(),
recursion_variables: Default::default(),
typ,
kind: roc_types::types::AliasKind::Structural,
};
solve_aliases.insert(alias_name, alias);
};
unit_function(Symbol::NUM_SIGNED8, Symbol::NUM_AT_SIGNED8);
unit_function(Symbol::NUM_SIGNED16, Symbol::NUM_AT_SIGNED16);
unit_function(Symbol::NUM_SIGNED32, Symbol::NUM_AT_SIGNED32);
unit_function(Symbol::NUM_SIGNED64, Symbol::NUM_AT_SIGNED64);
unit_function(Symbol::NUM_SIGNED128, Symbol::NUM_AT_SIGNED128);
unit_function(Symbol::NUM_UNSIGNED8, Symbol::NUM_AT_UNSIGNED8);
unit_function(Symbol::NUM_UNSIGNED16, Symbol::NUM_AT_UNSIGNED16);
unit_function(Symbol::NUM_UNSIGNED32, Symbol::NUM_AT_UNSIGNED32);
unit_function(Symbol::NUM_UNSIGNED64, Symbol::NUM_AT_UNSIGNED64);
unit_function(Symbol::NUM_UNSIGNED128, Symbol::NUM_AT_UNSIGNED128);
unit_function(Symbol::NUM_BINARY32, Symbol::NUM_AT_BINARY32);
unit_function(Symbol::NUM_BINARY64, Symbol::NUM_AT_BINARY64);
solve_aliases
}