<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xml:base="http://www.eiffelroom.com" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
 <title>eiffelroom - Fun with Generics - Comments</title>
 <link>http://www.eiffelroom.com/article/fun_with_generics</link>
 <description>Comments for &quot;Fun with Generics&quot;</description>
 <language>en</language>
<item>
 <title>Church numerals</title>
 <link>http://www.eiffelroom.com/article/fun_with_generics#comment-277</link>
 <description>&lt;p&gt;Couldn&#039;t you make it more complicated by using Church numerals? Colin Adams&lt;/p&gt;

</description>
 <pubDate>Tue, 05 Jun 2007 07:28:54 -0700</pubDate>
 <dc:creator>colin-adams</dc:creator>
 <guid isPermaLink="false">comment 277 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Fixed ...</title>
 <link>http://www.eiffelroom.com/article/fun_with_generics#comment-275</link>
 <description>&lt;p&gt;You are right, &lt;tt&gt;SET[G]&lt;/tt&gt; would not type check. Was a typo. I have fixed it in the article.&lt;/p&gt;

</description>
 <pubDate>Tue, 05 Jun 2007 00:16:23 -0700</pubDate>
 <dc:creator>schoelle</dc:creator>
 <guid isPermaLink="false">comment 275 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Signature of i_th_power</title>
 <link>http://www.eiffelroom.com/article/fun_with_generics#comment-272</link>
 <description>&lt;p&gt;I think that the signature of &lt;code class=&quot;geshifilter eiffel&quot;&gt;i_th_power&lt;/code&gt; has to be:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;geshifilter eiffel&quot;&gt;i_th_power &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+INTEGER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;INTEGER&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+SET&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;SET&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+ANY&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;ANY&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I like &lt;code class=&quot;geshifilter eiffel&quot;&gt;NAT&lt;/code&gt;, that helps me on my endless quest to write a program that counts from 1 to 5 in the most complicated way:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;geshifilter eiffel&quot;&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;nat_to_integer &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#123;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+BOOLEAN&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;BOOLEAN&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; |..| nat_to_integer &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#123;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+BOOLEAN&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;BOOLEAN&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #000060;&quot;&gt;do_all&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;agent&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a_int: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+INTEGER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;INTEGER&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt; print &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a_int.&lt;span style=&quot;color: #000060;&quot;&gt;out&lt;/span&gt; + &lt;span style=&quot;color: #0080A0;&quot;&gt;&amp;quot;&lt;span style=&quot;color: #005070; font-weight: bold;&quot;&gt;%N&lt;/span&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;(:&lt;/p&gt;

</description>
 <pubDate>Mon, 04 Jun 2007 12:10:58 -0700</pubDate>
 <dc:creator>doenniu</dc:creator>
 <guid isPermaLink="false">comment 272 at http://www.eiffelroom.com</guid>
</item>
<item>
 <title>Fun with Generics</title>
 <link>http://www.eiffelroom.com/article/fun_with_generics</link>
 <description>
&lt;h2 id=&quot;toc0&quot;&gt;Intro&lt;/h2&gt;
&lt;p&gt;We all know about generics as a wonderful help to implement container data structures like lists, sets, bags etc. Much later than Eiffel, object-oriented programming languages like Java and C# have understood that generic parameters are a powerful extension to the type system and remove the need for many casts. So, what else can we do with generics beyond containers?&lt;/p&gt;

&lt;p&gt;Lets first look at the basics. With generics, we have to differentiate between &lt;strong&gt;class&lt;/strong&gt; and &lt;strong&gt;type&lt;/strong&gt;. OO languages without generics use these two words interchangeably, but with generics they become different concepts.&lt;/p&gt;

&lt;p&gt;A class is what you write: it is a piece of Eiffel code text, describing features, inheritance relations, invariants etc. Classes are instantiated into types. If a class is not generic, then there is exactly one type related to it. For example the class &lt;tt&gt;INTEGER&lt;/tt&gt; defines exactly one type, also called &lt;tt&gt;INTEGER&lt;/tt&gt;. We can think about a non-generic classes to be some kind of  type singletons.&lt;/p&gt;

