Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding update and extend type support for data types #1255

Open
wants to merge 3 commits into
base: horizon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/arr/compiler/type-check.arr
Original file line number Diff line number Diff line change
Expand Up @@ -1858,8 +1858,8 @@ end

fun synthesis-extend(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields :: List<A.Member>, context :: Context) -> TypingResult:
collect-members(fields, false, context).typing-bind(lam(new-members, shadow context):
instantiate-object-type(obj-type, context).typing-bind(lam(shadow obj-type, shadow context):
cases(Type) obj-type:
instantiate-object-type(obj-type, context).typing-bind(lam(instantiated-obj-type, shadow context):
cases(Type) instantiated-obj-type:
| t-record(t-fields, _, inferred) =>
final-fields = new-members.fold-keys(lam(key, final-fields):
final-fields.set(key, new-members.get-value(key))
Expand All @@ -1868,21 +1868,24 @@ fun synthesis-extend(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields ::
| t-existential(_, l, _) =>
typing-error([list: C.unable-to-infer(l)])
| else =>
typing-error([list: C.incorrect-type-expression(tostring(obj-type), obj-type.l, "an object type", update-loc, obj)])
shadow context = new-members.fold-keys(lam(key, shadow context):
context.add-field-constraint(instantiated-obj-type, key, new-members.get-value(key))
end, context)
typing-result(A.s-extend(update-loc, obj, fields), obj-type, context)
end
end)
end)
end

fun synthesis-update(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields :: List<A.Member>, context :: Context) -> TypingResult:
collect-members(fields, false, context).typing-bind(lam(new-members, shadow context):
instantiate-object-type(obj-type, context).typing-bind(lam(shadow obj-type, shadow context):
instantiate-object-type(obj-type, context).typing-bind(lam(instantiated-obj-type, shadow context):
cases(Type) obj-type:
| t-record(t-fields, _, inferred) =>
foldr-fold-result(lam(key, shadow context, final-fields):
cases(Option<Type>) t-fields.get(key):
| none =>
fold-errors([list: C.object-missing-field(key, tostring(obj-type), obj-type.l, update-loc)])
fold-errors([list: C.object-missing-field(key, tostring(instantiated-obj-type), instantiated-obj-type.l, update-loc)])
| some(old-type) =>
cases(Type) old-type:
| t-ref(onto, l, ref-inferred) =>
Expand All @@ -1898,7 +1901,11 @@ fun synthesis-update(update-loc :: Loc, obj :: Expr, obj-type :: Type, fields ::
| t-existential(_, l, _) =>
typing-error([list: C.unable-to-infer(l)])
| else =>
typing-error([list: C.incorrect-type-expression(tostring(obj-type), obj-type.l, "an object type", update-loc, obj)])
shadow context = new-members.fold-keys(lam(key, shadow context):
member-type = new-members.get-value(key)
context.add-field-constraint(instantiated-obj-type, key, t-ref(member-type, member-type.l, false))
end, context)
typing-result(A.s-update(update-loc, obj, fields), obj-type, context)
end
end)
end)
Expand Down
5 changes: 5 additions & 0 deletions tests/type-check/bad/data-as-object-extend-missing.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{a: "a"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these test cases named correctly? This doesn't seem to be a missing field, it's a wrong type. In general, could you add a comment to each bad test, above the failing expression, explaining what the error should be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops I flipped two of the test files for both update and extend.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks almost right...but you forgot to fix this specific file. After that, I'm good to merge this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed it now.

5 changes: 5 additions & 0 deletions tests/type-check/bad/data-as-object-extend-ref.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{c: "a"}
5 changes: 5 additions & 0 deletions tests/type-check/bad/data-as-object-extend-wrong-type.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{b: "b"}
5 changes: 5 additions & 0 deletions tests/type-check/bad/data-as-object-update-missing.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{b: 3}
5 changes: 5 additions & 0 deletions tests/type-check/bad/data-as-object-update-non-ref.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{c: 3}
5 changes: 5 additions & 0 deletions tests/type-check/bad/data-as-object-update-wrong.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{b: 3}
6 changes: 6 additions & 0 deletions tests/type-check/good/data-as-object.arr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
data Foo:
| bar(a :: Number, ref b :: String)
end

bar(1, "a").{a: 3}
bar(1, "b")!{b: "a"}