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

Add typing rules for functions/calls #298

Merged
merged 3 commits into from
Jan 31, 2024
Merged
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
2 changes: 1 addition & 1 deletion tree_unique_args/src/function_inlining.egg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
(rule
(
(= call (Call f arg))
(Function f out)
(Function f out in-ty out-ty)
(ExprIsValid call)
)
(
Expand Down
8 changes: 5 additions & 3 deletions tree_unique_args/src/purity_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn purity_rule_for_ctor(ctor: Constructor) -> Option<String> {
pub(crate) fn purity_analysis_rules() -> Vec<String> {
ESort::iter()
.map(|sort| "(relation *IsPure (*))".replace('*', sort.name()))
.chain(iter::once( "(relation FunctionIsPure (IdSort))\n(rule ((Function id out) (ExprIsPure out)) ((FunctionIsPure id)):ruleset always-run)".to_string()))
.chain(iter::once( "(relation FunctionIsPure (IdSort))\n(rule ((Function id out in-ty out-ty) (ExprIsPure out)) ((FunctionIsPure id)):ruleset always-run)".to_string()))
.chain(Constructor::iter().filter_map(purity_rule_for_ctor))
.collect::<Vec<_>>()
}
Expand Down Expand Up @@ -112,7 +112,8 @@ fn test_purity_function() -> Result<(), egglog::Error> {
(Function id_fun1
(Add
(Get (Arg id_fun1) 0)
(Get (Arg id_fun1) 0))))
(Get (Arg id_fun1) 0))
(TupleT (TCons (IntT) (TNil))) (IntT)))
;; f2 is impure
(let f2
(Function id_fun2
Expand All @@ -123,7 +124,8 @@ fn test_purity_function() -> Result<(), egglog::Error> {
(Add
(Get (Arg id_fun2) 0)
(Get (Arg id_fun2) 0))))
1)))
1)
(TupleT (TCons (IntT) (TNil))) (IntT)))
(let pure-loop
(Loop id1
(All id-outer (Parallel) (Pair (Num id-outer 0) (Num id-outer 0)))
Expand Down
4 changes: 2 additions & 2 deletions tree_unique_args/src/schema.egg
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
; Functions
; ==========================

; f output
(function Function (IdSort Expr) Expr)
; f output in-type out-type
(function Function (IdSort Expr Type Type) Expr)

; f arg
(function Call (IdSort Expr) Expr)
Expand Down
21 changes: 21 additions & 0 deletions tree_unique_args/src/type_analysis.egg
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,25 @@
(!= (TypeList-length lst) 2)
)
((panic "Loop did not get two arguments (predicate and output)"))
:ruleset type-analysis)

; Functions
(rule ((= f (Function id body in-ty out-ty)))
((HasType (Arg id) in-ty))
:ruleset type-analysis)

(rule (
Copy link
Member

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

(= f (Function id body in-ty out-ty))
(HasType body out-ty)
)
((HasType f (FuncT in-ty out-ty)))
:ruleset type-analysis)

(rule (
Copy link
Member

Choose a reason for hiding this comment

The 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

(= 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)
24 changes: 24 additions & 0 deletions tree_unique_args/src/type_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,27 @@ 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)
}
Copy link
Member

Choose a reason for hiding this comment

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

Add a test or two that test erroring cases

Loading