chapel-lang / chapel

a Productive Parallel Programming Language
https://chapel-lang.org
Other
1.79k stars 420 forks source link

More expressive generic homogenous tuple arguments #9374

Open BryantLam opened 6 years ago

BryantLam commented 6 years ago

Suggestions for more expressive generics. Today's implementation as of 1.17 with query expressions is shown alongside the desired constrained generics for homogeneous tuples.

Source Code:

module QueriedTuples {
  proc tfun1(t1, t2) where (isHomogeneousTuple(t1) && isHomogeneousTuple(t2) &&
                            (t1[1].type == t2[1].type) &&
                            (t1.size == 3) && (t2.size == 5) {
    writeln(t1, " ", t2);
  }
  // Suggested alternative:
  // proc tfun1(t1: 3*?t, t2: 5*t) { ... }

  proc tfun2(t1: ?t, t2: t) where (isHomogeneousTuple(t1) &&
                                   isHomogeneousTuple(t2) &&
                                   t1[1].type == int) {
    writeln(t1, " ", t2);
  }
  // Suggested alternative:
  // proc tfun2(t1: ?n*int, t2: n*int) { ... }

  proc main() {
    tfun1( (1, 1, 1), (2, 2, 2, 2, 2) );
    tfun2( (1, 1, 1), (2, 2, 2) );
  }
}
bradcray commented 6 years ago

I think this makes a lot of sense. There are several formal type query modes that I think we'd like to support in addition to the ones we have today (e.g., proc foo(A: [?lo..?hi] real) to accept a 1D array of reals with any low and high bounds).