mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 05:34:11 +03:00
Merge pull request #211 from rtfeldman/unique-builtins-implementations
Unique builtins implementations
This commit is contained in:
commit
d4d6ca2a4a
@ -336,6 +336,18 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
SolvedType::Func(vec![bool_type(), bool_type()], Box::new(bool_type())),
|
||||
);
|
||||
|
||||
// xor : Bool, Bool -> Bool
|
||||
add_type(
|
||||
Symbol::BOOL_XOR,
|
||||
SolvedType::Func(vec![bool_type(), bool_type()], Box::new(bool_type())),
|
||||
);
|
||||
|
||||
// not : Bool -> Bool
|
||||
add_type(
|
||||
Symbol::BOOL_NOT,
|
||||
SolvedType::Func(vec![bool_type()], Box::new(bool_type())),
|
||||
);
|
||||
|
||||
// Str module
|
||||
|
||||
// isEmpty : Str -> Bool
|
||||
@ -401,6 +413,34 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
),
|
||||
);
|
||||
|
||||
// foldr : List a, (a -> b -> b), b -> b
|
||||
add_type(
|
||||
Symbol::LIST_FOLDR,
|
||||
SolvedType::Func(
|
||||
vec![
|
||||
list_type(flex(TVAR1)),
|
||||
SolvedType::Func(vec![flex(TVAR1), flex(TVAR2)], Box::new(flex(TVAR2))),
|
||||
flex(TVAR2),
|
||||
],
|
||||
Box::new(flex(TVAR2)),
|
||||
),
|
||||
);
|
||||
|
||||
// push : List a -> a -> List a
|
||||
add_type(
|
||||
Symbol::LIST_PUSH,
|
||||
SolvedType::Func(
|
||||
vec![list_type(flex(TVAR1))],
|
||||
Box::new(list_type(flex(TVAR1))),
|
||||
),
|
||||
);
|
||||
|
||||
// length : List a -> Int
|
||||
add_type(
|
||||
Symbol::LIST_LENGTH,
|
||||
SolvedType::Func(vec![list_type(flex(TVAR1))], Box::new(int_type())),
|
||||
);
|
||||
|
||||
// Result module
|
||||
|
||||
// map : Result a err, (a -> b) -> Result b err
|
||||
|
@ -5,10 +5,11 @@ use crate::collections::{ImMap, MutMap, SendMap};
|
||||
use crate::constrain::expr::constrain_decls;
|
||||
use crate::module::symbol::{ModuleId, Symbol};
|
||||
use crate::region::Located;
|
||||
use crate::solve::{BuiltinAlias, SolvedType};
|
||||
use crate::solve::{BuiltinAlias, SolvedAtom, SolvedType};
|
||||
use crate::subs::{VarId, VarStore, Variable};
|
||||
use crate::types::{Alias, Constraint, LetConstraint, Type};
|
||||
use crate::uniqueness;
|
||||
use crate::uniqueness::boolean_algebra::{Atom, Bool};
|
||||
|
||||
#[inline(always)]
|
||||
pub fn constrain_module(
|
||||
@ -239,7 +240,16 @@ pub fn to_type(solved_type: &SolvedType, free_vars: &mut FreeVars, var_store: &V
|
||||
Box::new(to_type(ext, free_vars, var_store)),
|
||||
)
|
||||
}
|
||||
Boolean(val) => Type::Boolean(val.clone()),
|
||||
Boolean(solved_free, solved_rest) => {
|
||||
let free = to_atom(solved_free, free_vars, var_store);
|
||||
let mut rest = Vec::with_capacity(solved_rest.len());
|
||||
|
||||
for solved_atom in solved_rest {
|
||||
rest.push(to_atom(solved_atom, free_vars, var_store));
|
||||
}
|
||||
|
||||
Type::Boolean(Bool::from_parts(free, rest))
|
||||
}
|
||||
Alias(symbol, solved_type_variables, solved_actual) => {
|
||||
let mut type_variables = Vec::with_capacity(solved_type_variables.len());
|
||||
|
||||
@ -258,6 +268,23 @@ pub fn to_type(solved_type: &SolvedType, free_vars: &mut FreeVars, var_store: &V
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_atom(solved_atom: &SolvedAtom, free_vars: &mut FreeVars, var_store: &VarStore) -> Atom {
|
||||
match solved_atom {
|
||||
SolvedAtom::Zero => Atom::Zero,
|
||||
SolvedAtom::One => Atom::One,
|
||||
SolvedAtom::Variable(var_id) => {
|
||||
if let Some(var) = free_vars.unnamed_vars.get(&var_id) {
|
||||
Atom::Variable(*var)
|
||||
} else {
|
||||
let var = var_store.fresh();
|
||||
free_vars.unnamed_vars.insert(*var_id, var);
|
||||
|
||||
Atom::Variable(var)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn constrain_imported_aliases(
|
||||
aliases: MutMap<Symbol, Alias>,
|
||||
body_con: Constraint,
|
||||
|
@ -605,8 +605,12 @@ define_builtins! {
|
||||
2 LIST_ISEMPTY: "isEmpty"
|
||||
3 LIST_GET: "get"
|
||||
4 LIST_SET: "set"
|
||||
5 LIST_MAP: "map"
|
||||
6 LIST_GET_UNSAFE: "getUnsafe" // TODO remove once we can code gen Result
|
||||
5 LIST_PUSH: "push"
|
||||
6 LIST_MAP: "map"
|
||||
7 LIST_LENGTH: "length"
|
||||
8 LIST_FOLDL: "foldl"
|
||||
9 LIST_FOLDR: "foldr"
|
||||
10 LIST_GET_UNSAFE: "getUnsafe" // TODO remove once we can code gen Result
|
||||
}
|
||||
7 RESULT: "Result" => {
|
||||
0 RESULT_RESULT: "Result" imported // the Result.Result type alias
|
||||
|
@ -166,15 +166,32 @@ fn find_names_needed(
|
||||
find_names_needed(ext_var, subs, roots, root_appearances, names_taken);
|
||||
find_names_needed(rec_var, subs, roots, root_appearances, names_taken);
|
||||
}
|
||||
Structure(Boolean(b)) => {
|
||||
for var in b.variables() {
|
||||
find_names_needed(var, subs, roots, root_appearances, names_taken);
|
||||
Structure(Boolean(b)) =>
|
||||
// NOTE it's important that we traverse the variables in the same order as they are
|
||||
// below in write_boolean, hence the call to `simplify`.
|
||||
{
|
||||
match b.simplify(subs) {
|
||||
Err(Atom::Variable(var)) => {
|
||||
find_names_needed(var, subs, roots, root_appearances, names_taken);
|
||||
}
|
||||
Err(_) => {}
|
||||
Ok(mut variables) => {
|
||||
variables.sort();
|
||||
for var in variables {
|
||||
find_names_needed(var, subs, roots, root_appearances, names_taken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Alias(_, args, _actual) => {
|
||||
// TODO should we also look in the actual variable?
|
||||
for (_, var) in args {
|
||||
find_names_needed(var, subs, roots, root_appearances, names_taken);
|
||||
Alias(symbol, args, _actual) => {
|
||||
if let Symbol::ATTR_ATTR = symbol {
|
||||
find_names_needed(args[0].1, subs, roots, root_appearances, names_taken);
|
||||
find_names_needed(args[1].1, subs, roots, root_appearances, names_taken);
|
||||
} else {
|
||||
// TODO should we also look in the actual variable?
|
||||
for (_, var) in args {
|
||||
find_names_needed(var, subs, roots, root_appearances, names_taken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Error | Structure(Erroneous(_)) | Structure(EmptyRecord) | Structure(EmptyTagUnion) => {
|
||||
@ -523,7 +540,8 @@ fn chase_ext_tag_union(
|
||||
fn write_boolean(env: &Env, boolean: Bool, subs: &mut Subs, buf: &mut String, parens: Parens) {
|
||||
match boolean.simplify(subs) {
|
||||
Err(atom) => write_boolean_atom(env, atom, subs, buf, parens),
|
||||
Ok(variables) => {
|
||||
Ok(mut variables) => {
|
||||
variables.sort();
|
||||
let mut buffers_set = ImSet::default();
|
||||
|
||||
for v in variables {
|
||||
|
69
src/solve.rs
69
src/solve.rs
@ -51,12 +51,32 @@ pub enum SolvedType {
|
||||
Alias(Symbol, Vec<(Lowercase, SolvedType)>, Box<SolvedType>),
|
||||
|
||||
/// a boolean algebra Bool
|
||||
Boolean(boolean_algebra::Bool),
|
||||
Boolean(SolvedAtom, Vec<SolvedAtom>),
|
||||
|
||||
/// A type error
|
||||
Error,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum SolvedAtom {
|
||||
Zero,
|
||||
One,
|
||||
Variable(VarId),
|
||||
}
|
||||
|
||||
impl SolvedAtom {
|
||||
pub fn from_atom(atom: boolean_algebra::Atom) -> Self {
|
||||
use boolean_algebra::Atom::*;
|
||||
|
||||
// NOTE we blindly trust that `var` is a root and has a FlexVar as content
|
||||
match atom {
|
||||
Zero => SolvedAtom::Zero,
|
||||
One => SolvedAtom::One,
|
||||
Variable(var) => SolvedAtom::Variable(VarId::from_var(var)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SolvedType {
|
||||
pub fn new(solved_subs: &Solved<Subs>, var: Variable) -> Self {
|
||||
Self::from_var(solved_subs.inner(), var)
|
||||
@ -152,7 +172,15 @@ impl SolvedType {
|
||||
|
||||
SolvedType::Alias(symbol, solved_args, Box::new(solved_type))
|
||||
}
|
||||
Boolean(val) => SolvedType::Boolean(val),
|
||||
Boolean(val) => {
|
||||
let free = SolvedAtom::from_atom(val.0);
|
||||
|
||||
let mut rest = Vec::with_capacity(val.1.len());
|
||||
for atom in val.1 {
|
||||
rest.push(SolvedAtom::from_atom(atom));
|
||||
}
|
||||
SolvedType::Boolean(free, rest)
|
||||
}
|
||||
Variable(var) => Self::from_var(solved_subs.inner(), var),
|
||||
}
|
||||
}
|
||||
@ -254,7 +282,15 @@ impl SolvedType {
|
||||
}
|
||||
EmptyRecord => SolvedType::EmptyRecord,
|
||||
EmptyTagUnion => SolvedType::EmptyTagUnion,
|
||||
Boolean(val) => SolvedType::Boolean(val),
|
||||
Boolean(val) => {
|
||||
let free = SolvedAtom::from_atom(val.0);
|
||||
|
||||
let mut rest = Vec::with_capacity(val.1.len());
|
||||
for atom in val.1 {
|
||||
rest.push(SolvedAtom::from_atom(atom));
|
||||
}
|
||||
SolvedType::Boolean(free, rest)
|
||||
}
|
||||
Erroneous(problem) => SolvedType::Erroneous(problem),
|
||||
}
|
||||
}
|
||||
@ -871,6 +907,23 @@ fn check_for_infinite_type(
|
||||
}
|
||||
}
|
||||
|
||||
fn content_attr_alias(subs: &mut Subs, u: Variable, a: Variable) -> Content {
|
||||
let actual = subs.fresh_unnamed_flex_var();
|
||||
let ext_var = subs.fresh_unnamed_flex_var();
|
||||
|
||||
let mut attr_at_attr = MutMap::default();
|
||||
attr_at_attr.insert(TagName::Private(Symbol::ATTR_AT_ATTR), vec![u, a]);
|
||||
let attr_tag = FlatType::TagUnion(attr_at_attr, ext_var);
|
||||
|
||||
subs.set_content(actual, Content::Structure(attr_tag));
|
||||
|
||||
Content::Alias(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![("u".into(), u), ("a".into(), a)],
|
||||
actual,
|
||||
)
|
||||
}
|
||||
|
||||
fn correct_recursive_attr(
|
||||
subs: &mut Subs,
|
||||
recursive: Variable,
|
||||
@ -882,10 +935,8 @@ fn correct_recursive_attr(
|
||||
let rec_var = subs.fresh_unnamed_flex_var();
|
||||
let attr_var = subs.fresh_unnamed_flex_var();
|
||||
|
||||
subs.set_content(
|
||||
attr_var,
|
||||
Content::Structure(FlatType::Apply(Symbol::ATTR_ATTR, vec![uniq_var, rec_var])),
|
||||
);
|
||||
let content = content_attr_alias(subs, uniq_var, rec_var);
|
||||
subs.set_content(attr_var, content);
|
||||
|
||||
let mut new_tags = MutMap::default();
|
||||
|
||||
@ -902,9 +953,9 @@ fn correct_recursive_attr(
|
||||
let new_tag_type = FlatType::RecursiveTagUnion(rec_var, new_tags, new_ext_var);
|
||||
subs.set_content(tag_union_var, Content::Structure(new_tag_type));
|
||||
|
||||
let new_recursive = FlatType::Apply(Symbol::ATTR_ATTR, vec![uniq_var, tag_union_var]);
|
||||
let new_recursive = content_attr_alias(subs, uniq_var, tag_union_var);
|
||||
|
||||
subs.set_content(recursive, Content::Structure(new_recursive));
|
||||
subs.set_content(recursive, new_recursive);
|
||||
}
|
||||
|
||||
fn circular_error(
|
||||
|
@ -417,7 +417,11 @@ impl Type {
|
||||
}
|
||||
ext.instantiate_aliases(aliases, var_store, introduced);
|
||||
}
|
||||
Alias(_, _, actual_type) => {
|
||||
Alias(_, type_args, actual_type) => {
|
||||
for arg in type_args {
|
||||
arg.1.instantiate_aliases(aliases, var_store, introduced);
|
||||
}
|
||||
|
||||
actual_type.instantiate_aliases(aliases, var_store, introduced);
|
||||
}
|
||||
Apply(symbol, args) => {
|
||||
|
@ -4,7 +4,7 @@ use crate::can::ident::TagName;
|
||||
use crate::collections::{default_hasher, MutMap};
|
||||
use crate::module::symbol::Symbol;
|
||||
use crate::region::{Located, Region};
|
||||
use crate::solve::{BuiltinAlias, SolvedType};
|
||||
use crate::solve::{BuiltinAlias, SolvedAtom, SolvedType};
|
||||
use crate::subs::VarId;
|
||||
use std::collections::HashMap;
|
||||
|
||||
@ -15,6 +15,7 @@ const NUM_BUILTIN_IMPORTS: usize = 7;
|
||||
/// These can be shared between definitions, they will get instantiated when converted to Type
|
||||
const TVAR1: VarId = VarId::from_u32(1);
|
||||
const TVAR2: VarId = VarId::from_u32(2);
|
||||
const TVAR3: VarId = VarId::from_u32(3);
|
||||
|
||||
/// These can be shared between definitions, they will get instantiated when converted to Type
|
||||
const FUVAR: VarId = VarId::from_u32(1000);
|
||||
@ -22,14 +23,73 @@ const UVAR1: VarId = VarId::from_u32(1001);
|
||||
const UVAR2: VarId = VarId::from_u32(1002);
|
||||
const UVAR3: VarId = VarId::from_u32(1003);
|
||||
const UVAR4: VarId = VarId::from_u32(1004);
|
||||
const UVAR5: VarId = VarId::from_u32(1005);
|
||||
const UVAR6: VarId = VarId::from_u32(1006);
|
||||
|
||||
fn shared() -> SolvedType {
|
||||
SolvedType::Boolean(SolvedAtom::Zero, vec![])
|
||||
}
|
||||
|
||||
fn boolean(b: VarId) -> SolvedType {
|
||||
SolvedType::Boolean(SolvedAtom::Variable(b), vec![])
|
||||
}
|
||||
|
||||
fn disjunction(free: VarId, rest: Vec<VarId>) -> SolvedType {
|
||||
let solved_rest = rest.into_iter().map(SolvedAtom::Variable).collect();
|
||||
|
||||
SolvedType::Boolean(SolvedAtom::Variable(free), solved_rest)
|
||||
}
|
||||
|
||||
pub fn uniqueness_stdlib() -> StdLib {
|
||||
use builtins::Mode;
|
||||
|
||||
let types = types();
|
||||
let aliases = aliases();
|
||||
|
||||
/*
|
||||
debug_assert!({
|
||||
let normal_types: MutSet<Symbol> = builtins::types().keys().copied().collect();
|
||||
let normal_aliases: MutSet<Symbol> = builtins::aliases().keys().copied().collect();
|
||||
|
||||
let unique_types = types.keys().copied().collect();
|
||||
let unique_aliases = aliases.keys().copied().collect();
|
||||
|
||||
let missing_unique_types: MutSet<Symbol> =
|
||||
normal_types.difference(&unique_types).copied().collect();
|
||||
let missing_normal_types: MutSet<Symbol> =
|
||||
unique_types.difference(&normal_types).copied().collect();
|
||||
|
||||
let missing_unique_aliases: MutSet<Symbol> = normal_aliases
|
||||
.difference(&unique_aliases)
|
||||
.copied()
|
||||
.collect();
|
||||
let missing_normal_aliases: MutSet<Symbol> = unique_aliases
|
||||
.difference(&normal_aliases)
|
||||
.copied()
|
||||
.filter(|v| *v != Symbol::ATTR_ATTR)
|
||||
.collect();
|
||||
|
||||
let cond = missing_normal_types.is_empty()
|
||||
&& missing_unique_types.is_empty()
|
||||
&& missing_normal_aliases.is_empty()
|
||||
&& missing_unique_aliases.is_empty();
|
||||
|
||||
if !cond {
|
||||
println!("Missing hardcoded types for:");
|
||||
println!("normal types: {:?}", missing_normal_types);
|
||||
println!("unique types: {:?}", missing_unique_types);
|
||||
println!("normal aliases: {:?}", missing_normal_aliases);
|
||||
println!("unique aliases: {:?}", missing_unique_aliases);
|
||||
}
|
||||
|
||||
cond
|
||||
});
|
||||
*/
|
||||
|
||||
StdLib {
|
||||
mode: Mode::Uniqueness,
|
||||
types: types(),
|
||||
aliases: aliases(),
|
||||
types,
|
||||
aliases,
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,6 +192,22 @@ pub fn aliases() -> MutMap<Symbol, BuiltinAlias> {
|
||||
},
|
||||
);
|
||||
|
||||
// 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),
|
||||
),
|
||||
},
|
||||
);
|
||||
|
||||
// List a : [ @List a ]
|
||||
add_alias(
|
||||
Symbol::LIST_LIST,
|
||||
@ -161,6 +237,16 @@ pub fn aliases() -> MutMap<Symbol, BuiltinAlias> {
|
||||
},
|
||||
);
|
||||
|
||||
// Str : [ @Str ]
|
||||
add_alias(
|
||||
Symbol::STR_STR,
|
||||
BuiltinAlias {
|
||||
region: Region::zero(),
|
||||
vars: Vec::new(),
|
||||
typ: single_private_tag(Symbol::STR_AT_STR, Vec::new()),
|
||||
},
|
||||
);
|
||||
|
||||
aliases
|
||||
}
|
||||
|
||||
@ -200,6 +286,27 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
),
|
||||
);
|
||||
|
||||
// mul or (*) : Num a, Num a -> Num a
|
||||
add_type(
|
||||
Symbol::NUM_MUL,
|
||||
unique_function(
|
||||
vec![num_type(UVAR1, TVAR1), num_type(UVAR2, TVAR1)],
|
||||
num_type(UVAR3, TVAR1),
|
||||
),
|
||||
);
|
||||
|
||||
// abs : Num a -> Num a
|
||||
add_type(
|
||||
Symbol::NUM_ABS,
|
||||
unique_function(vec![num_type(UVAR1, TVAR1)], num_type(UVAR2, TVAR1)),
|
||||
);
|
||||
|
||||
// neg : Num a -> Num a
|
||||
add_type(
|
||||
Symbol::NUM_NEG,
|
||||
unique_function(vec![num_type(UVAR1, TVAR1)], num_type(UVAR2, TVAR1)),
|
||||
);
|
||||
|
||||
// isLt or (<) : Num a, Num a -> Bool
|
||||
add_type(
|
||||
Symbol::NUM_LT,
|
||||
@ -218,6 +325,24 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
),
|
||||
);
|
||||
|
||||
// isGt or (>) : Num a, Num a -> Bool
|
||||
add_type(
|
||||
Symbol::NUM_GT,
|
||||
unique_function(
|
||||
vec![num_type(UVAR1, TVAR1), num_type(UVAR2, TVAR1)],
|
||||
bool_type(UVAR3),
|
||||
),
|
||||
);
|
||||
|
||||
// isGte or (>=) : Num a, Num a -> Bool
|
||||
add_type(
|
||||
Symbol::NUM_GE,
|
||||
unique_function(
|
||||
vec![num_type(UVAR1, TVAR1), num_type(UVAR2, TVAR1)],
|
||||
bool_type(UVAR3),
|
||||
),
|
||||
);
|
||||
|
||||
// Int module
|
||||
|
||||
// highest : Int
|
||||
@ -226,11 +351,18 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
// lowest : Int
|
||||
add_type(Symbol::INT_LOWEST, int_type(UVAR1));
|
||||
|
||||
// div or (//) : Int, Int -> Int
|
||||
add_type(
|
||||
Symbol::INT_DIV,
|
||||
unique_function(vec![int_type(UVAR1), int_type(UVAR2)], int_type(UVAR3)),
|
||||
);
|
||||
|
||||
// mod : Int, Int -> Int
|
||||
add_type(
|
||||
Symbol::INT_MOD,
|
||||
unique_function(vec![int_type(UVAR1), int_type(UVAR2)], int_type(UVAR3)),
|
||||
);
|
||||
|
||||
// Float module
|
||||
|
||||
// div : Float, Float -> Float
|
||||
@ -242,6 +374,21 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
),
|
||||
);
|
||||
|
||||
// mod : Float, Float -> Float
|
||||
add_type(
|
||||
Symbol::FLOAT_MOD,
|
||||
unique_function(
|
||||
vec![float_type(UVAR1), float_type(UVAR2)],
|
||||
float_type(UVAR3),
|
||||
),
|
||||
);
|
||||
|
||||
// sqrt : Float -> Float
|
||||
add_type(
|
||||
Symbol::FLOAT_SQRT,
|
||||
unique_function(vec![float_type(UVAR1)], float_type(UVAR2)),
|
||||
);
|
||||
|
||||
// highest : Float
|
||||
add_type(Symbol::FLOAT_HIGHEST, float_type(UVAR1));
|
||||
|
||||
@ -250,6 +397,30 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
|
||||
// Bool module
|
||||
|
||||
// and or (&&) : Attr u1 Bool, Attr u2 Bool -> Attr u3 Bool
|
||||
add_type(
|
||||
Symbol::BOOL_AND,
|
||||
unique_function(vec![bool_type(UVAR1), bool_type(UVAR2)], bool_type(UVAR3)),
|
||||
);
|
||||
|
||||
// or or (||) : Attr u1 Bool, Attr u2 Bool -> Attr u3 Bool
|
||||
add_type(
|
||||
Symbol::BOOL_OR,
|
||||
unique_function(vec![bool_type(UVAR1), bool_type(UVAR2)], bool_type(UVAR3)),
|
||||
);
|
||||
|
||||
// xor : Attr u1 Bool, Attr u2 Bool -> Attr u3 Bool
|
||||
add_type(
|
||||
Symbol::BOOL_XOR,
|
||||
unique_function(vec![bool_type(UVAR1), bool_type(UVAR2)], bool_type(UVAR3)),
|
||||
);
|
||||
|
||||
// not : Attr u1 Bool -> Attr u2 Bool
|
||||
add_type(
|
||||
Symbol::BOOL_NOT,
|
||||
unique_function(vec![bool_type(UVAR1)], bool_type(UVAR2)),
|
||||
);
|
||||
|
||||
// List module
|
||||
|
||||
// isEmpty : Attr u (List *) -> Attr v Bool
|
||||
@ -258,6 +429,12 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
unique_function(vec![list_type(UVAR1, TVAR1)], bool_type(UVAR2)),
|
||||
);
|
||||
|
||||
// length : List a -> Int
|
||||
add_type(
|
||||
Symbol::LIST_LENGTH,
|
||||
unique_function(vec![list_type(UVAR1, TVAR1)], int_type(UVAR2)),
|
||||
);
|
||||
|
||||
// get : List a, Int -> Result a [ IndexOutOfBounds ]*
|
||||
let index_out_of_bounds = SolvedType::TagUnion(
|
||||
vec![(TagName::Global("IndexOutOfBounds".into()), vec![])],
|
||||
@ -272,15 +449,164 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
|
||||
),
|
||||
);
|
||||
|
||||
// set : List a, Int, a -> List a
|
||||
add_type(
|
||||
Symbol::LIST_SET,
|
||||
Symbol::LIST_GET_UNSAFE,
|
||||
unique_function(
|
||||
vec![list_type(UVAR1, TVAR1), int_type(UVAR2), flex(TVAR1)],
|
||||
list_type(UVAR3, TVAR1),
|
||||
vec![list_type(UVAR1, TVAR1), int_type(UVAR2)],
|
||||
attr_type(UVAR3, TVAR1),
|
||||
),
|
||||
);
|
||||
|
||||
// set : Attr (w | u | v) (List (Attr u a))
|
||||
// , Attr * Int
|
||||
// , Attr (u | v) a
|
||||
// -> List a
|
||||
add_type(Symbol::LIST_SET, {
|
||||
let u = UVAR1;
|
||||
let v = UVAR2;
|
||||
let w = UVAR3;
|
||||
let star1 = UVAR4;
|
||||
let star2 = UVAR5;
|
||||
|
||||
let a = TVAR1;
|
||||
|
||||
unique_function(
|
||||
vec![
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
disjunction(w, vec![u, v]),
|
||||
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
|
||||
],
|
||||
),
|
||||
int_type(star1),
|
||||
SolvedType::Apply(Symbol::ATTR_ATTR, vec![disjunction(u, vec![v]), flex(a)]),
|
||||
],
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
boolean(star2),
|
||||
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
|
||||
],
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
// push : Attr (w | u | v) (List (Attr u a))
|
||||
// , Attr (u | v) a
|
||||
// -> Attr * (List (Attr u a))
|
||||
add_type(Symbol::LIST_PUSH, {
|
||||
let u = UVAR1;
|
||||
let v = UVAR2;
|
||||
let w = UVAR3;
|
||||
let star = UVAR4;
|
||||
|
||||
let a = TVAR1;
|
||||
|
||||
unique_function(
|
||||
vec![
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
disjunction(w, vec![u, v]),
|
||||
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
|
||||
],
|
||||
),
|
||||
SolvedType::Apply(Symbol::ATTR_ATTR, vec![disjunction(u, vec![v]), flex(a)]),
|
||||
],
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
boolean(star),
|
||||
SolvedType::Apply(Symbol::LIST_LIST, vec![attr_type(u, a)]),
|
||||
],
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
// map : List a, (a -> b) -> List b
|
||||
add_type(
|
||||
Symbol::LIST_MAP,
|
||||
unique_function(
|
||||
vec![
|
||||
list_type(UVAR1, TVAR1),
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
shared(),
|
||||
SolvedType::Func(vec![flex(TVAR1)], Box::new(flex(TVAR2))),
|
||||
],
|
||||
),
|
||||
],
|
||||
list_type(UVAR2, TVAR2),
|
||||
),
|
||||
);
|
||||
|
||||
// foldr : List a, (a -> b -> b), b -> b
|
||||
add_type(
|
||||
Symbol::LIST_FOLDR,
|
||||
unique_function(
|
||||
vec![
|
||||
list_type(UVAR1, TVAR1),
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
shared(),
|
||||
SolvedType::Func(vec![flex(TVAR1), flex(TVAR2)], Box::new(flex(TVAR2))),
|
||||
],
|
||||
),
|
||||
flex(TVAR2),
|
||||
],
|
||||
flex(TVAR2),
|
||||
),
|
||||
);
|
||||
|
||||
// Str module
|
||||
|
||||
// isEmpty : Attr u Str -> Attr v Bool
|
||||
add_type(Symbol::STR_ISEMPTY, {
|
||||
unique_function(vec![str_type(UVAR1)], bool_type(UVAR2))
|
||||
});
|
||||
|
||||
// Result module
|
||||
|
||||
// map : Attr (* | u | v) (Result (Attr u a) e), Attr * (Attr u a -> b) -> Attr * (Result b e)
|
||||
add_type(Symbol::RESULT_MAP, {
|
||||
let u = UVAR1;
|
||||
let star1 = UVAR4;
|
||||
let star2 = UVAR5;
|
||||
let star3 = UVAR6;
|
||||
|
||||
let a = TVAR1;
|
||||
let b = TVAR2;
|
||||
let e = TVAR3;
|
||||
unique_function(
|
||||
vec![
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
disjunction(star1, vec![u]),
|
||||
SolvedType::Apply(Symbol::RESULT_RESULT, vec![attr_type(u, a), flex(e)]),
|
||||
],
|
||||
),
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
flex(star2),
|
||||
SolvedType::Func(vec![attr_type(u, a)], Box::new(flex(b))),
|
||||
],
|
||||
),
|
||||
],
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![
|
||||
flex(star3),
|
||||
SolvedType::Apply(Symbol::RESULT_RESULT, vec![flex(b), flex(e)]),
|
||||
],
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
types
|
||||
}
|
||||
|
||||
@ -332,10 +658,12 @@ fn bool_type(u: VarId) -> SolvedType {
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[inline(always)]
|
||||
fn str_type() -> SolvedType {
|
||||
SolvedType::Apply(Symbol::STR_STR, Vec::new())
|
||||
fn str_type(u: VarId) -> SolvedType {
|
||||
SolvedType::Apply(
|
||||
Symbol::ATTR_ATTR,
|
||||
vec![flex(u), SolvedType::Apply(Symbol::STR_STR, Vec::new())],
|
||||
)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
use self::Atom::*;
|
||||
use crate::collections::{ImSet, SendSet};
|
||||
use crate::collections::SendSet;
|
||||
use crate::subs::{Content, FlatType, Subs, Variable};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||
@ -76,13 +76,13 @@ impl Bool {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn simplify(&self, subs: &mut Subs) -> Result<ImSet<Variable>, Atom> {
|
||||
pub fn simplify(&self, subs: &mut Subs) -> Result<Vec<Variable>, Atom> {
|
||||
match self.0 {
|
||||
Atom::Zero => Err(Atom::Zero),
|
||||
Atom::One => Err(Atom::One),
|
||||
Atom::Variable(var) => {
|
||||
let mut result = ImSet::default();
|
||||
result.insert(var);
|
||||
let mut result = Vec::new();
|
||||
result.push(var);
|
||||
|
||||
for atom in &self.1 {
|
||||
match atom {
|
||||
@ -93,7 +93,7 @@ impl Bool {
|
||||
match nested.simplify(subs) {
|
||||
Ok(variables) => {
|
||||
for var in variables {
|
||||
result.insert(var);
|
||||
result.push(var);
|
||||
}
|
||||
}
|
||||
Err(Atom::Zero) => {}
|
||||
@ -102,7 +102,7 @@ impl Bool {
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
result.insert(*v);
|
||||
result.push(*v);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -119,7 +119,9 @@ pub fn uniq_expr_with(
|
||||
expected2,
|
||||
);
|
||||
|
||||
let types = unique_builtins::types();
|
||||
let stdlib = unique_builtins::uniqueness_stdlib();
|
||||
|
||||
let types = stdlib.types;
|
||||
let imports: Vec<_> = types
|
||||
.iter()
|
||||
.map(|(symbol, (solved_type, region))| Import {
|
||||
@ -133,11 +135,8 @@ pub fn uniq_expr_with(
|
||||
roc::constrain::module::constrain_imported_values(imports, constraint, &var_store);
|
||||
|
||||
// load builtin types
|
||||
let mut constraint = roc::constrain::module::load_builtin_aliases(
|
||||
&unique_builtins::aliases(),
|
||||
constraint,
|
||||
&var_store,
|
||||
);
|
||||
let mut constraint =
|
||||
roc::constrain::module::load_builtin_aliases(&stdlib.aliases, constraint, &var_store);
|
||||
|
||||
constraint.instantiate_aliases(&var_store);
|
||||
|
||||
|
@ -1024,7 +1024,7 @@ mod test_infer_uniq {
|
||||
\{ left, right } -> { left, right }
|
||||
"#
|
||||
),
|
||||
"Attr * (Attr (* | a | b) { left : (Attr b c), right : (Attr a d) }* -> Attr * { left : (Attr b c), right : (Attr a d) })",
|
||||
"Attr * (Attr (* | a | b) { left : (Attr a c), right : (Attr b d) }* -> Attr * { left : (Attr a c), right : (Attr b d) })"
|
||||
);
|
||||
}
|
||||
|
||||
@ -1066,7 +1066,7 @@ mod test_infer_uniq {
|
||||
// TODO: is it safe to ignore uniqueness constraints from patterns that bind no identifiers?
|
||||
// i.e. the `b` could be ignored in this example, is that true in general?
|
||||
// seems like it because we don't really extract anything.
|
||||
"Attr * (Attr (* | a | b) [ Foo (Attr a c) (Attr b *) ]* -> Attr * [ Foo (Attr a c) (Attr * Str) ]*)",
|
||||
"Attr * (Attr (* | a | b) [ Foo (Attr a c) (Attr b *) ]* -> Attr * [ Foo (Attr a c) (Attr * Str) ]*)"
|
||||
);
|
||||
}
|
||||
|
||||
@ -1342,12 +1342,8 @@ mod test_infer_uniq {
|
||||
r.tic.tac.toe
|
||||
"#
|
||||
),
|
||||
"Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (b | c | e) { bar : (Attr (b | e) { baz : (Attr b f) }*) }*), tic : (Attr (a | b | d) { tac : (Attr (a | b) { toe : (Attr b f) }*) }*) }* -> Attr b f)"
|
||||
// "Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (c | d | e) { bar : (Attr (c | d) { baz : (Attr c f) }*) }*), tic : (Attr (a | b | c) { tac : (Attr (a | c) { toe : (Attr c f) }*) }*) }* -> Attr c f)"
|
||||
// "Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (b | d | e) { bar : (Attr (b | d) { baz : (Attr b f) }*) }*), tic : (Attr (a | b | c) { tac : (Attr (b | c) { toe : (Attr b f) }*) }*) }* -> Attr b f)"
|
||||
// "Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (b | c | e) { bar : (Attr (b | e) { baz : (Attr b f) }*) }*), tic : (Attr (a | b | d) { tac : (Attr (b | d) { toe : (Attr b f) }*) }*) }* -> Attr b f)"
|
||||
// "Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (a | c | d) { bar : (Attr (a | c) { baz : (Attr c f) }*) }*), tic : (Attr (b | c | e) { tac : (Attr (c | e) { toe : (Attr c f) }*) }*) }* -> Attr c f)"
|
||||
// "Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (a | c | d) { bar : (Attr (c | d) { baz : (Attr d f) }*) }*), tic : (Attr (b | d | e) { tac : (Attr (b | d) { toe : (Attr d f) }*) }*) }* -> Attr d f)"
|
||||
"Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (a | b | e) { bar : (Attr (a | e) { baz : (Attr e f) }*) }*), tic : (Attr (c | d | e) { tac : (Attr (d | e) { toe : (Attr e f) }*) }*) }* -> Attr e f)"
|
||||
// "Attr * (Attr (* | a | b | c | d | e) { foo : (Attr (a | b | c) { bar : (Attr (a | c) { baz : (Attr c f) }*) }*), tic : (Attr (c | d | e) { tac : (Attr (c | d) { toe : (Attr c f) }*) }*) }* -> Attr c f)"
|
||||
);
|
||||
}
|
||||
|
||||
@ -1890,19 +1886,6 @@ mod test_infer_uniq {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_set() {
|
||||
infer_eq(
|
||||
indoc!(
|
||||
r#"
|
||||
[1, 2 ]
|
||||
|> List.set 1 42
|
||||
"#
|
||||
),
|
||||
"Attr * (List (Attr * Int))",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn float_div_builtins() {
|
||||
infer_eq(
|
||||
@ -2024,4 +2007,83 @@ mod test_infer_uniq {
|
||||
"Attr * (Attr (a | b) (List (Attr b Int)) -> Attr (a | b) (List (Attr b Int)))",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_set() {
|
||||
infer_eq(indoc!(r#"List.set"#), "Attr * (Attr (* | a | b) (List (Attr a c)), Attr * Int, Attr (a | b) c -> Attr * (List (Attr a c)))");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_map() {
|
||||
infer_eq(
|
||||
indoc!(r#"List.map"#),
|
||||
"Attr * (Attr * (List a), Attr Shared (a -> b) -> Attr * (List b))",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_map_identity() {
|
||||
infer_eq(
|
||||
indoc!(r#"\list -> List.map list (\x -> x)"#),
|
||||
"Attr * (Attr * (List a) -> Attr * (List a))",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_foldr() {
|
||||
infer_eq(
|
||||
indoc!(r#"List.foldr"#),
|
||||
"Attr * (Attr * (List a), Attr Shared (a, b -> b), b -> b)",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_foldr_sum() {
|
||||
infer_eq(
|
||||
indoc!(
|
||||
r#"
|
||||
sum = \list -> List.foldr list Num.add 0
|
||||
|
||||
sum
|
||||
"#
|
||||
),
|
||||
"Attr * (Attr * (List (Attr * Int)) -> Attr * Int)",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_push() {
|
||||
infer_eq(
|
||||
indoc!(r#"List.push"#),
|
||||
"Attr * (Attr (* | a | b) (List (Attr a c)), Attr (a | b) c -> Attr * (List (Attr a c)))"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_push_singleton() {
|
||||
infer_eq(
|
||||
indoc!(
|
||||
r#"
|
||||
singleton = \x -> List.push [] x
|
||||
|
||||
singleton
|
||||
"#
|
||||
),
|
||||
"Attr * (Attr (* | a) b -> Attr * (List (Attr a b)))",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_foldr_reverse() {
|
||||
infer_eq(
|
||||
indoc!(
|
||||
r#"
|
||||
reverse = \list -> List.foldr list (\e, l -> List.push l e) []
|
||||
|
||||
reverse
|
||||
"#
|
||||
),
|
||||
"Attr * (Attr * (List (Attr (a | b) c)) -> Attr (* | a | b) (List (Attr a c)))",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user