This commit is contained in:
Folkert 2022-02-27 21:53:56 +01:00
parent 5c31234b24
commit db1669154e
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
6 changed files with 118 additions and 64 deletions

View file

@ -314,7 +314,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
Box::new(result_type(int_type(flex(TVAR1)), div_by_zero.clone())), Box::new(result_type(int_type(flex(TVAR1)), div_by_zero.clone())),
); );
//divCeil: Int a, Int a -> Result (Int a) [ DivByZero ]* // divCeil: Int a, Int a -> Result (Int a) [ DivByZero ]*
add_top_level_function_type!( add_top_level_function_type!(
Symbol::NUM_DIV_CEIL, Symbol::NUM_DIV_CEIL,
vec![int_type(flex(TVAR1)), int_type(flex(TVAR1))], vec![int_type(flex(TVAR1)), int_type(flex(TVAR1))],

View file

@ -383,8 +383,7 @@ fn can_annotation_help(
As( As(
loc_inner, loc_inner,
_spaces, _spaces,
alias_header alias_header @ TypeHeader {
@ TypeHeader {
name, name,
vars: loc_vars, vars: loc_vars,
}, },

View file

@ -242,12 +242,19 @@ pub fn canonicalize_module_defs<'a>(
panic!("TODO gracefully handle shadowing in imports.") panic!("TODO gracefully handle shadowing in imports.")
} }
} }
} else if symbol == Symbol::LIST_LIST || symbol == Symbol::STR_STR { } else if [
Symbol::LIST_LIST,
Symbol::STR_STR,
Symbol::DICT_DICT,
Symbol::SET_SET,
// Symbol::BOX_BOX,
]
.contains(&symbol)
{
// These are not aliases but Apply's and we make sure they are always in scope // These are not aliases but Apply's and we make sure they are always in scope
} else { } else {
// This is a type alias // This is a type alias
dbg!(scope.aliases.keys().collect::<Vec<_>>());
// the symbol should already be added to the scope when this module is canonicalized // the symbol should already be added to the scope when this module is canonicalized
debug_assert!( debug_assert!(
scope.contains_alias(symbol), scope.contains_alias(symbol),

View file

@ -144,7 +144,14 @@ pub fn pre_constrain_imports(
// We used this module, so clearly it is not unused! // We used this module, so clearly it is not unused!
unused_imports.remove(&module_id); unused_imports.remove(&module_id);
if module_id.is_builtin() && module_id != ModuleId::STR { let builtin_applies = [
Symbol::LIST_LIST,
Symbol::STR_STR,
Symbol::DICT_DICT,
Symbol::SET_SET,
];
if module_id.is_builtin() && builtin_applies.contains(&symbol) {
// For builtin modules, we create imports from the // For builtin modules, we create imports from the
// hardcoded builtin map. // hardcoded builtin map.
match stdlib.types.get(&symbol) { match stdlib.types.get(&symbol) {
@ -197,10 +204,7 @@ pub fn pre_constrain_imports(
}); });
} }
None => { None => {
panic!( panic!("Module {:?} does not have info for module {:?} in its exposed types", module_id, home)
"Could not find module {:?} in exposed_types {:?}",
module_id, exposed_types
);
} }
} }
@ -254,9 +258,9 @@ pub fn pre_constrain_imports(
} }
None => { None => {
panic!( panic!(
"Could not find module {:?} in exposed_types {:?}", "Module {:?} does not have info for module {:?} in its exposed types.\n I was looking for symbol {:?}",
module_id, exposed_types home, module_id, symbol,
); )
} }
} }
} }

View file

