Difference between revisions of "DDC/ClassSystem"
Line 1: | Line 1: | ||
+ | == Regions == |
||
− | == Constant vs Mutable data == |
||
+ | 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: |
||
− | In the section on the [[DDC/EffectSystem|EffectSystem]] we saw how ''region variables'' are used to represent the area of memory in which a data value lies. |
||
+ | |||
+ | <haskell> |
||
+ | 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 |
||
+ | </haskell> |
||
+ | |||
+ | Region variables can be quantified with <hask>forall</hask> 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 <hask>succ</hask>, both <hask>%r1</hask> and <hask>%r2</hask> are quantified, this means that <hask>succ</hask> accepts data from any region and returns a freshly allocated <hask>Int</hask>. |
||
+ | |||
+ | <hask>sameInt</hask> just passes its data though so the same region is on both argument and return types. |
||
+ | |||
+ | <hask>pi</hask> doesn't do any allocation at all, its just a static <hask>Float</hask>, so it doesn't have a <hask>forall</hask>. |
||
+ | |||
+ | == Region classes == |
Revision as of 03:59, 19 March 2008
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
.