Skip to content

Commit

Permalink
Fix some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ayazhafiz committed Jan 11, 2025
1 parent ee3c71d commit e5d0b77
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 112 deletions.
1 change: 0 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ ROC_TRACE_COMPACTION = "0"
ROC_PRINT_UNIFICATIONS_DERIVED = "0"
ROC_PRINT_MISMATCHES = "0"
ROC_PRINT_FIXPOINT_FIXING = "0"
ROC_VERIFY_RIGID_LET_GENERALIZED = "0"
ROC_VERIFY_OCCURS_ONE_RECURSION = "0"
ROC_CHECK_MONO_IR = "0"
ROC_PRINT_IR_AFTER_SPECIALIZATION = "0"
Expand Down
134 changes: 67 additions & 67 deletions crates/cli/tests/benchmarks/AStar.roc
Original file line number Diff line number Diff line change
@@ -1,107 +1,107 @@
module [find_path, Model, initial_model, cheapest_open, reconstruct_path]
module [findPath, Model, initialModel, cheapestOpen, reconstructPath]

import Quicksort

find_path = \cost_fn, move_fn, start, end ->
astar(cost_fn, move_fn, end, initial_model(start))
findPath = \costFn, moveFn, start, end ->
astar costFn moveFn end (initialModel start)

Model position : {
evaluated : Set position,
open_set : Set position,
openSet : Set position,
costs : Dict position F64,
came_from : Dict position position,
cameFrom : Dict position position,
} where position implements Hash & Eq

