-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add typing rules for functions/calls #298
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
(rule | ||
( | ||
(= call (Call f arg)) | ||
(Function f out) | ||
(Function f out in-ty out-ty) | ||
(ExprIsValid call) | ||
) | ||
( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,4 +211,42 @@ | |
(!= (TypeList-length lst) 2) | ||
) | ||
((panic "Loop did not get two arguments (predicate and output)")) | ||
:ruleset type-analysis) | ||
:ruleset type-analysis) | ||
|
||
; Functions | ||
(rule ((= f (Function id body in-ty out-ty))) | ||
((HasType (Arg id) in-ty)) | ||
:ruleset type-analysis) | ||
|
||
(rule ( | ||
(= f (Function id body in-ty out-ty)) | ||
(HasType body out-ty) | ||
) | ||
((HasType f (FuncT in-ty out-ty))) | ||
:ruleset type-analysis) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar: need an error when call type and argument type do not match |
||
(rule ( | ||
(= f (Function id body in-ty out-ty)) | ||
(HasType body ty) | ||
(!= ty out-ty) | ||
) | ||
((panic "Function body had wrong type :(")) | ||
:ruleset type-analysis) | ||
|
||
(rule ( | ||
(= c (Call f-id arg)) | ||
(= f (Function f-id body in-ty out-ty)) | ||
(HasType f (FuncT in-ty out-ty)) | ||
(HasType arg in-ty) | ||
) | ||
((HasType c out-ty)) | ||
:ruleset type-analysis) | ||
|
||
(rule ( | ||
(= c (Call f-id arg)) | ||
(= f (Function f-id body in-ty out-ty)) | ||
(HasType arg ty) | ||
(!= ty in-ty) | ||
) | ||
((panic "Function call had wrong argument type :(")) | ||
:ruleset type-analysis) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,3 +231,62 @@ fn read_write() -> Result<(), egglog::Error> { | |
); | ||
crate::run_test(build, &check) | ||
} | ||
|
||
#[test] | ||
fn func() -> Result<(), egglog::Error> { | ||
let build = " | ||
(let f-id (Id (i64-fresh!))) | ||
(let ctx (Id (i64-fresh!))) | ||
(let f (Function f-id (Switch (Get (Arg f-id) 1) | ||
(Cons (Add (Get (Arg f-id) 0) (Num f-id 4)) | ||
(Cons (Get (Arg f-id) 0) (Nil)))) | ||
(TupleT (TCons (IntT) (TCons (BoolT) (TNil)))) | ||
(IntT))) | ||
(let call (Call f-id (All ctx (Parallel) (Cons (Num ctx 2) (Cons (Boolean ctx true) (Nil)))))) | ||
"; | ||
let check = format!( | ||
" | ||
{SCHED} | ||
(check (HasType call (IntT))) | ||
(check (HasType f (FuncT (TupleT (TCons (IntT) (TCons (BoolT) (TNil)))) (IntT)))) | ||
" | ||
); | ||
crate::run_test(build, &check) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a test or two that test erroring cases |
||
|
||
#[test] | ||
#[should_panic] | ||
fn func_input_type() { | ||
let build = " | ||
(let ctx (Id 0)) | ||
(let f-id (Id 1)) | ||
(let f (Function f-id (Add (Arg f-id) (Num f-id 2)) (IntT) (IntT))) | ||
(let c (Call f-id (Boolean ctx true))) | ||
"; | ||
let check = format!( | ||
" | ||
{SCHED} | ||
" | ||
); | ||
|
||
let _ = crate::run_test(build, &check); | ||
} | ||
|
||
#[test] | ||
#[should_panic] | ||
fn func_output_type() { | ||
let build = " | ||
(let ctx (Id 0)) | ||
(let f-id (Id 1)) | ||
(let f (Function f-id (Add (Arg f-id) (Num f-id 2)) (IntT) (BoolT))) | ||
"; | ||
let check = format!( | ||
" | ||
{SCHED} | ||
" | ||
); | ||
|
||
let _ = crate::run_test(build, &check); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably have a rule that errors when out-ty for the function and out-ty for the body are different