@@ -492,9 +492,31 @@ case class TypeChecker(names: NameAnalyser) {
492492
493493 def ground (exp : PExp , pts : PTypeSubstitution ): PTypeSubstitution = {
494494 pts.m.flatMap(kv => kv._2.freeTypeVariables &~ pts.m.keySet).foldLeft(pts)((ts, fv) => {
495- messages ++= FastMessaging .message(exp,
496- s " Unconstrained type parameter, substituting default type ${PTypeSubstitution .defaultType}. " , error = false )
497- ts.add(PTypeVar (fv), PTypeSubstitution .defaultType).toOption.get
495+ var chosenType : PType = PTypeSubstitution .defaultType
496+ curMember match {
497+ case ax : PAxiom if ax.parent.exists(p => p.isInstanceOf [PDomain ]) =>
498+ // If we are inside the domain that defines the type variable, then we choose the type variable itself
499+ // as the default.
500+ val domain = ax.parent.get.asInstanceOf [PDomain ]
501+ // The name pf domain function application type variables has the form
502+ // domainName + PTypeVar.domainNameSep + typeVarName + PTypeVar.sep + index
503+ if (fv.startsWith(domain.idndef.name + PTypeVar .domainNameSep)) {
504+ var tvName = fv.substring(domain.idndef.name.length + 1 )
505+ if (tvName.contains(PTypeVar .sep)) {
506+ tvName = tvName.substring(0 , tvName.indexOf(PTypeVar .sep))
507+ if (domain.typVars.exists(tv => tv.idndef.name == tvName)) {
508+ // The type variable refers to an actual type variable of the current domain.
509+ chosenType = PTypeVar (tvName)
510+ }
511+ }
512+ }
513+ case _ =>
514+ }
515+ if (chosenType == PTypeSubstitution .defaultType) {
516+ messages ++= FastMessaging .message(exp,
517+ s " Unconstrained type parameter, substituting default type ${PTypeSubstitution .defaultType}. " , error = false )
518+ }
519+ ts.add(PTypeVar (fv), chosenType).toOption.get
498520 })
499521 }
500522
@@ -642,7 +664,7 @@ case class TypeChecker(names: NameAnalyser) {
642664
643665 case pdf@ PDomainFunction (_, _, _, _, _) =>
644666 val domain = names.definition(curMember)(pdf.domainName).get.asInstanceOf [PDomain ]
645- val fdtv = PTypeVar .freshTypeSubstitution((domain.typVars map (tv => tv.idndef.name)).distinct) // fresh domain type variables
667+ val fdtv = PTypeVar .freshTypeSubstitution((domain.typVars map (tv => tv.idndef.name)).distinct, Some (domain.idndef.name) ) // fresh domain type variables
646668 pfa.domainTypeRenaming = Some (fdtv)
647669 pfa._extraLocalTypeVariables = (domain.typVars map (tv => PTypeVar (tv.idndef.name))).toSet
648670 extraReturnTypeConstraint = explicitType
0 commit comments