initial_model : position -> Model position where position implements Hash & Eq
initial_model = \start -> {
evaluated: Set.empty({}),
open_set: Set.single(start),
costs: Dict.single(start, 0),
came_from: Dict.empty({}),
initialModel : position -> Model position where position implements Hash & Eq
initialModel = \start -> {
evaluated: Set.empty {},
openSet: Set.single start,
costs: Dict.single start 0,
cameFrom: Dict.empty {},
}

cheapest_open : (position -> F64), Model position -> Result position {} where position implements Hash & Eq
cheapest_open = \cost_fn, model ->
model.open_set
|> Set.to_list
|> List.keep_oks(
\position ->
when Dict.get(model.costs, position) is
Err(_) -> Err({})
Ok(cost) -> Ok({ cost: cost + cost_fn(position), position }),
)
|> Quicksort.sort_by(.cost)
cheapestOpen : (position -> F64), Model position -> Result position {} where position implements Hash & Eq
cheapestOpen = \costFn, model ->
model.openSet
|> Set.toList
|> List.keepOks
(\position ->
when Dict.get model.costs position is
Err _ -> Err {}
Ok cost -> Ok { cost: cost + costFn position, position }
)
|> Quicksort.sortBy .cost
|> List.first
|> Result.map(.position)
|> Result.map_err(\_ -> {})
|> Result.map .position
|> Result.mapErr (\_ -> {})

reconstruct_path : Dict position position, position -> List position where position implements Hash & Eq
reconstruct_path = \came_from, goal ->
when Dict.get(came_from, goal) is
Err(_) -> []
Ok(next) -> List.append(reconstruct_path(came_from, next), goal)
reconstructPath : Dict position position, position -> List position where position implements Hash & Eq
reconstructPath = \cameFrom, goal ->
when Dict.get cameFrom goal is
Err _ -> []
Ok next -> List.append (reconstructPath cameFrom next) goal

update_cost : position, position, Model position -> Model position where position implements Hash & Eq
update_cost = \current, neighbor, model ->
new_came_from =
Dict.insert(model.came_from, neighbor, current)
updateCost : position, position, Model position -> Model position where position implements Hash & Eq
updateCost = \current, neighbor, model ->
newCameFrom =
Dict.insert model.cameFrom neighbor current

new_costs =
Dict.insert(model.costs, neighbor, distance_to)
newCosts =
Dict.insert model.costs neighbor distanceTo

distance_to =
reconstruct_path(new_came_from, neighbor)
distanceTo =
reconstructPath newCameFrom neighbor
|> List.len
|> Num.to_frac
|> Num.toFrac

new_model =
newModel =
{ model &
costs: new_costs,
came_from: new_came_from,
costs: newCosts,
cameFrom: newCameFrom,
}

when Dict.get(model.costs, neighbor) is
Err(_) ->
new_model
when Dict.get model.costs neighbor is
Err _ ->
newModel

Ok(previous_distance) ->
if distance_to < previous_distance then
new_model
Ok previousDistance ->
if distanceTo < previousDistance then
newModel
else
model

astar : (position, position -> F64), (position -> Set position), position, Model position -> Result (List position) {} where position implements Hash & Eq
astar = \cost_fn, move_fn, goal, model ->
when cheapest_open(\source -> cost_fn(source, goal), model) is
Err({}) -> Err({})
Ok(current) ->
astar = \costFn, moveFn, goal, model ->
when cheapestOpen (\source -> costFn source goal) model is
Err {} -> Err {}
Ok current ->
if current == goal then
Ok(reconstruct_path(model.came_from, goal))
Ok (reconstructPath model.cameFrom goal)
else
model_popped =
modelPopped =
{ model &
open_set: Set.remove(model.open_set, current),
evaluated: Set.insert(model.evaluated, current),
openSet: Set.remove model.openSet current,
evaluated: Set.insert model.evaluated current,
}

neighbors =
move_fn(current)
moveFn current

new_neighbors =
Set.difference(neighbors, model_popped.evaluated)
newNeighbors =
Set.difference neighbors modelPopped.evaluated

model_with_neighbors : Model position
model_with_neighbors =
model_popped
|> &open_set(Set.union(model_popped.open_set, new_neighbors))
modelWithNeighbors : Model _
modelWithNeighbors =
modelPopped
|> &openSet (Set.union modelPopped.openSet newNeighbors)

walker : Model position, position -> Model position
walker = \amodel, n -> update_cost(current, n, amodel)
walker : Model _, _ -> Model _
walker = \amodel, n -> updateCost current n amodel

model_with_costs =
Set.walk(new_neighbors, model_with_neighbors, walker)
modelWithCosts =
Set.walk newNeighbors modelWithNeighbors walker

astar(cost_fn, move_fn, goal, model_with_costs)
astar costFn moveFn goal modelWithCosts

# takeStep = \moveFn, _goal, model, current ->
# modelPopped =
Expand Down
34 changes: 8 additions & 26 deletions crates/compiler/solve/tests/solve_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2510,13 +2510,15 @@ mod solve_expr {
infer_eq_without_problem(
indoc!(
r"
empty : [Cons a (ConsList a), Nil] as ConsList a
empty = Nil
ConsList a : [Cons a (ConsList a), Nil]
empty
"
empty : ConsList _
empty = Nil
empty
"
),
"ConsList a",
"ConsList *",
);
}

Expand Down Expand Up @@ -3742,7 +3744,7 @@ mod solve_expr {
indoc!(
r"
\rec ->
{ x, y } : { x : I64, y ? Bool }*
{ x, y } : { x : I64, y ? Bool }_
{ x, y ? Bool.false } = rec
{ x, y }
Expand Down Expand Up @@ -3909,26 +3911,6 @@ mod solve_expr {
);
}

#[test]
fn double_named_rigids() {
infer_eq_without_problem(
indoc!(
r#"
app "test" provides [main] to "./platform"
main : List x
main =
empty : List x
empty = []
empty
"#
),
"List x",
);
}

#[test]
fn double_tag_application() {
infer_eq_without_problem(
Expand Down
32 changes: 16 additions & 16 deletions crates/compiler/test_gen/src/gen_primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ fn linked_list_len_1() {
LinkedList a : [Nil, Cons a (LinkedList a)]
one : LinkedList (Int *)
one : LinkedList (Int _)
one = Cons 1 Nil
length : LinkedList a -> Int *
Expand Down Expand Up @@ -690,7 +690,7 @@ fn linked_list_len_twice_1() {
LinkedList a : [Nil, Cons a (LinkedList a)]
one : LinkedList (Int *)
one : LinkedList (Int _)
one = Cons 1 Nil
length : LinkedList a -> Int *
Expand Down Expand Up @@ -718,7 +718,7 @@ fn linked_list_len_3() {
LinkedList a : [Nil, Cons a (LinkedList a)]
three : LinkedList (Int *)
three : LinkedList (Int _)
three = Cons 3 (Cons 2 (Cons 1 Nil))
length : LinkedList a -> Int *
Expand Down Expand Up @@ -747,7 +747,7 @@ fn linked_list_sum_num_a() {
LinkedList a : [Nil, Cons a (LinkedList a)]
three : LinkedList (Int *)
three : LinkedList (Int _)
three = Cons 3 (Cons 2 (Cons 1 Nil))
Expand Down Expand Up @@ -776,7 +776,7 @@ fn linked_list_sum_int() {
LinkedList a : [Nil, Cons a (LinkedList a)]
zero : LinkedList (Int *)
zero : LinkedList (Int _)
zero = Nil
sum : LinkedList (Int a) -> Int a
Expand Down Expand Up @@ -804,7 +804,7 @@ fn linked_list_map() {
LinkedList a : [Nil, Cons a (LinkedList a)]
three : LinkedList (Int *)
three : LinkedList (Int _)
three = Cons 3 (Cons 2 (Cons 1 Nil))
sum : LinkedList (Num a) -> Num a
Expand Down Expand Up @@ -836,7 +836,7 @@ fn when_nested_maybe() {
r"
Maybe a : [Nothing, Just a]
x : Maybe (Maybe (Int a))
x : Maybe (Maybe (Int _))
x = Just (Just 41)
when x is
Expand All @@ -853,7 +853,7 @@ fn when_nested_maybe() {
r"
Maybe a : [Nothing, Just a]
x : Maybe (Maybe (Int *))
x : Maybe (Maybe (Int _))
x = Just Nothing
when x is
Expand All @@ -871,7 +871,7 @@ fn when_nested_maybe() {
r"
Maybe a : [Nothing, Just a]
x : Maybe (Maybe (Int *))
x : Maybe (Maybe (Int _))
x = Nothing
when x is
Expand Down Expand Up @@ -1402,7 +1402,7 @@ fn recursive_function_with_rigid() {
else
1 + foo { count: state.count - 1, x: state.x }
main : Int *
main : Int _
main =
foo { count: 3, x: {} }
"#
Expand Down Expand Up @@ -1517,7 +1517,7 @@ fn rbtree_balance_3() {
balance = \key, left ->
Node key left Empty
main : RedBlackTree (Int *)
main : RedBlackTree (Int _)
main =
balance 0 Empty
"#
Expand Down Expand Up @@ -1696,7 +1696,7 @@ fn nested_pattern_match_two_ways() {
_ -> 3
_ -> 3
main : Int *
main : Int _
main =
when balance Nil is
_ -> 3
Expand All @@ -1719,7 +1719,7 @@ fn nested_pattern_match_two_ways() {
Cons 1 (Cons 1 _) -> 3
_ -> 3
main : Int *
main : Int _
main =
when balance Nil is
_ -> 3
Expand Down Expand Up @@ -1751,7 +1751,7 @@ fn linked_list_guarded_double_pattern_match() {
_ -> 3
_ -> 3
main : Int *
main : Int _
main =
when balance Nil is
_ -> 3
Expand All @@ -1778,7 +1778,7 @@ fn linked_list_double_pattern_match() {
Cons _ (Cons x _) -> x
_ -> 0
main : Int *
main : Int _
main =
foo (Cons 1 (Cons 32 Nil))
"#
Expand Down Expand Up @@ -1886,7 +1886,7 @@ fn wildcard_rigid() {
@Effect inner
main : MyTask {} (Frac *)
main : MyTask {} (Frac _)
main = always {}
"#
),
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/test_gen/src/gen_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn err_type_var_annotation() {
assert_evals_to!(
indoc!(
r"
ok : Result I64 *
ok : Result I64 _
ok = Ok 3
Result.map ok (\x -> x + 1)
Expand Down
Loading

0 comments on commit e5d0b77

Please sign in to comment.