Skip to content

Commit

Permalink
feat: make lattice_fold and lattice_reduce consistent with fold/reduce (
Browse files Browse the repository at this point in the history
  • Loading branch information
zzlk authored Sep 13, 2023
1 parent 51a200a commit 1ce5f01
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
error: `lattice_reduce` should have exactly 1 generic type arguments, actually has 0.
--> tests/compile-fail/surface_flow_props_cumul_warn_cumul.rs:9:33
error: `lattice_fold` should have exactly 0 generic type arguments, actually has 1.
--> tests/compile-fail/surface_flow_props_cumul_warn_cumul.rs:7:31
|
9 | -> lattice_reduce::<'static>()
| ^^^^^^^
7 | -> lattice_fold::<'static, SetUnionHashSet<_>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: `lattice_fold` should have exactly 0 generic type arguments, actually has 1.
--> tests/compile-fail/surface_flow_props_cumul_warn_cumul.rs:8:31
|
8 | -> lattice_fold::<'static, SetUnionHashSet<_>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected 1 argument(s), found 0
--> tests/compile-fail/surface_flow_props_cumul_warn_cumul.rs:7:16
|
7 | -> lattice_fold::<'static, SetUnionHashSet<_>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected 1 argument(s), found 0
--> tests/compile-fail/surface_flow_props_cumul_warn_cumul.rs:8:16
|
8 | -> lattice_fold::<'static, SetUnionHashSet<_>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: warning test
--> tests/compile-fail/surface_flow_props_cumul_warn_cumul.rs:14:5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
error: `lattice_reduce` should have exactly 1 generic type arguments, actually has 0.
--> tests/compile-fail/surface_flow_props_cumul_warn_none.rs:10:33
|
10 | -> lattice_reduce::<'static>()
| ^^^^^^^
error: `lattice_fold` should have exactly 0 generic type arguments, actually has 1.
--> tests/compile-fail/surface_flow_props_cumul_warn_none.rs:8:31
|
8 | -> lattice_fold::<'static, SetUnionHashSet<_>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected 1 argument(s), found 0
--> tests/compile-fail/surface_flow_props_cumul_warn_none.rs:8:16
|
8 | -> lattice_fold::<'static, SetUnionHashSet<_>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: warning test
--> tests/compile-fail/surface_flow_props_cumul_warn_none.rs:15:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hydroflow::hydroflow_syntax;
fn main() {
let mut df = hydroflow_syntax! {
source_iter([1,2,3,4,5])
-> lattice_fold::<'static, usize>()
-> lattice_fold::<'static, usize>(Default::default())
-> for_each(|x| println!("Least upper bound: {:?}", x));
};
df.run_available();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hydroflow::hydroflow_syntax;
fn main() {
let mut df = hydroflow_syntax! {
source_iter([1,2,3,4,5])
-> lattice_fold::<'static>()
-> lattice_fold::<hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
-> for_each(|x| println!("Least upper bound: {:?}", x));
};
df.run_available();
Expand Down
11 changes: 11 additions & 0 deletions hydroflow/tests/compile-fail/surface_lattice_fold_noarg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: `lattice_fold` should have exactly 0 generic type arguments, actually has 1.
--> tests/compile-fail/surface_lattice_fold_noarg.rs:6:31
|
6 | -> lattice_fold::<hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: expected 1 argument(s), found 0
--> tests/compile-fail/surface_lattice_fold_noarg.rs:6:16
|
6 | -> lattice_fold::<hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hydroflow::hydroflow_syntax;
fn main() {
let mut df = hydroflow_syntax! {
source_iter([1,2,3,4,5])
-> lattice_fold::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
-> lattice_fold::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>(hydroflow::lattices::set_union::SetUnionHashSet::<u32>::default())
-> for_each(|x| println!("Least upper bound: {:?}", x));
};
df.run_available();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
warning: `lattice_fold` expects lattice flow input, has sequential input. This may be an error in the future.
--> tests/compile-fail/surface_lattice_fold_wronggeneric.rs:6:16
error: `lattice_fold` should have exactly 0 generic type arguments, actually has 1.
--> tests/compile-fail/surface_lattice_fold_wronggeneric.rs:6:31
|
6 | -> lattice_fold::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0308]: mismatched types
--> tests/compile-fail/surface_lattice_fold_wronggeneric.rs:6:16
|
4 | let mut df = hydroflow_syntax! {
| __________________-
5 | | source_iter([1,2,3,4,5])
6 | | -> lattice_fold::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `SetUnion<_>`, found integer
7 | | -> for_each(|x| println!("Least upper bound: {:?}", x));
8 | | };
| |_____- arguments to this function are incorrect
|
= note: expected struct `SetUnion<_>`
found type `{integer}`
note: method defined here
--> $WORKSPACE/lattices/src/lib.rs
|
| fn merge(&mut self, other: Other) -> bool;
| ^^^^^
help: try wrapping the expression in `hydroflow::lattices::set_union::SetUnion`
|
6 | -> hydroflow::lattices::set_union::SetUnion(lattice_fold::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>())
| +++++++++++++++++++++++++++++++++++++++++ +
6 | ... -> lattice_fold::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>(hydroflow::lattices::set_union::SetUnionHashSet::<...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 changes: 0 additions & 10 deletions hydroflow/tests/compile-fail/surface_lattice_reduce_nogeneric.rs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
warning: `lattice_reduce` expects lattice flow input, has sequential input. This may be an error in the future.
--> tests/compile-fail/surface_lattice_reduce_wronggeneric.rs:6:16
error: `lattice_reduce` should have exactly 0 generic type arguments, actually has 1.
--> tests/compile-fail/surface_lattice_reduce_wronggeneric.rs:6:33
|
6 | -> lattice_reduce::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0271]: expected `Drain<'_, {integer}>` to be an iterator that yields `SetUnion<HashSet<u32>>`, but it yields `{integer}`
--> tests/compile-fail/surface_lattice_reduce_wronggeneric.rs:5:9
|
5 | source_iter([1,2,3,4,5])
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `SetUnion<HashSet<u32>>`, found integer
6 | -> lattice_reduce::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
| ---------------------------------------------------- required by a bound introduced by this call
|
= note: expected struct `SetUnion<HashSet<u32>>`
found type `{integer}`
note: required by a bound in `check_inputs`
--> tests/compile-fail/surface_lattice_reduce_wronggeneric.rs:6:42
|
6 | -> lattice_reduce::<'static, hydroflow::lattices::set_union::SetUnionHashSet<u32>>()
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `check_inputs`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ digraph {
fillcolor="#dddddd"
style=filled
label = "sg_2v1\nstratum 1"
n9v1 [label="(n9v1) lattice_reduce::<'static, SetUnionHashSet<_>>()", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"]
n9v1 [label="(n9v1) lattice_reduce()", fontname=Monaco, shape=invhouse, style = filled, color = "#0022ff", fontcolor = "#ffffff"]
n10v1 [label="(n10v1) for_each(|s| println!(\"cumul {:?}\", s))", fontname=Monaco, shape=house, style = filled, color = "#ffff00"]
n9v1 -> n10v1 [style=bold]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ subgraph sg_1v1 ["sg_1v1 stratum 0"]
end
end
subgraph sg_2v1 ["sg_2v1 stratum 1"]
9v1[\"(9v1) <code>lattice_reduce::&lt;'static, SetUnionHashSet&lt;_&gt;&gt;()</code>"/]:::pullClass
9v1[\"(9v1) <code>lattice_reduce()</code>"/]:::pullClass
10v1[/"(10v1) <code>for_each(|s| println!(&quot;cumul {:?}&quot;, s))</code>"\]:::pushClass
9v1==>10v1
end
Expand Down
2 changes: 1 addition & 1 deletion hydroflow/tests/surface_flow_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn test_basic() {

my_tee
-> map(|SetUnion(SingletonSet(x))| SetUnionHashSet::new_from([x]))
-> lattice_reduce::<'static, SetUnionHashSet<_>>()
-> lattice_reduce()
-> for_each(|s| println!("cumul {:?}", s));
};
hf.run_available();
Expand Down
2 changes: 1 addition & 1 deletion hydroflow/tests/surface_lattice_fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn test_basic() {
let mut df = hydroflow_syntax! {
source_iter([1,2,3,4,5])
-> map(Max::new)
-> lattice_fold::<'static, Max<u32>>()
-> lattice_fold::<'static>(Max::<u32>::new(0))
-> for_each(|x: Max<u32>| println!("Least upper bound: {:?}", x));
};
df.run_available();
Expand Down
2 changes: 1 addition & 1 deletion hydroflow/tests/surface_lattice_reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn test_basic() {
let mut df = hydroflow_syntax! {
source_iter([1,2,3,4,5])
-> map(Max::new)
-> lattice_reduce::<'static, Max<u32>>()
-> lattice_reduce()
-> for_each(|x: Max<u32>| println!("Least upper bound: {:?}", x));
};
df.run_available();
Expand Down
41 changes: 19 additions & 22 deletions hydroflow_lang/src/graph/ops/lattice_fold.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
use syn::parse_quote_spanned;
use syn::spanned::Spanned;

use super::{
DelayType, FlowProperties, FlowPropertyVal, OpInstGenerics, OperatorCategory,
OperatorConstraints, OperatorInstance, WriteContextArgs, LATTICE_FOLD_REDUCE_FLOW_PROP_FN,
RANGE_1,
DelayType, FlowProperties, FlowPropertyVal, OperatorCategory, OperatorConstraints,
OperatorInstance, WriteContextArgs, LATTICE_FOLD_REDUCE_FLOW_PROP_FN, RANGE_0, RANGE_1,
};

/// > 1 input stream, 1 output stream
///
/// > Generic parameters: A `Lattice` type, must implement [`Merge<Self>`](https://hydro-project.github.io/hydroflow/doc/lattices/trait.Merge.html)
/// type.
///
/// A specialized operator for merging lattices together into a accumulated value. Like [`fold()`](#fold)
/// but specialized for lattice types. `lattice_fold::<MyLattice>()` is equivalent to `fold(MyLattice::default(), hydroflow::lattices::Merge::merge_owned)`.
/// but specialized for lattice types. `lattice_fold::<MyLattice>()` is equivalent to `fold(MyLattice::default(), hydroflow::lattices::Merge::merge)`.
///
/// `lattice_fold` can also be provided with one generic lifetime persistence argument, either
/// `'tick` or `'static`, to specify how data persists. With `'tick`, values will only be collected
Expand All @@ -22,12 +17,15 @@ use super::{
/// defaults to `'tick`.
///
/// `lattice_fold` is differentiated from `lattice_reduce` in that `lattice_fold` can accumulate into a different type from its input.
/// But it also means that the accumulating type must implement `Default`
/// But it also means that the accumulating type must have a sensible default value
///
/// ```hydroflow
/// source_iter([hydroflow::lattices::set_union::SetUnionSingletonSet::new_from(7)])
/// -> lattice_fold::<'static, hydroflow::lattices::set_union::SetUnionHashSet<usize>>()
/// -> assert_eq([hydroflow::lattices::set_union::SetUnionHashSet::new_from([7])]);
/// use hydroflow::lattices::set_union::SetUnionSingletonSet;
/// use hydroflow::lattices::set_union::SetUnionHashSet;
///
/// source_iter([SetUnionSingletonSet::new_from(7)])
/// -> lattice_fold(SetUnionHashSet::<usize>::default())
/// -> assert_eq([SetUnionHashSet::new_from([7])]);
/// ```
pub const LATTICE_FOLD: OperatorConstraints = OperatorConstraints {
name: "lattice_fold",
Expand All @@ -36,9 +34,9 @@ pub const LATTICE_FOLD: OperatorConstraints = OperatorConstraints {
soft_range_inn: RANGE_1,
hard_range_out: RANGE_1,
soft_range_out: RANGE_1,
num_args: 0,
num_args: 1,
persistence_args: &(0..=1),
type_args: RANGE_1,
type_args: RANGE_0,
is_external_input: false,
ports_inn: None,
ports_out: None,
Expand All @@ -52,20 +50,17 @@ pub const LATTICE_FOLD: OperatorConstraints = OperatorConstraints {
write_fn: |wc @ &WriteContextArgs {
root,
is_pull,
op_inst:
op_inst @ OperatorInstance {
generics: OpInstGenerics { type_args, .. },
..
},
op_span,
op_inst: op_inst @ OperatorInstance { arguments, .. },
..
},
diagnostics| {
assert!(is_pull);

let lat_type = &type_args[0];
let first_arg = &arguments[0];

let arguments = parse_quote_spanned! {lat_type.span()=> // Uses `lat_type.span()`!
<#lat_type as ::std::default::Default>::default(), #root::lattices::Merge::merge
let arguments = parse_quote_spanned! {op_span=>
#first_arg, #root::lattices::Merge::merge
};

let wc = WriteContextArgs {
Expand All @@ -76,6 +71,8 @@ pub const LATTICE_FOLD: OperatorConstraints = OperatorConstraints {
..wc.clone()
};

// Can't do better type checking here because we need heavy type inference
// to support different accumulator and input types.
(super::fold::FOLD.write_fn)(&wc, diagnostics)
},
};
Loading

0 comments on commit 1ce5f01

Please sign in to comment.