@ -278,8 +278,6 @@ fn start_phase<'a>(
} }
} }
dbg!(module_id, &parsed.imported_modules);
BuildTask::CanonicalizeAndConstrain { BuildTask::CanonicalizeAndConstrain {
parsed, parsed,
dep_idents, dep_idents,
@ -1744,29 +1742,45 @@ fn update<'a>(
.insert(ModuleId::NUM, Region::zero()); .insert(ModuleId::NUM, Region::zero());
let prelude_types = [ let prelude_types = [
Symbol::NUM_NUM, (Ident::from("Num"), Symbol::NUM_NUM),
Symbol::NUM_INT, (Ident::from("Int"), Symbol::NUM_INT),
Symbol::NUM_FLOAT, (Ident::from("Float"), Symbol::NUM_FLOAT),
Symbol::NUM_NAT, (Ident::from("Integer"), Symbol::NUM_INTEGER),
Symbol::NUM_I8, (Ident::from("FloatingPoint"), Symbol::NUM_FLOATINGPOINT),
Symbol::NUM_I16, (Ident::from("Binary32"), Symbol::NUM_BINARY32),
Symbol::NUM_I32, (Ident::from("Binary64"), Symbol::NUM_BINARY64),
Symbol::NUM_I64, (Ident::from("Signed128"), Symbol::NUM_SIGNED128),
Symbol::NUM_I128, (Ident::from("Signed64"), Symbol::NUM_SIGNED64),
Symbol::NUM_U8, (Ident::from("Signed32"), Symbol::NUM_SIGNED32),
Symbol::NUM_U16, (Ident::from("Signed16"), Symbol::NUM_SIGNED16),
Symbol::NUM_U32, (Ident::from("Signed8"), Symbol::NUM_SIGNED8),
Symbol::NUM_U64, (Ident::from("Unsigned128"), Symbol::NUM_UNSIGNED128),
Symbol::NUM_U128, (Ident::from("Unsigned64"), Symbol::NUM_UNSIGNED64),
Symbol::NUM_F32, (Ident::from("Unsigned32"), Symbol::NUM_UNSIGNED32),
Symbol::NUM_F64, (Ident::from("Unsigned16"), Symbol::NUM_UNSIGNED16),
Symbol::NUM_DEC, (Ident::from("Unsigned8"), Symbol::NUM_UNSIGNED8),
(Ident::from("Natural"), Symbol::NUM_NATURAL),
(Ident::from("Decimal"), Symbol::NUM_DECIMAL),
(Ident::from("Nat"), Symbol::NUM_NAT),
(Ident::from("I8"), Symbol::NUM_I8),
(Ident::from("I16"), Symbol::NUM_I16),
(Ident::from("I32"), Symbol::NUM_I32),
(Ident::from("I64"), Symbol::NUM_I64),
(Ident::from("I128"), Symbol::NUM_I128),
(Ident::from("U8"), Symbol::NUM_U8),
(Ident::from("U16"), Symbol::NUM_U16),
(Ident::from("U32"), Symbol::NUM_U32),
(Ident::from("U64"), Symbol::NUM_U64),
(Ident::from("U128"), Symbol::NUM_U128),
(Ident::from("F32"), Symbol::NUM_F32),
(Ident::from("F64"), Symbol::NUM_F64),
(Ident::from("Dec"), Symbol::NUM_DEC),
]; ];
for symbol in prelude_types { for (ident, symbol) in prelude_types {
header header
.exposed_imports .exposed_imports
.insert(Ident::from("Num"), (symbol, Region::zero())); .insert(ident, (symbol, Region::zero()));
} }
} }
@ -1785,15 +1799,14 @@ fn update<'a>(
} }
if !header.module_id.is_builtin() { if !header.module_id.is_builtin() {
// header header
// .package_qualified_imported_modules .package_qualified_imported_modules
// .insert(PackageQualified::Unqualified(ModuleId::STR)); .insert(PackageQualified::Unqualified(ModuleId::STR));
//
// header header
// .imported_modules .imported_modules
// .insert(ModuleId::STR, Region::zero()); .insert(ModuleId::STR, Region::zero());
/*
header header
.package_qualified_imported_modules .package_qualified_imported_modules
.insert(PackageQualified::Unqualified(ModuleId::DICT)); .insert(PackageQualified::Unqualified(ModuleId::DICT));
@ -1817,7 +1830,6 @@ fn update<'a>(
header header
.imported_modules .imported_modules
.insert(ModuleId::LIST, Region::zero()); .insert(ModuleId::LIST, Region::zero());
*/
} }
state state
@ -2622,6 +2634,9 @@ fn load_module<'a>(
let src_bytes = r#" let src_bytes = r#"
isEmpty : List a -> Bool isEmpty : List a -> Bool
isEmpty = \list ->
List.len list == 0
get : List a, Nat -> Result a [ OutOfBounds ]* get : List a, Nat -> Result a [ OutOfBounds ]*
set : List a, Nat, a -> List a set : List a, Nat, a -> List a
append : List a, a -> List a append : List a, a -> List a
@ -2639,7 +2654,12 @@ fn load_module<'a>(
walkUntil : List elem, state, (state, elem -> [ Continue state, Stop state ]) -> state walkUntil : List elem, state, (state, elem -> [ Continue state, Stop state ]) -> state
sum : List (Num a) -> Num a sum : List (Num a) -> Num a
sum = \list ->
List.walk list 0 Num.add
product : List (Num a) -> Num a product : List (Num a) -> Num a
product = \list ->
List.walk list 1 Num.mul
any : List a, (a -> Bool) -> Bool any : List a, (a -> Bool) -> Bool
all : List a, (a -> Bool) -> Bool all : List a, (a -> Bool) -> Bool
@ -2657,7 +2677,11 @@ fn load_module<'a>(
range : Int a, Int a -> List (Int a) range : Int a, Int a -> List (Int a)
sortWith : List a, (a, a -> [ LT, EQ, GT ] ) -> List a sortWith : List a, (a, a -> [ LT, EQ, GT ] ) -> List a
sortAsc : List (Num a) -> List (Num a) sortAsc : List (Num a) -> List (Num a)
sortAsc = \list -> List.sortWith list Num.compare
sortDesc : List (Num a) -> List (Num a) sortDesc : List (Num a) -> List (Num a)
sortDesc = \list -> List.sortWith list (\a, b -> Num.compare b a)
swap : List a, Nat, Nat -> List a swap : List a, Nat, Nat -> List a
first : List a -> Result a [ ListWasEmpty ]* first : List a -> Result a [ ListWasEmpty ]*
@ -2704,15 +2728,15 @@ fn load_module<'a>(
Loc::at_zero(ExposedName::new("repeat")), Loc::at_zero(ExposedName::new("repeat")),
Loc::at_zero(ExposedName::new("countGraphemes")), Loc::at_zero(ExposedName::new("countGraphemes")),
Loc::at_zero(ExposedName::new("startsWithCodePt")), Loc::at_zero(ExposedName::new("startsWithCodePt")),
// Loc::at_zero(ExposedName::new("toUtf8")), Loc::at_zero(ExposedName::new("toUtf8")),
// Loc::at_zero(ExposedName::new("fromUtf8")), Loc::at_zero(ExposedName::new("fromUtf8")),
// Loc::at_zero(ExposedName::new("fromUtf8Range")), Loc::at_zero(ExposedName::new("fromUtf8Range")),
Loc::at_zero(ExposedName::new("startsWith")), Loc::at_zero(ExposedName::new("startsWith")),
Loc::at_zero(ExposedName::new("endsWith")), Loc::at_zero(ExposedName::new("endsWith")),
Loc::at_zero(ExposedName::new("trim")), Loc::at_zero(ExposedName::new("trim")),
Loc::at_zero(ExposedName::new("trimLeft")), Loc::at_zero(ExposedName::new("trimLeft")),
Loc::at_zero(ExposedName::new("trimRight")), Loc::at_zero(ExposedName::new("trimRight")),
/* //
Loc::at_zero(ExposedName::new("toDec")), Loc::at_zero(ExposedName::new("toDec")),
Loc::at_zero(ExposedName::new("toF64")), Loc::at_zero(ExposedName::new("toF64")),
Loc::at_zero(ExposedName::new("toF32")), Loc::at_zero(ExposedName::new("toF32")),
@ -2727,7 +2751,6 @@ fn load_module<'a>(
Loc::at_zero(ExposedName::new("toI16")), Loc::at_zero(ExposedName::new("toI16")),
Loc::at_zero(ExposedName::new("toU8")), Loc::at_zero(ExposedName::new("toU8")),
Loc::at_zero(ExposedName::new("toI8")), Loc::at_zero(ExposedName::new("toI8")),
*/
]; ];
const IMPORTS: &[Loc<ImportsEntry>] = &[ const IMPORTS: &[Loc<ImportsEntry>] = &[
@ -2741,12 +2764,10 @@ fn load_module<'a>(
roc_parse::header::ModuleName::new("Bool"), roc_parse::header::ModuleName::new("Bool"),
Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("Bool")))]), Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("Bool")))]),
)), )),
/*
Loc::at_zero(ImportsEntry::Module( Loc::at_zero(ImportsEntry::Module(
roc_parse::header::ModuleName::new("List"), roc_parse::header::ModuleName::new("List"),
Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("List")))]), Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("List")))]),
)), )),
*/
Loc::at_zero(ImportsEntry::Module( Loc::at_zero(ImportsEntry::Module(
roc_parse::header::ModuleName::new("Num"), roc_parse::header::ModuleName::new("Num"),
Collection::with_items(&[ Collection::with_items(&[
@ -2799,7 +2820,7 @@ fn load_module<'a>(
EncodesSurrogateHalf, EncodesSurrogateHalf,
] ]
# Utf8Problem : { byteIndex : Nat, problem : Utf8ByteProblem } Utf8Problem : { byteIndex : Nat, problem : Utf8ByteProblem }
isEmpty : Str -> Bool isEmpty : Str -> Bool
concat : Str, Str -> Str concat : Str, Str -> Str
@ -2810,13 +2831,13 @@ fn load_module<'a>(
countGraphemes : Str -> Nat countGraphemes : Str -> Nat
startsWithCodePt : Str, U32 -> Bool startsWithCodePt : Str, U32 -> Bool
# toUtf8 : Str -> List U8 toUtf8 : Str -> List U8
# fromUtf8 : List U8 -> Result Str [ BadUtf8 Utf8Problem ]* # fromUtf8 : List U8 -> Result Str [ BadUtf8 Utf8Problem ]*
# fromUtf8Range : List U8 -> Result Str [ BadUtf8 Utf8Problem, OutOfBounds ]* # fromUtf8Range : List U8 -> Result Str [ BadUtf8 Utf8Problem, OutOfBounds ]*
# fromUtf8 : List U8 -> Result Str [ BadUtf8 Utf8ByteProblem Nat ]* fromUtf8 : List U8 -> Result Str [ BadUtf8 Utf8ByteProblem Nat ]*
# fromUtf8Range : List U8 -> Result Str [ BadUtf8 Utf8ByteProblem Nat, OutOfBounds ]* fromUtf8Range : List U8 -> Result Str [ BadUtf8 Utf8ByteProblem Nat, OutOfBounds ]*
startsWith : Str, Str -> Bool startsWith : Str, Str -> Bool
endsWith : Str, Str -> Bool endsWith : Str, Str -> Bool
@ -2824,6 +2845,21 @@ fn load_module<'a>(
trim : Str -> Str trim : Str -> Str
trimLeft : Str -> Str trimLeft : Str -> Str
trimRight : Str -> Str trimRight : Str -> Str
toDec : Str -> Result Dec [ InvalidNumStr ]*
toF64 : Str -> Result F64 [ InvalidNumStr ]*
toF32 : Str -> Result F32 [ InvalidNumStr ]*
toNat : Str -> Result Nat [ InvalidNumStr ]*
toU128 : Str -> Result U128 [ InvalidNumStr ]*
toI128 : Str -> Result I128 [ InvalidNumStr ]*
toU64 : Str -> Result U64 [ InvalidNumStr ]*
toI64 : Str -> Result I64 [ InvalidNumStr ]*
toU32 : Str -> Result U32 [ InvalidNumStr ]*
toI32 : Str -> Result I32 [ InvalidNumStr ]*
toU16 : Str -> Result U16 [ InvalidNumStr ]*
toI16 : Str -> Result I16 [ InvalidNumStr ]*
toU8 : Str -> Result U8 [ InvalidNumStr ]*
toI8 : Str -> Result I8 [ InvalidNumStr ]*
"#; "#;
let parse_state = roc_parse::state::State::new(src_bytes.as_bytes()); let parse_state = roc_parse::state::State::new(src_bytes.as_bytes());
@ -2869,10 +2905,10 @@ fn load_module<'a>(
roc_parse::header::ModuleName::new("List"), roc_parse::header::ModuleName::new("List"),
Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("List")))]), Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("List")))]),
)), )),
// Loc::at_zero(ImportsEntry::Module( Loc::at_zero(ImportsEntry::Module(
// roc_parse::header::ModuleName::new("Num"), roc_parse::header::ModuleName::new("Num"),
// Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("Nat")))]), Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("Nat")))]),
// )), )),
]; ];
const GENERATES_WITH: &[Symbol] = &[]; const GENERATES_WITH: &[Symbol] = &[];
@ -2920,7 +2956,7 @@ fn load_module<'a>(
)); ));
} }
"Set" => { "Set" => {
let filename = PathBuf::from("Dict.roc"); let filename = PathBuf::from("Set.roc");
const EXPOSES: &[Loc<ExposedName>] = &[ const EXPOSES: &[Loc<ExposedName>] = &[
Loc::at_zero(ExposedName::new("empty")), Loc::at_zero(ExposedName::new("empty")),
@ -2951,6 +2987,10 @@ fn load_module<'a>(
roc_parse::header::ModuleName::new("List"), roc_parse::header::ModuleName::new("List"),
Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("List")))]), Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("List")))]),
)), )),
Loc::at_zero(ImportsEntry::Module(
roc_parse::header::ModuleName::new("Dict"),
Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("Dict")))]),
)),
Loc::at_zero(ImportsEntry::Module( Loc::at_zero(ImportsEntry::Module(
roc_parse::header::ModuleName::new("Num"), roc_parse::header::ModuleName::new("Num"),
Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("Nat")))]), Collection::with_items(&[Loc::at_zero(Spaced::Item(ExposedName::new("Nat")))]),
@ -3109,9 +3149,9 @@ fn load_module<'a>(
Loc::at_zero(ExposedName::new("intCast")), Loc::at_zero(ExposedName::new("intCast")),
Loc::at_zero(ExposedName::new("bytesToU16")), Loc::at_zero(ExposedName::new("bytesToU16")),
Loc::at_zero(ExposedName::new("bytesToU32")), Loc::at_zero(ExposedName::new("bytesToU32")),
Loc::at_zero(ExposedName::new("divCeil")),
Loc::at_zero(ExposedName::new("toStr")),
*/ */
// Loc::at_zero(ExposedName::new("divCeil")),
// Loc::at_zero(ExposedName::new("toStr")),
Loc::at_zero(ExposedName::new("isMultipleOf")), Loc::at_zero(ExposedName::new("isMultipleOf")),
// Loc::at_zero(ExposedName::new("minI8")), // Loc::at_zero(ExposedName::new("minI8")),
// Loc::at_zero(ExposedName::new("maxI8")), // Loc::at_zero(ExposedName::new("maxI8")),
@ -3255,6 +3295,8 @@ fn load_module<'a>(
sqrt : Float a -> Result (Float a) [ SqrtOfNegative ]* sqrt : Float a -> Result (Float a) [ SqrtOfNegative ]*
log : Float a, Float a -> Result (Float a) [ LogNeedsPositive ]* log : Float a, Float a -> Result (Float a) [ LogNeedsPositive ]*
div : Float a -> Result (Float a) [ DivByZero ]* div : Float a -> Result (Float a) [ DivByZero ]*
# divCeil: Int a, Int a -> Result (Int a) [ DivByZero ]*
# mod : Float a, Float a -> Result (Float a) [ DivByZero ]* # mod : Float a, Float a -> Result (Float a) [ DivByZero ]*
rem : Int a, Int a -> Result (Int a) [ DivByZero ]* rem : Int a, Int a -> Result (Int a) [ DivByZero ]*
@ -3287,7 +3329,7 @@ fn load_module<'a>(
# mulSaturated : Num a, Num a -> Num a # mulSaturated : Num a, Num a -> Num a
mulChecked : Num a, Num a -> Result (Num a) [ Overflow ]* mulChecked : Num a, Num a -> Result (Num a) [ Overflow ]*
minI8 : I8 # minI8 : I8
# maxI8 : I8 # maxI8 : I8
# minU8 : U8 # minU8 : U8
# maxU8 : U8 # maxU8 : U8
@ -4285,7 +4327,7 @@ fn run_solve<'a>(
// Finish constraining the module by wrapping the existing Constraint // Finish constraining the module by wrapping the existing Constraint
// in the ones we just computed. We can do this off the main thread. // in the ones we just computed. We can do this off the main thread.
let constraint = constrain_imports(imported_symbols, constraint, &mut var_store); let constraint = constrain_imports(imported_symbols, constraint, &mut var_store);
dbg!(&constraint); // dbg!(&constraint);
let constrain_end = SystemTime::now(); let constrain_end = SystemTime::now();
@ -4305,6 +4347,8 @@ fn run_solve<'a>(
let (solved_subs, solved_env, problems) = let (solved_subs, solved_env, problems) =
roc_solve::module::run_solve(aliases, rigid_variables, constraint, var_store); roc_solve::module::run_solve(aliases, rigid_variables, constraint, var_store);
dbg!(module_id, &problems);
let mut exposed_vars_by_symbol: MutMap<Symbol, Variable> = solved_env.vars_by_symbol.clone(); let mut exposed_vars_by_symbol: MutMap<Symbol, Variable> = solved_env.vars_by_symbol.clone();
exposed_vars_by_symbol.retain(|k, _| exposed_symbols.contains(k)); exposed_vars_by_symbol.retain(|k, _| exposed_symbols.contains(k));

View file

@ -129,7 +129,7 @@ mod solve_expr {
let mut buffer = String::from(indoc!( let mut buffer = String::from(indoc!(
r#" r#"
app "test" app "test"
imports [ Result.{ Result } ] imports []
provides [ main ] to "./platform" provides [ main ] to "./platform"
main = main =