[nem-bug] [Nemerle 0000657]: Unhandled Exception: Nemerle.Core.MatchFailureException in ncc

feedback at nemerle.org feedback at nemerle.org
Tue May 2 19:28:52 CEST 2006


The following issue has been RESOLVED.
======================================================================
<http://nemerle.org/bugs/view.php?id=657> 
======================================================================
Reported By:                VladD2
Assigned To:                malekith
======================================================================
Project:                    Nemerle
Issue ID:                   657
Category:                   Compiler
Reproducibility:            always
Severity:                   minor
Priority:                   normal
Status:                     resolved
Resolution:                 fixed
Fixed in Version:           
======================================================================
Date Submitted:             04-17-2006 04:09 CEST
Last Modified:              05-02-2006 19:28 CEST
======================================================================
Summary:                    Unhandled Exception:
Nemerle.Core.MatchFailureException in ncc
Description: 
using System.Console;

def Sort[T](lst, gt)
{
  match (lst)
  { | [] => [] : list[T] // This cause of Unhandled Exception
    | head :: tail => Sort($[y | y in tail, gt(head, y)], gt) 
                    + [head]
                    + Sort($[y | y in tail, gt(y, head)], gt)
  }
}

WriteLine(Sort([9, 1, 2, 8, 3, 7, 4, 6, 5, 0], _ > _));
WriteLine(Sort(["B", "A", "Q", "Z", "T", "Z", "J", "C", "D", "H"],
  fun(x, y) { string.Compare(x, y) > 0 }));

If try to compile this code the result compiler crash:
Unhandled Exception: Nemerle.Core.MatchFailureException: Exception of type
'Nemerle.Core.MatchFailureException' was thrown.
   at _N_AutoModule._N_Sort1746[T](_N_closure1740 _N_Main_cp1745, list`1
lst, Function`3 gt) in C:\VS\Nemerle\Tests\Sort.n:line 5
   at _N_AutoModule._N_Sort1746[T](_N_closure1740 _N_Main_cp1745, list`1
lst, Function`3 gt) in C:\VS\Nemerle\Tests\Sort.n:line 7
   at _N_AutoModule.Main() in C:\VS\Nemerle\Tests\Sort.n:line 13
======================================================================

----------------------------------------------------------------------
 malekith - 04-28-06 00:56 
----------------------------------------------------------------------
Mono stack trace shows the cause better:

Unhandled Exception: Nemerle.Core.MatchFailureException: Exception of type
Nemerle.Core.MatchFailureException was thrown.
in <0x008a6> _N_AutoModule:_N_Sort1625[Object] (._N_closure1619
_N_Main_cp1624, Nemerle.Core.list`1 lst, Nemerle.Builtins.Function`3 gt)
in <0x00474> _N_AutoModule:_N_Sort1625[Int32] (._N_closure1619
_N_Main_cp1624, Nemerle.Core.list`1 lst, Nemerle.Builtins.Function`3 gt)
in <0x00194> _N_AutoModule:Main ()
    0.05s real     0.04s user     0.01s system
Non zero exit code.

we're calling Sort[object] from Sort[int], which is wrong. I'm not sure
why though. Will investigate.

The fully typed version:
  def Sort[T](lst:list[T], gt)
works fine (just in case you need this to work now).

----------------------------------------------------------------------
 VladD2 - 04-28-06 12:26 
----------------------------------------------------------------------
I understand that it's wrong code.

But compiler should report correct error message but not crash.

----------------------------------------------------------------------
 malekith - 04-28-06 13:41 
----------------------------------------------------------------------
The code is OK. Note that it's the generated code what is wrong, not the
source.

It is OK, because by enforcing result type to be list[T], you also enforce
the source type to be list[T] (because you're using + on result type and
source type).

----------------------------------------------------------------------
 VladD2 - 04-29-06 00:56 
----------------------------------------------------------------------
[offtop]
Where possible to read about algorithm of a types inference?
[/offtop]

----------------------------------------------------------------------
 malekith - 04-29-06 10:15 
----------------------------------------------------------------------
http://nemerle.org/Type_inference

----------------------------------------------------------------------
 VladD2 - 04-29-06 15:14 
----------------------------------------------------------------------
I meant article that describing principles of the algorithm.

----------------------------------------------------------------------
 VladD2 - 04-29-06 15:18 
----------------------------------------------------------------------
Sorry. I have not noticed the http://nemerle.org/~malekith/msc.pdf

----------------------------------------------------------------------
 malekith - 05-01-06 12:02 
----------------------------------------------------------------------
OK, the smaller example is:

def Sort[T](lst)
{
  | [] => [] : list[T]
  | head :: tail => [head] + Sort(tail)
}

_ = Sort([1]);

The problem is, that when we seen Sort invocation within the Sort
function, we try to construct a fresh instance of Sort type. The intended
type of Sort is

   forall T. list[T] -> list[T]

however at the point of Sort reference we don't know it yet, so we make
something like:

   Sort[fresh_tv] : list[something] -> something-else

Later something is unified with T and something-else with list[T], which
happens to work (because tail is list[T]), but there is no connection
between T and fresh_tv introduced in Sort reference.

So what we need here, is some restriction on types of local polymorphic
functions. The easiest option would be to require explicit types on all
parameters and return type of generic local functions, but I would like to
avoid that.

----------------------------------------------------------------------
 malekith - 05-02-06 19:28 
----------------------------------------------------------------------
Resolved on trunk, r6213.

This code causes an error now, one need to use explicit types.

Issue History
Date Modified  Username       Field                    Change              
======================================================================
04-17-06 04:09 VladD2         New Issue                                    
04-28-06 00:56 malekith       Note Added: 0001205                          
04-28-06 12:26 VladD2         Note Added: 0001206                          
04-28-06 13:41 malekith       Note Added: 0001207                          
04-29-06 00:56 VladD2         Note Added: 0001208                          
04-29-06 10:15 malekith       Note Added: 0001209                          
04-29-06 15:14 VladD2         Note Added: 0001210                          
04-29-06 15:18 VladD2         Note Added: 0001211                          
05-01-06 12:02 malekith       Note Added: 0001214                          
05-02-06 19:28 malekith       Status                   new => resolved     
05-02-06 19:28 malekith       Resolution               open => fixed       
05-02-06 19:28 malekith       Assigned To               => malekith        
05-02-06 19:28 malekith       Note Added: 0001217                          
======================================================================




More information about the bugs mailing list