&lt;p&gt;When we have generic parameters, we have to provide types to create a type from a class. So, we take the class &lt;tt&gt;LINKED_LIST[G]&lt;/tt&gt;, provide it with the type &lt;tt&gt;INTEGER&lt;/tt&gt; and we get the type &lt;tt&gt;LINKED_LIST[INTEGER]&lt;/tt&gt;.&lt;/p&gt;

&lt;p&gt;First observation: this definition is recursive. To get a type from a generic class, we have to provide it with other types. Recursive, that means that every new type can itself be used again to instantiate a class.&lt;/p&gt;

&lt;p&gt;But how many types can we form from a fixed set of classes? There are two answers, depending on whether we talk about &lt;strong&gt;static types&lt;/strong&gt; and &lt;strong&gt;dynamic types&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Static types are the types that we use while writing Eiffel code to declare types for entities (arguments, locals, return values) and when creating new objects. Should we reference a generic parameter of the surrounding type, then we replace it by the type of the constrain (or &lt;tt&gt;ANY&lt;/tt&gt; if there is none).&lt;/p&gt;

&lt;p&gt;How many static types are there? As the number of static types is bound by the program text, there is always a fixed, finite set of static types for a given Eiffel program. There might be more static types than classes (for example &lt;tt&gt;LIST[INTEGER]&lt;/tt&gt; and &lt;tt&gt;LIST[BOOLEAN]&lt;/tt&gt; creates two types from the single class &lt;tt&gt;LIST[G]&lt;/tt&gt;), but a static analyzer can still easily compute the set of all static types for a given program.&lt;/p&gt;

&lt;p&gt;Dynamic types on the other hand are created at run-time. Whenever a new object is created, it gets assigned to be of some type. This type information is then used to resolve dynamic binding of feature invocations and conformance checks on assignment attempts.&lt;/p&gt;

&lt;p&gt;How many dynamic types are there? The number of dynamic types is unbounded: we can create any number of types during the execution of a program. Look at the following Eiffel program that creates arbitrary powersets:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+SET&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;SET&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;G&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;feature&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; powerset: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+SET&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;SET&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+SET&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;SET&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;G&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- ... compute the powerset from a set ...&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; i_th_power &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i:&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+INTEGER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;INTEGER&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+SET&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;SET&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+ANY&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;ANY&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;require&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; i &amp;gt;= &lt;span style=&quot;color: #FF0000;&quot;&gt;0&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;local&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; n: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+INTEGER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;INTEGER&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt; := &lt;span style=&quot;color: #800080;&quot;&gt;Current&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;from&lt;/span&gt; n := &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;until&lt;/span&gt; n &amp;gt; i &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;loop&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt; := &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt;.&lt;span style=&quot;color: #000060;&quot;&gt;powerset&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; n := n + &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Obviously the dynamic type of the resulting set will depend in the input &lt;tt&gt;n&lt;/tt&gt;: if &lt;tt&gt;n = 3&lt;/tt&gt;, the resulting type will be &lt;tt&gt;SET[SET[SET[SET[X]]]]&lt;/tt&gt; (whatever &lt;tt&gt;X&lt;/tt&gt; was when the set was initially created). Of course, the static type of the result is still just &lt;tt&gt;SET[ANY]&lt;/tt&gt; ...&lt;/p&gt;


&lt;h2 id=&quot;toc1&quot;&gt;Counting with generics&lt;/h2&gt;
&lt;p&gt;Now that we have look at generics, lets have a look what we can do with it. We know that we can have an unbounded number of types, so lets use them to implement natural numbers using Peanos axioms:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;V -&amp;gt; &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+ANY&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;ANY&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; default_create &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;feature&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; succ: NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;V&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; pred: V &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Now we can use &lt;tt&gt;NAT[BOOLEAN]&lt;/tt&gt; as zero, &lt;tt&gt;NAT[NAT[BOOLEAN]]&lt;/tt&gt; as one, &lt;tt&gt;NAT[NAT[NAT[BOOLEAN]]]&lt;/tt&gt; as two and so forth.&lt;/p&gt;

&lt;p&gt;Is the successor of the successor of 1 really 3? Or is 3 the direct successor of one? The type checker will tell us:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;check_this &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;local&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; one: NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+BOOLEAN&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;BOOLEAN&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; three: NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+BOOLEAN&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;BOOLEAN&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; three := one.&lt;span style=&quot;color: #000060;&quot;&gt;succ&lt;/span&gt;.&lt;span style=&quot;color: #000060;&quot;&gt;succ&lt;/span&gt; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- 1 &lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; three := one.&lt;span style=&quot;color: #000060;&quot;&gt;succ&lt;/span&gt; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- 2&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;The type accepts statement 1, but not statement 2, proving to us this trivial case of mathematics.&lt;/p&gt;

&lt;p&gt;Of course, dynamically we can create any integer value now:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;integer_to_nat &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i:&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+INTEGER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;INTEGER&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;: NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+ANY&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;ANY&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;if&lt;/span&gt; i = &lt;span style=&quot;color: #FF0000;&quot;&gt;0&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;then&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;create&lt;/span&gt; &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#123;&lt;/span&gt;NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+BOOLEAN&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;BOOLEAN&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;else&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt; := integer_to_nat&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;i&lt;span style=&quot;color: #FF0000;&quot;&gt;-1&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;.&lt;span style=&quot;color: #000060;&quot;&gt;succ&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;But we loose the precise value on the level of the static type, as &lt;tt&gt;integer_to_nat&lt;/tt&gt; still returns only &lt;tt&gt;NAT[ANY]&lt;/tt&gt;. But dynamically, we can recompute the value using assignment attempts:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;nat_to_integer&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;a_nat:NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+ANY&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;ANY&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;: &lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+INTEGER&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;INTEGER&lt;/span&gt;&lt;/a&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;is&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;local&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; not_zero: NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;NAT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;&lt;a href=&quot;http://www.google.com/search?q=site%3Ahttp%3A%2F%2Fdocs.eiffel.com%2Feiffelstudio%2Flibraries+ANY&amp;btnI=I%27m+Feeling+Lucky&quot;&gt;&lt;span style=&quot;color: #800000&quot;&gt;ANY&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;do&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; not_zero ?= a_nat&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;if&lt;/span&gt; not_zero /= &lt;span style=&quot;color: #800080;&quot;&gt;Void&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;then&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #800080;&quot;&gt;Result&lt;/span&gt; := nat_to_integer&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;not_zero.&lt;span style=&quot;color: #000060;&quot;&gt;pred&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt; + &lt;span style=&quot;color: #FF0000;&quot;&gt;1&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Now we have managed to store arbitrary integers into objects without using fields. &lt;tt&gt;NAT&lt;/tt&gt; is an immutable &lt;tt&gt;INTEGER_REF&lt;/tt&gt;, as the dynamic type of an object cannot be changed.&lt;/p&gt;

&lt;p&gt;Of course, I really do now want to know the overhead connected to this, but this example is made for fun, and purely academic (I am a researcher and I am allowed to do this, but should I ever discover production-level code using this encoding of naturals, I will personally shoot or hang the developer, or both).&lt;/p&gt;


&lt;h2 id=&quot;toc2&quot;&gt;Is it useful ?&lt;/h2&gt;
&lt;p&gt;When does it make sense to define such data-types? Specially as we know that the static type set, thus the part of the software that is statically checkable, remains always bound by the software text. Relying on assignment attempts is very close to the casting-mess we face with languages like Java or C++.&lt;/p&gt;

&lt;p&gt;One possible application: we might use the static checker to check properties of data structures by encoding them into types. For example, imagine we have lots of cases where we work with trees and want to make sure that the structures passed in have certain properties.&lt;/p&gt;

