[nem-bug] [Nemerle 0000746]: Unrestricted matching in def's and method "heads"

feedback at nemerle.org feedback at nemerle.org
Tue Sep 5 02:06:14 CEST 2006


A NOTE has been added to this issue.
======================================================================
<http://nemerle.org/bugs/view.php?id=746> 
======================================================================
Reported By:                d
Assigned To:                
======================================================================
Project:                    Nemerle
Issue ID:                   746
Category:                   Language Feature
Reproducibility:            N/A
Severity:                   minor
Priority:                   normal
Status:                     feedback
======================================================================
Date Submitted:             08-20-2006 03:03 CEST
Last Modified:              09-05-2006 02:06 CEST
======================================================================
Summary:                    Unrestricted matching in def's and method "heads"
Description: 
#pragma indent

using Tree

variant Tree ['a]
  | Node
    left : Tree ['a]
    elem : 'a
    right : Tree ['a]
  | Leaf

def Node (_, e, _) = Node (Leaf (), "foo", Leaf ())

System.Console.WriteLine (e)

// And similar stuff in function/method definitions.
/* This is a classic in the functional world, so it would be expected to
work. It's also very useful sometimes, makes the code shorter and more
readable. */
======================================================================

----------------------------------------------------------------------
 nazgul - 08-20-06 12:24 
----------------------------------------------------------------------
Try:

#pragma indent

using Tree

variant Tree ['a]
 | Node
   left : Tree ['a]
   elem : 'a
   right : Tree ['a]
 | Leaf

def (_, e, _) = Node (Leaf (), "foo", Leaf ())

System.Console.WriteLine (e)

====================

The def foo (x,y) 
syntax is simply reserved to functions



----------------------------------------------------------------------
 d - 08-20-06 19:33 
----------------------------------------------------------------------
This is counter-intuitive and inconsistent. It seems to make no sense. It
looks like matching a tuple against some other constructor. That's evil
IMO.
Besides, check out these examples:

#pragma indent

using T

variant T ['a]
 | Cons1
   elema : 'a
   elemb : 'a
 | Cons2
   elema : 'a
   elemb : 'a
 | Cons3
   elema : 'a

def (e1, _) = Cons1 (1, 1)
def (e2, _) = Cons2 (2, 2)
def (e3) = Cons3 (3)
def (e4,) = Cons3 (4)

System.Console.WriteLine (e1)
System.Console.WriteLine (e2)
System.Console.WriteLine (e3)
System.Console.WriteLine (e4)

/*
Will print

1
2
T`1+Cons3[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]
T`1+Cons3[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]

// Inconsistent.
*/

def foo ((x1, x2))
  x1 + x2

foo (Cons1 (5, 6))

/*
error: in argument http://nemerle.org/bugs/view.php?id=1 (_N_pat1071) of foo,
needed a (? * ?), got
T.Cons1[int]: T.Cons1[int] is not a subtype of (? * ?) [simple require]

// Inconsistent.
*/

def bar (Cons1 (x1, x2))
  x1 + x2

bar (Cons1 (5, 6))

/*
error: parse error near `(...)' group: unexpected token after group of
tokens
*/

def bar ((x1))
  x1

System.Console.WriteLine (bar (Cons3 (5)))

/*
T`1+Cons3[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]

// Incosistent.
*/

/*
And so on and so on. This really doesn't work the way you'd expect it to.
Solutions ? `def foo (x, y) { body }` and `def foo (x, y) = sth` are
easily differenciated by checking if "=" occurs in them. It is even easier
see this in function arguments..
*/

----------------------------------------------------------------------
 malekith - 09-05-06 02:06 
----------------------------------------------------------------------
The rule is: when you match some non-tuple against a tuple pattern, and
this non-tuple thing has the right amount of fields it's converted to a
tuple. 

<sidenote>
This is very often useful (for example in variant matching), though
sometimes not enough, like in:

def h = System.Collections.Generic.Dictionary ()
...
foreach ((k, v) in h) ...

which doesn't work, beacause KeyValuePair.Key and .Value are properties
and not fields.

One needs to use (Key = k, Value = v) here.
</sidenote>

There is no notion of constructor (as seen in functional languages) in
Nemerle, these are just objects that happen to have the right amount of
fields.

I agree there is a problem with (x), which should be 1-tuple, but I hope
you agree this would just bring more confusion.

Next the notion of patterns in function parameters and "def" is limited to
match-all patterns (which are subset of patterns that are syntactically
known to always match). This is mostly an implementation issue, however I
believe it is a good thing that "def" and funtion calls cannot fail at
runtime.

Issue History
Date Modified  Username       Field                    Change              
======================================================================
08-20-06 03:03 d              New Issue                                    
08-20-06 03:04 d              Summary                  Unrestricted matching in
def's method "heads" => Unrestricted matching in def\'s and method \"heads\"
08-20-06 03:04 d              Description Updated                          
08-20-06 03:05 d              Issue Monitored: d                           
08-20-06 12:24 nazgul         Note Added: 0001434                          
08-20-06 12:24 nazgul         Status                   new => feedback     
08-20-06 12:24 nazgul         Summary                  Unrestricted matching in
def's and method "heads" => Unrestricted matching in def\'s and method \"heads\"
08-20-06 12:24 nazgul         Description Updated                          
08-20-06 19:33 d              Note Added: 0001435                          
09-05-06 02:06 malekith       Note Added: 0001439                          
======================================================================




More information about the bugs mailing list