Difference between revisions of "DDC/DestructiveUpdate"
(6 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | '''This is outdated information'''. The DDC project has moved to [http://discus-lang.org http://discus-lang.org] |
||
+ | |||
+ | |||
+ | |||
== Update of base values == |
== Update of base values == |
||
− | Values can be updated with the <hask>:=</hask> operator, which copies its second argument over its first. |
+ | Values can be updated with the <hask>(:=)</hask> operator, which copies its second argument over its first. |
<haskell> |
<haskell> |
||
Line 14: | Line 18: | ||
== Update of algebraic data == |
== Update of algebraic data == |
||
− | The structure of algebraic data can be modified with the <hask>#=</hask> operator. This replaces the constructor at a particular node with a new one. |
+ | The structure of algebraic data can be modified with the <hask>(#=)</hask> operator. This replaces the constructor at a particular node with a new one. |
We'll use a simple point type as an example. In this definition, <hask>x</hask> and <hask>y</hask> are field names and are ''local'' to the <hask>Point</hask> type. This is different from Haskell, where all field names become functions in the top level scope. |
We'll use a simple point type as an example. In this definition, <hask>x</hask> and <hask>y</hask> are field names and are ''local'' to the <hask>Point</hask> type. This is different from Haskell, where all field names become functions in the top level scope. |
||
Line 39: | Line 43: | ||
</haskell> |
</haskell> |
||
− | Projecting the <hask>x</hask> field gives us a |
+ | Projecting the <hask>x</hask> field gives us a handle to the <hask>Float</hask> object inside <hask>point</hask>. |
<haskell> |
<haskell> |
||
oldX = point.x |
oldX = point.x |
||
Line 45: | Line 49: | ||
</haskell> |
</haskell> |
||
− | + | Binding a literal value allocates a new object. |
|
<haskell> |
<haskell> |
||
newX = 5.0 |
newX = 5.0 |
||
Line 58: | Line 62: | ||
== Reference projections == |
== Reference projections == |
||
− | In Disciple, we don't need to change our data definitions to include <hask>Ref</hask> or <hask>IORef</hask> types before we can update them. References are created on the fly with the reference projection operator <hask>#</hask>. |
+ | In Disciple, we don't need to change our data definitions to include <hask>Ref</hask> or <hask>IORef</hask> types before we can update them. References are created on the fly with the reference projection operator <hask>(#)</hask>. |
− | In the previous example, when we used <hask>#</hask> a reference was created which held a pointer ''into'' the <hask>point</hask> object. |
+ | In the previous example, when we used <hask>(#)</hask> a reference was created which held a pointer ''into'' the <hask>point</hask> object. |
<haskell> |
<haskell> |
||
(point # x) :: Ref Float |
(point # x) :: Ref Float |
||
</haskell> |
</haskell> |
||
− | The |
+ | The <hask>(#=)</hask> operator is just a regular function which has the (simplified) type: |
<haskell> |
<haskell> |
||
(#=) :: forall a. Ref a -> a -> () |
(#=) :: forall a. Ref a -> a -> () |
Latest revision as of 00:46, 24 February 2018
This is outdated information. The DDC project has moved to http://discus-lang.org
Update of base values
Values can be updated with the (:=)
operator, which copies its second argument over its first.
main ()
= do x = 2
out x -- prints '2'
x := 3
out x -- prints '3'
Update of algebraic data
The structure of algebraic data can be modified with the (#=)
operator. This replaces the constructor at a particular node with a new one.
We'll use a simple point type as an example. In this definition, x
and y
are field names and are local to the Point
type. This is different from Haskell, where all field names become functions in the top level scope.
data Point
= Point { x :: Float; y :: Float; }
We'll also define an instance of the Out
type-class, which we're using to print things to stdout
until the implementation of dictionary passing for type classes is finished.
instance Out Point where
out (Point x y) = println $ parens (show x % "," % show y)
Values of Point
type are constructed in the standard way, and we can use the field names to access their components.
main ()
= do point = Point 2.0 3.0 -- construct a new point
out point -- prints '(2.0, 3.0)'
out point.x -- prints '2.0'
Projecting the x
field gives us a handle to the Float
object inside point
.
oldX = point.x
out oldX -- prints '2.0'
Binding a literal value allocates a new object.
newX = 5.0
We can now make point
reference this new object.
point#x #= newX
out point -- prints '(5.0, 3.0)'
out oldX -- oldX is still '2.0'
Reference projections
In Disciple, we don't need to change our data definitions to include Ref
or IORef
types before we can update them. References are created on the fly with the reference projection operator (#)
.
In the previous example, when we used (#)
a reference was created which held a pointer into the point
object.
(point # x) :: Ref Float
The (#=)
operator is just a regular function which has the (simplified) type:
(#=) :: forall a. Ref a -> a -> ()
When we then evaluated:
point#x #= newX
The pointer inside point
which used to point to oldX
was updated to point to newX
.