Пример #1
0
 /** @see jaskell.compiler.JaskellVisitor#visit(Application) */
 public Object visit(Application a) {
   try {
     /* get type of function */
     Expression fun = a.getFunction();
     /* get type deduced from arguments */
     LinkedList l = new LinkedList();
     Iterator it = a.getArgs().iterator();
     while (it.hasNext()) {
       Expression e = (Expression) it.next();
       l.add((Type) e.visit(this));
     }
     Type vt = TypeFactory.freshBinding();
     Type ft = Types.fun(l, vt);
     log.finer("TypeChecker => In application " + a + ", type is " + ft);
     /* apply substitution on both types */
     ft = subst.substitute(ft);
     /* try to unify function type and constructed types */
     Type t = (Type) fun.visit(this);
     log.finer("In application, function " + fun + " :: " + t);
     t = subst.substitute(t);
     log.finer("In application, trying to unify function type " + t + " with body " + ft);
     /*
      * TODO : problem with unification of constrained types
      */
     TypeApplication uni = (TypeApplication) tu.unify(t, ft, typeVariablesMap);
     /* sets type of functional expression - this allows
      * polymorphic functions to receive several types
      * in the same code */
     // fun.setType(uni);
     /* apply arguments type to compute return type */
     log.finer("Done unify application :" + uni);
     it = PrimitiveType.functionIterator(uni);
     Iterator it2 = l.iterator();
     TypeApplication ut = uni;
     while (it2.hasNext()) {
       /* type of argument */
       Type at = (Type) it2.next();
       ut = (TypeApplication) it.next();
       /* try unification */
       tu.unify(((TypeApplication) ut.getDomain()).getRange(), at, new HashMap(typeVariablesMap));
     }
     ft = subst.substitute(ft);
     fun.setType(ft);
     log.finer("Setting type of functional element [" + fun + "] to :" + ft);
     a.setType(ut.getRange());
     return ut.getRange();
   } catch (TypeError te) {
     if (te.getLineCol() == null) te.setLineCol(a.getTag("source"));
     throw te;
   }
 }
Пример #2
0
 /**
  * Returns null or throws a TypeError if type cannot be inferred for any definition found.
  * Verifies that main symbol in Main module is typed as <code>IO ()</code>.
  *
  * @see jaskell.compiler.JaskellVisitor#visit(Module)
  */
 public Object visit(Namespace a) {
   // visit definitions
   Iterator it = a.getAllBindings().entrySet().iterator();
   while (it.hasNext()) {
     Map.Entry entry = (Map.Entry) it.next();
     String name = (String) entry.getKey();
     Expression def = (Expression) entry.getValue();
     Type type = (Type) def.visit(this);
     def.setType(type);
     log.finer("TypeChecker => END Visiting " + name);
   }
   log.finer("Substitution map = " + typeVariablesMap);
   return null;
 }
Пример #3
0
 /** @see jaskell.compiler.JaskellVisitor#visit(Let) */
 public Object visit(Let a) {
   Type ret = a.getType();
   if (ret != null) return ret;
   // visit definitions
   Iterator it = a.getBindings().entrySet().iterator();
   while (it.hasNext()) {
     Map.Entry entry = (Map.Entry) it.next();
     String name = (String) entry.getKey();
     Expression def = (Expression) entry.getValue();
     log.finer("Visiting " + name);
     def.setType((Type) def.visit(this));
   }
   // visit body
   ret = (Type) a.getBody().visit(this);
   a.setType(ret);
   // return type of body
   return ret;
 }