DDC/ClassSystem
Regions
In short, two pieces of data are in different regions if they are never substituted for each other. This property, or lack thereof, is sometimes sometimes called aliasing.
Data type constructors have a region annotation as their first argument, which indicates what region they're in. Due to type elaboration, we usually don't see the region annotations, but we can write them in signatures if we want to:
succ :: forall %r1 %r2. Int %r1 -> Int %r2
succ x = x + 1
sameInt :: forall %r1. Int %r1 -> Int %r1
sameInt x = x
pi :: Float %r1
pi = 3.1415926535
Region variables can be quantified with forall
much like type variables. If a region variable in the return type is quantified it means the region is fresh, ie the data was allocated by the function itself.
Notice that in the type of succ
, both %r1
and %r2
are quantified, this means that succ
accepts data from any region and returns a freshly allocated Int
.
sameInt
just passes its data though so the same region is on both argument and return types.
pi
doesn't do any allocation at all, its just a static Float
, so it doesn't have a forall
.