Commit 01f9d1e1 authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2004-05-06 10:52:43 by beppe] Empty log message

Original author: beppe
Date: 2004-05-06 10:52:43+00:00
parent a465b072
......@@ -19,8 +19,8 @@ test it on the <a href="cgi-bin/cduce">online interpreter</a>.
<box title="Advanced programming" link="adv">
<p>
The fact that reference types are encoded rather than primitive brings some
advantages. Among these it is noteworthy that thanks to the encoding the default
The fact that reference types are encoded, rather than primitive, brings some
advantages. Among these it is noteworthy that, thanks to the encoding, the default
behavior of the <code>get</code> and <code>set</code> functions can be
modified. So a programmer can define a reference that whenever is read, records
the access in a log file, or it performs some sanity checks before performing a
......@@ -31,19 +31,19 @@ writing.
For instance the following template program, shows a way to define an integer reference <code>x</code> that whenever it is read executes some extra code, while whenever it is written performs some checks and possibly raises an exception:
</p>
<sample><![CDATA[
let x =
let x : ref Int =
let hidden = ref Int 0 in
{ get = (%%some_extra_code%%; hidden . get) ;
set = fun (x :Int):[] =
if %%some_condition%%
then hidden . set(x)
else raise %%some_exception%%
else raise "Invalid assignment"
}
]]></sample>
<p>
Another advantage is that it is possible to define the types for read only and write only channels, which can be specialized respectively in a covariant and contravariant way.
For instance if a function only reads the content of some integer reference passed as argument, then it can specify its input type as <code>fun ( x :{ get = []->%%T%% } )...</code> so that it will accept as arguments any reference of type <code>ref %%S%%</code>, with <code>%%S%%</code> subtype of <code>%%T%%</code>.
For instance if the body of a function performs on some integer reference passed as argument only read operations, then it can specify its input type as <code>fun ( x :{ get = []->%%T%% } )...</code>. In this case the function can accept as argument any reference of type <code>ref %%S%%</code>, with <code>%%S%%</code> subtype of <code>%%T%%</code>.
</p>
</box>
......@@ -53,7 +53,7 @@ For instance if a function only reads the content of some integer reference pass
<p>
However the use of the encoding also causes some weirdness. For instance a
consequence of the encoding is that the type <code>ref Empty</code> is inhabited
(that is there exists some value). We invite the reader to stop reading the
(that is there exists some value of this type). We invite the reader to stop reading the
rest of this section and try as an exercise to define a value of type <code>ref
Empty</code>.
</p>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment