[clean-list] import StdString prevents declaring instance == {#Real}

John van Groningen johnvg at cs.ru.nl
Wed Dec 5 15:06:19 MET 2007


David Norris wrote:
>I find clm (on Linux) complains:
>
>  "Error [Eq.icl,4,import]: == multiply defined"
>
>if StdString is imported in the following program:
>
>--- file Eq.icl ---
>module Eq
>
>import StdClass, StdList, StdArray
>import StdString  // must comment this line to avoid conflict with instance == {#Real} below
>
>instance == {#Real}
>where
>  (==) infix 2:: !{#Real} !{#Real} -> Bool
>  (==) v1 v2 = and [e1 == e2 \\ e1 <-: v1 & e2 <-: v2]
>..
>---
>Apparently, StdString's instance == {#Char} conflicts with instance == {#Real}.  Why should these instance declarations clash? 

The compiler only uses the top level type constructor of the type of an
instance to determine which instance to use. So in this case the type is
an unboxed array type.

Such an instance for unboxed arrays has type {#a}. However, the compiler
also allows a more specific type instead of a, for example {#Char}, but only
for one instance.

This was necessary because the compilers before version 2.0 unified the
types after selecting the instance. For example, if an instance for
{#Char} was defined, and an instance was required for {#a}, the compiler
would use this instance and unify types a and Char. The compiler therefore
allowed only one instance for unboxed arrays for a class.

Starting with Clean 2.0 the compiler no longer unifies the types after
selecting an instance, but instead reports an error if for example
an instance of {#Char} is available, but {#a} is required.

However, the restriction of allowing only one instance of an unboxed array
still exitst, but is probably not necessary anymore. It is probably
possible to allow for example instances of both {#Char} and {#Real}, if
no instance of type {#a} is defined.

>How may I import StdString, yet still define == instance for arrays?

By importing all definitions exported by StdString except the instance for == :

import	StdOverloaded

from StdString import
//  instance ==         {#Char},
    instance <          {#Char},
    instance fromString {#Char},
    instance toString   Int,
    instance toString   Char,
    instance toString   Real,
    instance toString   Bool,
    instance toString   {#Char},
    instance %          {#Char},
    instance +++        {#Char},
    +++. , :=

Kind regards,

John van Groningen


More information about the clean-list mailing list