[bitc-dev] Syntax for constraints

Sandro Magi naasking at higherlogics.com
Mon Apr 12 15:07:04 PDT 2010


On 12/04/2010 5:12 PM, Jonathan S. Shapiro wrote:
> On Mon, Apr 12, 2010 at 8:01 AM, Rick Richardson
> <rick.richardson at gmail.com> wrote:
>> What about an optional keyword  such as 'where'
>> So:
>>
>> def sortList (List 'a) where [Ord 'a, Eq a'] -> { ... }
> 
> I'm hesitant about the postfix modifier, but it may be okay. Let's
> give it a try. Given the other discussion about application and
> parameterized types, though, it's going to be:
> 
>   def sortList (lst :: List 'a) where Ord('a), Eq('a) {
>      ...
>   }

Incidentally, this is exactly the type class encoding I use in C# that I
mentioned earlier [1]. For partial specialization:

public static SortList<T>(List<T> lst)
  where T : IOrd<T>, IEq<T>
{
  ...
}

Or for full specialization:

public static SortList<L, T>(L lst)
  where L : List<T>
  where T : IOrd<T>, IEq<T>
{
  ...
}

There's no difference for the List<T> case, but if you had an INumerics
interface for all base numerical values, then the latter would force the
CLR JIT to generate fully specialized numerical code.

Sandro

[1] http://www.coyotos.org/pipermail/bitc-dev/2010-March/001933.html



More information about the bitc-dev mailing list