Example #1
0
 /** @see jaskell.compiler.JaskellVisitor#visit(ConstructorPattern) */
 public Object visit(ConstructorPattern a) {
   String cname = a.getConstructor().getName();
   /* retrieve parameter types of constructor */
   ConstructorDefinition ctor = (ConstructorDefinition) a.getConstructor().lookup(cname);
   if (ctor == null) throw new TypeError("Undefined constructor pattern  " + a);
   /* type of data constructed by constructor */
   TypeInstantiator ti = new TypeInstantiator(ctor.getDataType());
   Type rtype = ti.instance();
   Map map = ti.getMap();
   TypeSubstitution ts = new TypeSubstitution(map);
   Iterator ittypes = ctor.getParameters().iterator();
   /* retrieve type of patterns */
   Iterator it = a.getSubPatterns();
   while (it.hasNext()) {
     try {
       Pattern p = (Pattern) it.next();
       Type actual = TypeFactory.freshBinding();
       Type formal = ts.substitute((Type) ittypes.next());
       /* unify both types */
       p.setType(tu.unify(formal, actual, typeVariablesMap));
     } catch (NoSuchElementException nex) {
       throw new TypeError("Wrong number of arguments to pattern " + a);
     }
   }
   a.setType(rtype);
   return a.getType();
 }