felix-lang / felix

The Felix Programming Language
Other
805 stars 43 forks source link

Iterate thru compact linear type #119

Open skaller opened 6 years ago

skaller commented 6 years ago

At present you can loop through an integer slice:

for i in 1..10 do ...

and an array with a compact linear index:

  var x = 1,2,3,4;
  var y = (x,x,x,x);
  var z = y :>> (int ^ 16);
  var a = z :>> (int ^ ( 2 * 8));
  for i in a call println$ i; 

but there is no way to iterate through a compact linear type.

skaller commented 6 years ago

Partial implementation: this works with new syntax and classes:

instance Eq[5] {
  fun == (x:5,y:5) => caseno x == caseno y;
}
instance Tord[5] {
  fun < (x:5,y:5) => caseno x < caseno y;
}
instance BoundedTordForward[5] {
  fun maxval () => `4:5;
}

instance BoundedTordBidirectional[5] {
  fun minval () => `2:5;
}

instance BoundedTordRandom[5] {
  fun + (x:5,y:5)  => (caseno x + caseno y) :>> 5;
  fun - (x:5,y:5)  => (caseno x - caseno y) :>> 5;
  fun one () => `1:5;
  fun zero () => `0:5;
}

var x = ..[5];
for i in x perform println$ i._strr;

but needs to be generalised so the instance applies to UNITSUM kind, which doesn't yet work.