&lt;p&gt;A concrete example would be mathematical expressions made out of &lt;tt&gt;NUMBER&lt;/tt&gt;s, &lt;tt&gt;PLUS&lt;/tt&gt;es and &lt;tt&gt;MULT&lt;/tt&gt;s. Here is a sketch:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;deferred&lt;/span&gt; &lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; EXPR&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; NUMBER&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;inherit&lt;/span&gt; &lt;br /&gt;
&amp;nbsp; EXPR&lt;br /&gt;
&lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Code&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; PLUS&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;LEFT -&amp;gt; EXPR,RIGHT -&amp;gt; EXPR&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;inherit&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; EXPR&lt;br /&gt;
&lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Code&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;class&lt;/span&gt; MULT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;LEFT -&amp;gt; EXPR,RIGHT -&amp;gt; EXPR&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;inherit&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; EXPR&lt;br /&gt;
&lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- Code&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #0600FF; font-weight: bold;&quot;&gt;end&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;We want to write a function applying distributivity laws. Of course, such code only works on very specific expressions. The signature would look like this:&lt;/p&gt;

&lt;p&gt;&lt;div class=&quot;geshifilter eiffel&quot; style=&quot;font-family: monospace;&quot;&gt;distributed &lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#40;&lt;/span&gt;in: MULT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;EXPR,PLUS&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;EXPR,EXPR&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#41;&lt;/span&gt;: PLUS&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;MULT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;EXPR,EXPR&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;,MULT&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#91;&lt;/span&gt;EXPR,EXPR&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;span style=&quot;color: #FF0000;&quot;&gt;&amp;#93;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: #008000; font-style: italic;&quot;&gt;-- `in&#039; of the form &#039;a*(b+c)&#039; as an expression of the form `(a*b)+(a*c)&#039;&lt;/span&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Now, it would be impossible to pass an expression to &lt;tt&gt;distributed&lt;/tt&gt; that would not have the required form.&lt;/p&gt;


&lt;h2 id=&quot;toc3&quot;&gt;Some last remarks&lt;/h2&gt;
&lt;p&gt;In ETL2, Bertrand Meyer wrote the following definition of the dynamic type set:&lt;/p&gt;

&lt;dl&gt;
        &lt;dd&gt;The set of possible dynamic types for an entity or expression &lt;tt&gt;x&lt;/tt&gt; is called the &lt;em&gt;dynamic type set&lt;/em&gt; of  &lt;tt&gt;x&lt;/tt&gt;. [...] It is possible to determine the dynamic type set of &lt;tt&gt;x&lt;/tt&gt; through analysis of the classes in the system to which &lt;tt&gt;x&lt;/tt&gt; belongs, by considering all the attachment and reattachment instructions involving &lt;tt&gt;x&lt;/tt&gt; or its entities. (ETL2, p. 323)&lt;/dd&gt;
&lt;/dl&gt;

&lt;p&gt;The ECMA Eiffel standard does not make such a claim. We can now see that the dynamic type set cannot be computed at compile time, as it depends on the execution of the program. We have also seen how we can use dynamic and static types to improve the type safety of our code, and to produce horribly inefficient natural numbers ;-)&lt;/p&gt;

&lt;p&gt;Last but not least: Although all code presented here is 100% legal Eiffel, do not try to compile this code with any other compiler than EiffelStudio. And do not store nats larger than 1000 using &lt;tt&gt;NAT&lt;/tt&gt;. Bad things will happen ... we are here definitely at the border of what Eiffel compilers can handle.&lt;/p&gt;

</description>
 <comments>http://www.eiffelroom.com/article/fun_with_generics#comments</comments>
 <category domain="http://www.eiffelroom.com/taxonomy/term/3">Tutorial</category>
 <category domain="http://www.eiffelroom.com/taxonomy/term/8">Intermediate</category>
 <category domain="http://www.eiffelroom.com/tag/generic">generic</category>
 <category domain="http://www.eiffelroom.com/tag/type_safety">type safety</category>
 <category domain="http://www.eiffelroom.com/tag/types">types</category>
 <pubDate>Fri, 01 Jun 2007 02:16:42 -0700</pubDate>
 <dc:creator>schoelle</dc:creator>
 <guid isPermaLink="false">187 at http://www.eiffelroom.com</guid>
</item>
</channel>
</rss>
