mirror of
https://github.com/roc-lang/roc.git
synced 2024-11-11 05:34:11 +03:00
some bug fixes
This commit is contained in:
parent
4fc0dd9dd9
commit
28835d5bf3
@ -10,9 +10,9 @@ Model position : {
|
||||
openSet : Set position,
|
||||
costs : Dict position F64,
|
||||
cameFrom : Dict position position,
|
||||
}
|
||||
} | position has Hash & Eq
|
||||
|
||||
initialModel : position -> Model position
|
||||
initialModel : position -> Model position | position has Hash & Eq
|
||||
initialModel = \start -> {
|
||||
evaluated: Set.empty,
|
||||
openSet: Set.single start,
|
||||
@ -20,7 +20,7 @@ initialModel = \start -> {
|
||||
cameFrom: Dict.empty,
|
||||
}
|
||||
|
||||
cheapestOpen : (position -> F64), Model position -> Result position {} | position has Eq
|
||||
cheapestOpen : (position -> F64), Model position -> Result position {} | position has Hash & Eq
|
||||
cheapestOpen = \costFn, model ->
|
||||
model.openSet
|
||||
|> Set.toList
|
||||
@ -35,13 +35,13 @@ cheapestOpen = \costFn, model ->
|
||||
|> Result.map .position
|
||||
|> Result.mapErr (\_ -> {})
|
||||
|
||||
reconstructPath : Dict position position, position -> List position | position has Eq
|
||||
reconstructPath : Dict position position, position -> List position | position has Hash & Eq
|
||||
reconstructPath = \cameFrom, goal ->
|
||||
when Dict.get cameFrom goal is
|
||||
Err _ -> []
|
||||
Ok next -> List.append (reconstructPath cameFrom next) goal
|
||||
|
||||
updateCost : position, position, Model position -> Model position | position has Eq
|
||||
updateCost : position, position, Model position -> Model position | position has Hash & Eq
|
||||
updateCost = \current, neighbor, model ->
|
||||
newCameFrom =
|
||||
Dict.insert model.cameFrom neighbor current
|
||||
@ -70,7 +70,7 @@ updateCost = \current, neighbor, model ->
|
||||
else
|
||||
model
|
||||
|
||||
astar : (position, position -> F64), (position -> Set position), position, Model position -> Result (List position) {} | position has Eq
|
||||
astar : (position, position -> F64), (position -> Set position), position, Model position -> Result (List position) {} | position has Hash & Eq
|
||||
astar = \costFn, moveFn, goal, model ->
|
||||
when cheapestOpen (\source -> costFn source goal) model is
|
||||
Err {} -> Err {}
|
||||
|
@ -96,9 +96,7 @@ Dict k v := {
|
||||
dataIndices : List Nat,
|
||||
data : List (T k v),
|
||||
size : Nat,
|
||||
}
|
||||
# TODO: re-add type definition one #4408 is fixed
|
||||
# | k has Hash & Eq
|
||||
} | k has Hash & Eq
|
||||
|
||||
## Return an empty dictionary.
|
||||
empty : Dict k v | k has Hash & Eq
|
||||
@ -457,13 +455,13 @@ insertAll = \xs, ys ->
|
||||
keepShared : Dict k v, Dict k v -> Dict k v | k has Hash & Eq
|
||||
keepShared = \xs, ys ->
|
||||
walk
|
||||
ys
|
||||
xs
|
||||
(\state, k, _ ->
|
||||
if contains state k then
|
||||
state
|
||||
empty
|
||||
(\state, k, v ->
|
||||
if contains ys k then
|
||||
insert state k v
|
||||
else
|
||||
remove state k
|
||||
state
|
||||
)
|
||||
|
||||
## Remove the key-value pairs in the first input that are also in the second
|
||||
|
@ -19,17 +19,18 @@ interface Set
|
||||
Bool.{ Bool, Eq },
|
||||
Dict.{ Dict },
|
||||
Num.{ Nat },
|
||||
Hash.{ Hasher, Hash },
|
||||
Hash.{ Hash },
|
||||
]
|
||||
|
||||
# We should have this line above the next has.
|
||||
# It causes the formatter to fail currently.
|
||||
# | k has Hash & Eq
|
||||
Set k := Dict.Dict k {}
|
||||
# TODO: re-add type definition one #4408 is fixed
|
||||
# | k has Hash & Eq
|
||||
has [
|
||||
Eq {
|
||||
isEq,
|
||||
},
|
||||
]
|
||||
has [
|
||||
Eq {
|
||||
isEq,
|
||||
},
|
||||
]
|
||||
|
||||
isEq : Set k, Set k -> Bool | k has Hash & Eq
|
||||
isEq = \_, _ -> Bool.true
|
||||
@ -42,9 +43,6 @@ single : k -> Set k | k has Hash & Eq
|
||||
single = \key ->
|
||||
Dict.single key {} |> @Set
|
||||
|
||||
## Make sure never to insert a *NaN* to a [Set]! Because *NaN* is defined to be
|
||||
## unequal to *NaN*, adding a *NaN* results in an entry that can never be
|
||||
## retrieved or removed from the [Set].
|
||||
insert : Set k, k -> Set k | k has Hash & Eq
|
||||
insert = \@Set dict, key ->
|
||||
Dict.insert dict key {} |> @Set
|
||||
|
@ -13,7 +13,7 @@ Model position :
|
||||
}
|
||||
|
||||
|
||||
initialModel : position -> Model position
|
||||
initialModel : position -> Model position | position has Hash & Eq
|
||||
initialModel = \start ->
|
||||
{ evaluated : Set.empty
|
||||
, openSet : Set.single start
|
||||
@ -22,7 +22,7 @@ initialModel = \start ->
|
||||
}
|
||||
|
||||
|
||||
cheapestOpen : (position -> F64), Model position -> Result position [KeyNotFound] | position has Eq
|
||||
cheapestOpen : (position -> F64), Model position -> Result position [KeyNotFound] | position has Hash & Eq
|
||||
cheapestOpen = \costFunction, model ->
|
||||
|
||||
folder = \resSmallestSoFar, position ->
|
||||
@ -47,7 +47,7 @@ cheapestOpen = \costFunction, model ->
|
||||
|
||||
|
||||
|
||||
reconstructPath : Dict position position, position -> List position | position has Eq
|
||||
reconstructPath : Dict position position, position -> List position | position has Hash & Eq
|
||||
reconstructPath = \cameFrom, goal ->
|
||||
when Dict.get cameFrom goal is
|
||||
Err KeyNotFound ->
|
||||
@ -56,7 +56,7 @@ reconstructPath = \cameFrom, goal ->
|
||||
Ok next ->
|
||||
List.append (reconstructPath cameFrom next) goal
|
||||
|
||||
updateCost : position, position, Model position -> Model position | position has Eq
|
||||
updateCost : position, position, Model position -> Model position | position has Hash & Eq
|
||||
updateCost = \current, neighbour, model ->
|
||||
newCameFrom = Dict.insert model.cameFrom neighbour current
|
||||
|
||||
@ -80,12 +80,12 @@ updateCost = \current, neighbour, model ->
|
||||
model
|
||||
|
||||
|
||||
findPath : { costFunction: (position, position -> F64), moveFunction: (position -> Set position), start : position, end : position } -> Result (List position) [KeyNotFound] | position has Eq
|
||||
findPath : { costFunction: (position, position -> F64), moveFunction: (position -> Set position), start : position, end : position } -> Result (List position) [KeyNotFound] | position has Hash & Eq
|
||||
findPath = \{ costFunction, moveFunction, start, end } ->
|
||||
astar costFunction moveFunction end (initialModel start)
|
||||
|
||||
|
||||
astar : (position, position -> F64), (position -> Set position), position, Model position -> [Err [KeyNotFound], Ok (List position)] | position has Eq
|
||||
astar : (position, position -> F64), (position -> Set position), position, Model position -> [Err [KeyNotFound], Ok (List position)] | position has Hash & Eq
|
||||
astar = \costFn, moveFn, goal, model ->
|
||||
when cheapestOpen (\position -> costFn goal position) model is
|
||||
Err _ ->
|
||||
|
@ -491,12 +491,12 @@ fn load_astar() {
|
||||
expect_types(
|
||||
loaded_module,
|
||||
hashmap! {
|
||||
"findPath" => "{ costFunction : position, position -> F64, end : position, moveFunction : position -> Set position, start : position } -> Result (List position) [KeyNotFound] | position has Eq",
|
||||
"initialModel" => "position -> Model position",
|
||||
"reconstructPath" => "Dict position position, position -> List position | position has Eq",
|
||||
"updateCost" => "position, position, Model position -> Model position | position has Eq",
|
||||
"cheapestOpen" => "(position -> F64), Model position -> Result position [KeyNotFound] | position has Eq",
|
||||
"astar" => "(position, position -> F64), (position -> Set position), position, Model position -> [Err [KeyNotFound], Ok (List position)] | position has Eq",
|
||||
"findPath" => "{ costFunction : position, position -> F64, end : position, moveFunction : position -> Set position, start : position } -> Result (List position) [KeyNotFound] | position has Hash & Eq",
|
||||
"initialModel" => "position -> Model position | position has Hash & Eq",
|
||||
"reconstructPath" => "Dict position position, position -> List position | position has Hash & Eq",
|
||||
"updateCost" => "position, position, Model position -> Model position | position has Hash & Eq",
|
||||
"cheapestOpen" => "(position -> F64), Model position -> Result position [KeyNotFound] | position has Hash & Eq",
|
||||
"astar" => "(position, position -> F64), (position -> Set position), position, Model position -> [Err [KeyNotFound], Ok (List position)] | position has Hash & Eq",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -3473,7 +3473,7 @@ mod solve_expr {
|
||||
Dict.insert
|
||||
"#
|
||||
),
|
||||
"Dict k v, k, v -> Dict k v | k has Eq",
|
||||
"Dict k v, k, v -> Dict k v | k has Hash & Eq",
|
||||
);
|
||||
}
|
||||
|
||||
@ -3734,7 +3734,7 @@ mod solve_expr {
|
||||
infer_eq_without_problem(
|
||||
indoc!(
|
||||
r#"
|
||||
reconstructPath : Dict position position, position -> List position | position has Eq
|
||||
reconstructPath : Dict position position, position -> List position | position has Hash & Eq
|
||||
reconstructPath = \cameFrom, goal ->
|
||||
when Dict.get cameFrom goal is
|
||||
Err KeyNotFound ->
|
||||
@ -3746,7 +3746,7 @@ mod solve_expr {
|
||||
reconstructPath
|
||||
"#
|
||||
),
|
||||
"Dict position position, position -> List position | position has Eq",
|
||||
"Dict position position, position -> List position | position has Hash & Eq",
|
||||
);
|
||||
}
|
||||
|
||||
@ -3781,7 +3781,7 @@ mod solve_expr {
|
||||
|
||||
Model position : { openSet : Set position }
|
||||
|
||||
cheapestOpen : Model position -> Result position [KeyNotFound] | position has Eq
|
||||
cheapestOpen : Model position -> Result position [KeyNotFound] | position has Hash & Eq
|
||||
cheapestOpen = \model ->
|
||||
|
||||
folder = \resSmallestSoFar, position ->
|
||||
@ -3796,14 +3796,14 @@ mod solve_expr {
|
||||
Set.walk model.openSet (Ok { position: boom {}, cost: 0.0 }) folder
|
||||
|> Result.map (\x -> x.position)
|
||||
|
||||
astar : Model position -> Result position [KeyNotFound] | position has Eq
|
||||
astar : Model position -> Result position [KeyNotFound] | position has Hash & Eq
|
||||
astar = \model -> cheapestOpen model
|
||||
|
||||
main =
|
||||
astar
|
||||
"#
|
||||
),
|
||||
"Model position -> Result position [KeyNotFound] | position has Eq",
|
||||
"Model position -> Result position [KeyNotFound] | position has Hash & Eq",
|
||||
);
|
||||
}
|
||||
|
||||
@ -4445,7 +4445,7 @@ mod solve_expr {
|
||||
|
||||
Key k : Num k
|
||||
|
||||
removeHelpEQGT : Key k, RBTree (Key k) v -> RBTree (Key k) v | k has Eq
|
||||
removeHelpEQGT : Key k, RBTree (Key k) v -> RBTree (Key k) v | k has Hash & Eq
|
||||
removeHelpEQGT = \targetKey, dict ->
|
||||
when dict is
|
||||
Node color key value left right ->
|
||||
@ -4559,7 +4559,7 @@ mod solve_expr {
|
||||
_ ->
|
||||
Empty
|
||||
|
||||
removeHelp : Key k, RBTree (Key k) v -> RBTree (Key k) v | k has Eq
|
||||
removeHelp : Key k, RBTree (Key k) v -> RBTree (Key k) v | k has Hash & Eq
|
||||
removeHelp = \targetKey, dict ->
|
||||
when dict is
|
||||
Empty ->
|
||||
@ -4647,7 +4647,7 @@ mod solve_expr {
|
||||
|
||||
RBTree k v : [Node NodeColor k v (RBTree k v) (RBTree k v), Empty]
|
||||
|
||||
removeHelp : Num k, RBTree (Num k) v -> RBTree (Num k) v | k has Eq
|
||||
removeHelp : Num k, RBTree (Num k) v -> RBTree (Num k) v | k has Hash & Eq
|
||||
removeHelp = \targetKey, dict ->
|
||||
when dict is
|
||||
Empty ->
|
||||
@ -4682,7 +4682,7 @@ mod solve_expr {
|
||||
|
||||
removeHelpPrepEQGT : Key k, RBTree (Key k) v, NodeColor, (Key k), v, RBTree (Key k) v, RBTree (Key k) v -> RBTree (Key k) v
|
||||
|
||||
removeHelpEQGT : Key k, RBTree (Key k) v -> RBTree (Key k) v | k has Eq
|
||||
removeHelpEQGT : Key k, RBTree (Key k) v -> RBTree (Key k) v | k has Hash & Eq
|
||||
removeHelpEQGT = \targetKey, dict ->
|
||||
when dict is
|
||||
Node color key value left right ->
|
||||
@ -8270,7 +8270,7 @@ mod solve_expr {
|
||||
r#"
|
||||
app "test" provides [top] to "./platform"
|
||||
|
||||
MDict u := (List u) | u has Eq
|
||||
MDict u := (List u) | u has Hash & Eq
|
||||
|
||||
bot : MDict k -> MDict k
|
||||
bot = \@MDict data ->
|
||||
@ -8281,7 +8281,7 @@ mod solve_expr {
|
||||
top = \x -> bot x
|
||||
"#
|
||||
),
|
||||
"MDict v -> MDict v | v has Eq",
|
||||
"MDict v -> MDict v | v has Hash & Eq",
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -384,7 +384,7 @@ fn insert_all() {
|
||||
|
||||
#[test]
|
||||
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
|
||||
fn insert_all_prefer_first() {
|
||||
fn insert_all_prefer_second() {
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
@ -396,7 +396,7 @@ fn insert_all_prefer_first() {
|
||||
Dict.values myDict
|
||||
"#
|
||||
),
|
||||
RocList::from_slice(&[100]),
|
||||
RocList::from_slice(&[200]),
|
||||
RocList<i64>
|
||||
);
|
||||
}
|
||||
|
@ -62,16 +62,6 @@ fn single_to_list() {
|
||||
RocList::from_slice(&[1]),
|
||||
RocList<i64>
|
||||
);
|
||||
|
||||
assert_evals_to!(
|
||||
indoc!(
|
||||
r#"
|
||||
Set.toList (Set.single 1.0)
|
||||
"#
|
||||
),
|
||||
RocList::from_slice(&[1.0]),
|
||||
RocList<f64>
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user