add builtins required for AStar

This commit is contained in:
Folkert 2020-03-06 13:27:28 +01:00
parent de8d1f81d1
commit 4d061bd932
2 changed files with 167 additions and 2 deletions

View file

@ -159,6 +159,29 @@ pub fn aliases() -> MutMap<Symbol, BuiltinAlias> {
}, },
); );
// Map key value : [ @Map key value ]
add_alias(
Symbol::MAP_MAP,
BuiltinAlias {
region: Region::zero(),
vars: vec![
Located::at(Region::zero(), "key".into()),
Located::at(Region::zero(), "value".into()),
],
typ: single_private_tag(Symbol::MAP_AT_MAP, vec![flex(TVAR1), flex(TVAR2)]),
},
);
// Set key : [ @Set key ]
add_alias(
Symbol::SET_SET,
BuiltinAlias {
region: Region::zero(),
vars: vec![Located::at(Region::zero(), "key".into())],
typ: single_private_tag(Symbol::SET_AT_SET, vec![flex(TVAR1)]),
},
);
// Str : [ @Str ] // Str : [ @Str ]
add_alias( add_alias(
Symbol::STR_STR, Symbol::STR_STR,
@ -229,6 +252,18 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
SolvedType::Func(vec![num_type(flex(TVAR1))], Box::new(num_type(flex(TVAR1)))), SolvedType::Func(vec![num_type(flex(TVAR1))], Box::new(num_type(flex(TVAR1)))),
); );
// isEq or (==) : a, a -> Bool
add_type(
Symbol::BOOL_EQ,
SolvedType::Func(vec![flex(TVAR1), flex(TVAR1)], Box::new(bool_type())),
);
// isNeq or (!=) : a, a -> Bool
add_type(
Symbol::BOOL_NEQ,
SolvedType::Func(vec![flex(TVAR1), flex(TVAR1)], Box::new(bool_type())),
);
// isLt or (<) : Num a, Num a -> Bool // isLt or (<) : Num a, Num a -> Bool
add_type( add_type(
Symbol::NUM_LT, Symbol::NUM_LT,
@ -265,6 +300,12 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
), ),
); );
// toFloat : Num a -> Float
add_type(
Symbol::NUM_TO_FLOAT,
SolvedType::Func(vec![num_type(flex(TVAR1))], Box::new(float_type())),
);
// Int module // Int module
// highest : Int // highest : Int
@ -430,7 +471,7 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
add_type( add_type(
Symbol::LIST_PUSH, Symbol::LIST_PUSH,
SolvedType::Func( SolvedType::Func(
vec![list_type(flex(TVAR1))], vec![list_type(flex(TVAR1)), flex(TVAR1)],
Box::new(list_type(flex(TVAR1))), Box::new(list_type(flex(TVAR1))),
), ),
); );
@ -441,6 +482,100 @@ pub fn types() -> MutMap<Symbol, (SolvedType, Region)> {
SolvedType::Func(vec![list_type(flex(TVAR1))], Box::new(int_type())), SolvedType::Func(vec![list_type(flex(TVAR1))], Box::new(int_type())),
); );
// Map module
// empty : Map k v
add_type(Symbol::MAP_EMPTY, map_type(flex(TVAR1), flex(TVAR2)));
// singleton : k, v -> Map k v
add_type(
Symbol::MAP_SINGLETON,
SolvedType::Func(
vec![flex(TVAR1), flex(TVAR2)],
Box::new(map_type(flex(TVAR1), flex(TVAR2))),
),
);
// get : Map k v, k -> Result v [ KeyNotFound ]*
let key_not_found = SolvedType::TagUnion(
vec![(TagName::Global("KeyNotFound".into()), vec![])],
Box::new(SolvedType::Wildcard),
);
add_type(
Symbol::MAP_GET,
SolvedType::Func(
vec![map_type(flex(TVAR1), flex(TVAR2)), flex(TVAR1)],
Box::new(result_type(flex(TVAR2), key_not_found)),
),
);
add_type(
Symbol::MAP_INSERT,
SolvedType::Func(
vec![map_type(flex(TVAR1), flex(TVAR2)), flex(TVAR1), flex(TVAR2)],
Box::new(map_type(flex(TVAR1), flex(TVAR2))),
),
);
// Set module
// empty : Set a
add_type(Symbol::SET_EMPTY, set_type(flex(TVAR1)));
// singleton : a -> Set a
add_type(
Symbol::SET_SINGLETON,
SolvedType::Func(vec![flex(TVAR1)], Box::new(set_type(flex(TVAR1)))),
);
// union : Set a, Set a -> Set a
add_type(
Symbol::SET_UNION,
SolvedType::Func(
vec![set_type(flex(TVAR1)), set_type(flex(TVAR1))],
Box::new(set_type(flex(TVAR1))),
),
);
// diff : Set a, Set a -> Set a
add_type(
Symbol::SET_DIFF,
SolvedType::Func(
vec![set_type(flex(TVAR1)), set_type(flex(TVAR1))],
Box::new(set_type(flex(TVAR1))),
),
);
// foldl : Set a, (a -> b -> b), b -> b
add_type(
Symbol::SET_FOLDL,
SolvedType::Func(
vec![
set_type(flex(TVAR1)),
SolvedType::Func(vec![flex(TVAR1), flex(TVAR2)], Box::new(flex(TVAR2))),
flex(TVAR2),
],
Box::new(flex(TVAR2)),
),
);
add_type(
Symbol::SET_INSERT,
SolvedType::Func(
vec![set_type(flex(TVAR1)), flex(TVAR1)],
Box::new(set_type(flex(TVAR1))),
),
);
add_type(
Symbol::SET_REMOVE,
SolvedType::Func(
vec![set_type(flex(TVAR1)), flex(TVAR1)],
Box::new(set_type(flex(TVAR1))),
),
);
// Result module // Result module
// map : Result a err, (a -> b) -> Result b err // map : Result a err, (a -> b) -> Result b err
@ -497,3 +632,13 @@ fn result_type(a: SolvedType, e: SolvedType) -> SolvedType {
fn list_type(a: SolvedType) -> SolvedType { fn list_type(a: SolvedType) -> SolvedType {
SolvedType::Apply(Symbol::LIST_LIST, vec![a]) SolvedType::Apply(Symbol::LIST_LIST, vec![a])
} }
#[inline(always)]
fn set_type(a: SolvedType) -> SolvedType {
SolvedType::Apply(Symbol::SET_SET, vec![a])
}
#[inline(always)]
fn map_type(key: SolvedType, value: SolvedType) -> SolvedType {
SolvedType::Apply(Symbol::MAP_MAP, vec![key, value])
}

View file

@ -565,6 +565,7 @@ define_builtins! {
8 NUM_LE: "isLte" 8 NUM_LE: "isLte"
9 NUM_GT: "isGt" 9 NUM_GT: "isGt"
10 NUM_GE: "isGte" 10 NUM_GE: "isGte"
11 NUM_TO_FLOAT: "toFloat"
} }
2 INT: "Int" => { 2 INT: "Int" => {
0 INT_INT: "Int" imported // the Int.Int type alias 0 INT_INT: "Int" imported // the Int.Int type alias
@ -616,6 +617,25 @@ define_builtins! {
0 RESULT_RESULT: "Result" imported // the Result.Result type alias 0 RESULT_RESULT: "Result" imported // the Result.Result type alias
1 RESULT_MAP: "map" 1 RESULT_MAP: "map"
} }
8 MAP: "Map" => {
0 MAP_MAP: "Map" imported // the Map.Map type alias
1 MAP_AT_MAP: "@Map" // the Map.@Map private tag
2 MAP_EMPTY: "empty"
3 MAP_SINGLETON: "singleton"
4 MAP_GET: "get"
5 MAP_INSERT: "insert"
}
9 SET: "Set" => {
0 SET_SET: "Set" imported // the Set.Set type alias
1 SET_AT_SET: "@Set" // the Set.@Set private tag
2 SET_EMPTY: "empty"
3 SET_SINGLETON: "singleton"
4 SET_UNION: "union"
5 SET_FOLDL: "foldl"
6 SET_INSERT: "insert"
7 SET_REMOVE: "remove"
8 SET_DIFF: "diff"
}
num_modules: 8 // Keep this count up to date by hand! (Rust macros can't do arithmetic.) num_modules: 10 // Keep this count up to date by hand! (Rust macros can't do arithmetic.)
} }