Bolderax
Bolderax

Reputation: 43

Possible bug? using mapper on a list of list with the function size

I have encountered an issue in which I get an error in runtime on code which is valid according to the compiler. The code is pretty simple. This also happens when you have a set[set[str]], but here I have listed the list[list[str]] version. I do not know whether it is exclusive to strings or if it can be any type.

list[int] testMapperSizeListOfList() {
    list[list[str]] a = [["a", "b" , "c"] , ["d"] , ["e", "f"]];
    list[int] b = mapper(a, size);
    return b;
}

This is the error I get:

|std:///List.rsc|(8916,3,<394,56>,<394,59>): CallFailed(
  |std:///List.rsc|(8916,3,<394,56>,<394,59>),
  [["a","b","c"]])
        at mapper(|std:///List.rsc|(8912,25,<394,52>,<394,77>))
        at testMapperSizeListOfList(|project://series0/src/Prob3.rsc|(860,149,<32,0>,<36,1>))
        at $root$(|prompt:///|(0,27,<1,0>,<1,27>)ok

Just looking for some more info on what is happening and if this is intended.

EDIT: The module is a bit bloated, with stuf that is probably not relevant to the issue, but here it is with the corresponding submodules.

module Prob3

import IO;
import Set;
import List;
import Map;

import lang::java::m3::Core;
import lang::java::m3::AST;
import lang::java::jdt::m3::Core;
import lang::java::jdt::m3::AST;

import Helper;
import Prob2;

tuple[int, int, list[str]] mostOccurringNumber(list[Declaration] asts) {
     map[str name, int amount] dic = countVariables(asts);
     map[int amount, set[str] names] invertedDic = invert(dic);
     
     map[int amount, int count]  lengths = (entry : size(invertedDic[entry]) | entry <- invertedDic, true);
     int highest = max(lengths.count);
     int mostCommonValue = getOneFrom(invert(lengths)[highest]);
     
     set[str] filteredNames = invertedDic[mostCommonValue];
     return <mostCommonValue, highest, toList(filteredNames)>;
}

list[int] testMapperSizeListOfList() {
    list[list[str]] a = [["a", "b" , "c"] , ["d"] , ["e", "f"]];
    list[int] b = mapper(a, List::size);
    return b;
}

list[int] mapperSizeListOfListInt() {
    list[list[int]] a = [[1,2,3] , [4] , [5,6]];
    list[int] b = mapper(a, List::size);
    return b;
}

 set[int] mapSizeSetSet() {
     asts = getASTs(|project://smallsql0.21_src|);
     map[str name, int amount] dic = countVariables(asts);
     map[int amount, set[str] names] invertedDic = invert(dic);
     
     int temp = size(getOneFrom(invertedDic.names));
     
     set[int] lengths = mapper(invertedDic.names, size);
     
     //set[list[str]] listTestt = mapper(foo.names, toList);
     //list[list[str]] listTest = toList(listTestt);
     //list[int] lengths2 = mapper(listTest, size);
     
     
     //return temp;
     return lengths;
}

Submodules:

module Prob2

import IO;
import Set;
import List;
import Map;

import lang::java::m3::Core;
import lang::java::m3::AST;
import lang::java::jdt::m3::Core;
import lang::java::jdt::m3::AST;


tuple[int, list[str]] mostOccurringVariable(list[Declaration] asts){
    map[str name, int amount] dic = countVariables(asts);
    int highest = max(dic.amount);
    set[str] correspondingNames = invert(dic)[highest];
    return <highest, toList(correspondingNames)>;
}

map[str,int] countVariables(list[Declaration] asts) {
    map[str name, int amount] dic = ();

    visit(asts){
        case \simpleName(str name):  dic = addToDic(dic,name);
        case \variable(str name , _ , _ ): dic = addToDic(dic,name);
        case \variable(str name , _ ): dic = addToDic(dic,name);
        case \fieldAccess(_ , _ , str name): dic = addToDic(dic,name);
        case \fieldAccess(_ , str name): dic = addToDic(dic,name);
        case \vararg(_ , str name): dic = addToDic(dic,name);
    }
    
    return dic;
}

map[str,int] addToDic(map[str,int] dic, str name) {
    if(name in dic){
                dic[name] = dic[name] + 1; 
            } 
            else{       
             dic = dic += (name: 1);
            } 
    return dic;
}

Helper submodule:

module Helper

import IO;
import Set;
import List;

import lang::java::m3::Core;
import lang::java::m3::AST;
import lang::java::jdt::m3::Core;
import lang::java::jdt::m3::AST;

list[Declaration] getASTs(loc projectLocation){
M3 model = createM3FromEclipseProject(projectLocation);
list[Declaration] asts = [];
for (m <- model.containment, m[0].scheme == "java+compilationUnit"){
asts += createAstFromFile(m[0], true);
}
return asts;
}

int getNumberOfInterfaces(list[Declaration] asts){
int interfaces = 0;
visit(asts){
case \interface(_, _, _, _): interfaces 
+= 1; 
}
return interfaces;
}

Upvotes: 3

Views: 108

Answers (1)

Rodin Aarssen
Rodin Aarssen

Reputation: 153

As you provided only a code snippet, please correct my suspicions/assumptions where needed.

  • Are you importing the Set module as well?
  • Does the error disappear when you fully qualify the size function? (i.e., replacing the third line with list[int] b = mapper(a, List::size);)

If so, you hit a bug with higher-order functions, and we would appreciate a bug report on GitHub! In the meantime, you could use this as a workaround.

EDIT: I stand corrected. It seems that the type of the list elements is not derived correctly (value instead of list[&T]), which is a bug that has long been fixed, I believe. Is it possible for you to switch to rascal-unstable, to see if that solves your problem? It is available from a different update site (https://update.rascal-mpl.org/unstable).

Upvotes: 2

Related Questions