Contents lists available atScienceDirect
Science
of
Computer
Programming
www.elsevier.com/locate/scico
Ambiguity
and
constrained
polymorphism
Carlos Camarão
a,
Lucília Figueiredo
b,
Rodrigo Ribeiro
c,∗
aDep.deCiênciadaComputação,UniversidadeFederaldeMinasGerais,Av.AntônioCarlos6627,BeloHorizonte,MinasGerais,Brazil bDep.deComputação,UniversidadeFederaldeOuroPreto,ICEB,CampusUniversitárioMorrodoCruzeiro,OuroPreto,MinasGerais,Brazil cDep.deComputaçãoeSistemas,UniversidadeFederaldeOuroPreto,ICEA,JoãoMonlevade,MinasGerais,Brazil
a
r
t
i
c
l
e
i
n
f
o
a
b
s
t
r
a
c
t
Articlehistory: Received26April2015
Receivedinrevisedform19February2016 Accepted16March2016
Availableonline30March2016
Keywords: Ambiguity
Context-dependentoverloading Haskell
This paper considers the problem of ambiguity in Haskell-like languages. Overloading resolution ischaracterized inthe contextofconstrained polymorphismby thepresence ofunreachablevariablesinconstraintsonthetypeoftheexpression.Anewdefinitionof ambiguityispresented,whereexistenceofmorethanoneinstancefortheconstraintson anexpressiontypeisconsideredonlyafteroverloadingresolution.Thisintroducesaclear distinctionbetweenambiguityandoverloadingresolution,makesambiguitymoreintuitive andindependentfromextraconcepts,suchasfunctionaldependencies,andenablesmore programstotype-checkasfewerambiguitiesarise.
The paper presents a type system and a type inference algorithm that includes: a constraint-set satisfiabilityfunction, that determines whether agiven set of constraints is entailed or not in a given context, focusing on issues related to decidability, a constraint-set improvement function, for filtering out constraints for whichoverloading hasbeenresolved,andacontext-reductionfunction,forreducingconstraintsetsaccording to matching instances. A standard dictionary-style semantics for core Haskell is also presented.
2016ElsevierB.V.All rights reserved.
1. Introduction
Thispaperconsiderstheproblemofambiguityinthecontextofconstrainedpolymorphism.
We useconstrainedpolymorphism torefer tothe polymorphismoriginatedby the combinationofparametric polymor-phismandcontext-dependentoverloading.
Context-dependentoverloadingischaracterizedbythefactthatoverloadingresolutioninexpressions(functioncalls)ee′ isbasednotonlyonthetypesofthefunction(e)andtheargument(e′),butalsoonthecontextinwhichtheexpression (ee′) occurs. As resultof this, constants can also be overloaded — for example, literals(like
1
,2
etc.) can be used to representfixedandarbitraryprecisionintegersaswellasfractionalnumbers(forinstance,theycanbeusedinexpressions such as1
+
2.0
) —andfunctionswithtypesthat differonlyonthetype oftheresult(forexample,read functionscan be overloaded, of types String→
Bool, String→
Int etc., each taking a string and generating the denoted value in the corresponding type). In this way,context-dependent overloading allows overloading to have a less restrictive and more prominentroleinthepresenceofparametricpolymorphism,asexploredmainlyintheprogramminglanguageHaskell.*
Correspondingauthor.E-mailaddresses:camarao@dcc.ufmg.br(C. Camarão),luciliacf@gmail.com(L. Figueiredo),rodrigo@decsi.ufop.br(R. Ribeiro). http://dx.doi.org/10.1016/j.scico.2016.03.007
Ambiguityishoweveramajorconcernincontext-dependentoverloading.Theusualmeaningofanambiguousexpression
is, informally, an expression that has more thanone meaning,or an expression that can be interpreted in two ormore distinctways.
A formalization ofthis, withrespectto a language semanticsdefinition by meansoftype derivations, definesthat an expression e is ambiguousif there existtwo ormore type derivations that give the same type and mayassign distinct semantics valuesto e (inthe following,
Ŵ
⊢
e:
σ
specifies thattypeσ
isderivablefor expressione intyping contextŴ
, usingtheaxiomsandrulesofthetypesystem;[
[
Ŵ
⊢
e:
σ
]
]
denotesthesemanticvalueobtainedbyusingsuchaxiomsand rules):Definition1(StandardAmbiguity).Anexpressioneiscalledambiguousifthereexistderivations
and′of[
[
Ŵ
⊢
e:
σ
]
]
and of[
[
Ŵ
′⊢
e:
σ
]
]
,respectively,suchthat[
[
Ŵ
⊢
e:
σ
]
]
= [
[
Ŵ
′⊢
e:
σ
]
]
,whereŴ
andŴ
′givethesametypetoeveryxfreeine.Thisisequivalenttodefiningthatanexpressioneisambiguousifitpreventsthedefinitionofacoherentsemanticstoe [1,page286],thatis,asemanticsdefinedbyinductiononthestructureofexpressions wherethesemanticvalueassigned toawell-typedexpressionisnotindependentofthetypederivation.
Withoutanexplicitreferencetoadistinctdefinition,ambiguousreferstothestandarddefinitionabove.
Detection ofambiguityisusuallydone atcompile-time,bythecompiler typeanalysisphase—inHaskell,by thetype inferencealgorithm.Unfortunately,however,detectionofambiguitycannotbebasedontypesystemdefinitions,atleastfor usual definitions,thatallowcontext-freetypeinstantiations, thatis,type instantiationsthatcanbe doneindependentlyof thecontextwhereanexpressionoccurs.Thiscausesawell-knownincompletenessproblemforusualdefinitionsofHaskell typesystems[2–4].Thisproblemisnotthefocusofthispaper.
This paperconcentrates insteadon anotherissuerelatedto ambiguity inHaskell, whichhasnot received attentionin the technical literature,namely the relationbetweenambiguity andoverloading resolution inthe context ofconstrained polymorphism,inparticularthefactthatthepossibilityofinsertingnew(instance)definitionsdisregardsthatanexpression maybedisambiguatedbyoccurringinsome contextwherethereexistsasingleinstancewhichcanbeusedtoinstantiate typevariablesthatdonotoccurinthesimpletypecomponentoftheconstrainedtype.
Specifically,ourcontributionsare:
•
Aprecisecharacterizationofoverloadingresolutionandambiguity.•
Discussion of Haskell’s open-world definition of ambiguity and proposalof a new definition,called delayed-closure ambiguity, that is distinguished from overloading resolution: in the open-world approach, ambiguity is a syntactic propertyofatype,notdistinguishedfromoverloadingresolution,whereaswithdelayed-closurethissyntacticproperty (existence of unreachable variables in constraints) characterizes overloading resolution, and ambiguity is a property depending on thecontext wheretherelevant expressionoccurs, namelythe existence oftwoor moreinstancesthat entailtheconstraintwithunreachablevariables.Ambiguityistestedonlyafteroverloadingresolution.InSection 2we presentHaskell’sdefinitionofambiguity,calledopen-worldambiguity.InSection 3we compare open-worldambiguitywiththestandard,semanticalnotionofambiguity.
Substitutions,denotedbymeta-variable
φ
,possiblyprimedorsubscripted,areusedthroughoutthepaper.Asubstitution denotes a function fromtype variables to simple type expressions.φ
σ
andφ (
σ
)
denote the capture-free operation of substitutingφ (
α
)
foreach freeoccurrenceoftype variableα
inσ
,andanalogously fortheapplicationofsubstitutionsto constraints,setsoftypesandsetsofconstraints.Symbol
◦
denotes function composition, and dom(φ)
= {
α
|
φ (
α
)
=
α
}
and id denotes the identity substitution. The restrictionφ
|
V ofφ
toV denotesthesubstitutionφ
′suchthatφ
′(
α
)
=
φ (
α
)
ifα
∈
V,otherwiseα
.Asubstitution
φ
ismoregeneralthananotherφ
′,writtenφ
≤
φ
′,ifthereexistsφ
1 suchthatφ
=
φ
1◦
φ
′.Section4presentsanalternativedefinitionofambiguity,calleddelayed-closureambiguity,thatspecifiesessentiallythat:
1. Ambiguityshouldbe checkedwhen(andonlywhen) overloadingisresolved.Weidentifythatoverloadingisresolved in aconstrainton thetype of anexpression by thepresence ofunreachable variables inthisconstraint(overloading resolutionisdefinedformallyinSection2).Atypevariablethatoccursinaconstraintiscalledreachableifitoccursin thesimpletypeorinaconstraintwhereanotherreachabletypevariableoccurs,otherwiseunreachable.
Thisisunlike open-worldambiguity,wheretheexistenceofanytypevariablethat doesnot occurinthesimpletype componentofaconstrainedtypeimplies,intheabsenceoffunctionaldependencies(seebelow),ambiguity.For exam-ple,typeColl c e
⇒
c ofanemptymemberofaclassColl c e,isconsideredambiguousinHaskell,sincetypevariableedoesnotoccurinthesimpletypecomponentoftheconstrainedtypeColl c e
⇒
c(despitebeingreachable).In delayed-closure ambiguity, types withonly reachable type variables are not checked forambiguity, since overloadingis still unresolvedandmayberesolvedlater,dependingonaprogramcontextinwhichitoccurs.Animportantobservationisthatsuchconstraints,removedbytheexistenceofasinglesatisfyingsubstitution,become ambiguousbytheadditionoffurtherinstancesifasatisfyingsubstitutionexistsforaremovedconstraintwithrespect totheinstancesthathavebeenadded.
Thespecificationofdefaults,asproposedinsubsection4.2,allows programmerstoavoidtypestobecomeambiguous bytheadditionoffurtherinstances.
Section 5 contains a description of constraint set satisfiability, focusing on issues related to decidability. Section 6 presents a type systemfor a core-Haskelllanguage that adopts delayed-closure ambiguity. Section 7 presents atype in-ferencealgorithmforcore-Haskellanddiscussessoundnessandcompletenessofthetypeinferencealgorithmwithrespect to thetype system. Section 8 presentsa standarddictionary-style semanticsfor coreHaskell.Section 9discusses related workandSection10summarizesourconclusions.
2. Open-worldambiguity
ThesupportofoverloadinginHaskell isbasedonthedefinitionoftypeclasses.Atype classdeclarationspecifiesnames orsymbols,calledclassmembers,andtheircorrespondingtypes.Severaldefinitionsofthesenamescanbegiven,eachone inaninstancedefinition.Eachdefinitionofa namex,inaninstancedefinition,musthaveatype thatisaninstanceofthe typegiventoxinthetypeclassdeclaration.
Consider,forexample,adeclarationoftypeclassEqthatdefinessymbols
(==)
and(/=)
andtheirtypes,forcomparing iftwovaluesareequalornot,respectively:class
Eq awhere
(==) ::
a→
a→
Bool(/=) ::
a→
a→
Bool x==
y=
not(
x/=
y)
x
/=
y=
not(
x==
y)
TheclassdeclarationofEqspecifiesalsoso-calleddefaultdefinitions.Adefaultdefinitionofanamexisassumedtobe giveninaninstancedefinitionthatdoesnotspecifyitselfadefinitionforx.
InstancesoftypeclassEqdefiningequalityandinequalityofoperations,denotedby
(==)
and(/=)
,forvaluesoftypesIntandBool,canthenbegivenasfollows,assumingthatprimEqIntisaprimitivefunctionfortestingequalityofvaluesof typeInt:
instance
Eq Intwhere
(==) =
primEqIntinstance
Eq Boolwhere
True
==
True=
True False==
False=
True_
== _
=
FalseItis well-knownthat itispossibleto exploreinfinitaryconstrainedpolymorphism inHaskell,forexampleby defining equalityforaninfinitenumberoftypesoflists,asfollows:
instance
Eq a⇒
Eq[
a] where
[]
== []
=
True(
a:
x) == (
b:
y) = (a==b) && (x==y)
_
== _
=
FalseAsa consequenceofthisinstancedefinition,every listformedbyelements whichcanbecomparedforequalitycanitself becomparedforequality.
Polymorphicfunctionsmaybedefinedbytheuseofpolymorphicoverloadedsymbols;forexample:
member
_ []
=
Falsemember a
(
b:
x) = (
a==
b) ||
member a xThetype ofmember is
∀
a.
Eq a⇒
a→
[
a]
→
Bool.Constraint Eq arestrictsmember tobe appliedonlyfortypesthatare instancesoftypeclassEq.Atypeclasscanbedefinedasasubclassofanexistingtypeclass.Forexample:
class
Eq a⇒
Ord awhere
defines Ord asa subclassofEq,which meansthat every type that is an instanceofOrd must alsobe an instance ofEq. Considerthefollowingexample:
search
_ []
=
Falsesearch
a(
b:
x)
| (
a==
b)
=
True| (
a<
b)
=
False|
otherwise= search
a xThetypeofsearchis
∀
a.
Ord a⇒
a→
[
a]
→
Bool.ThefactthatEqisasubclassofOrdenablestheconstraintonthetypeof
search
tobeOrd a,insteadof(Ord a,
Eq a). ConstraintEq aneeds notbeexplicitlyincluded,becauseitisimpliedbytheconstraintOrd a.xdenotesthesequencex1
,
. . . ,
xn,wheren≥
0.Whenusedinthecontextofaset,itdenotesthecorrespondingsetof elementsinthesequence({
x1,
. . . ,
xn}
).Ingeneral,inaconstrainedtype
∀
a.
C⇒
τ
,C isasetofconstraints,thatrestrictsthesetoftypestowhich∀
a.
τ
maybe instantiated,so thatevery instanceτ
[
a→
τ
]
is satisfiableintheprogramtheory,wherea=
a1,
. . . ,
an,
τ
=
τ
1,
. . . ,
τ
n andτ
[
a→
τ
]
denotesthesimultaneoussubstitutionofai byτ
i inτ
,fori=
1,
. . . ,
n.Notationτ
[
a→
τ
]
isdefinedsimilarlyfor quantifiedtypesσ
(σ
[
a→
τ
]
)andforconstraints.Constraint-setsatisfiabilityisdiscussedinSection5.AmbiguityinHaskell isconsidered asa syntacticproperty ontypesofexpressions.The definitionofthispropertyhas been changingover time,since Haskell98,which supportsonlysingle parameter typeclasses (ithasremainedthe same in Haskell 2010): forsingle parametertype classes, ambiguityofa constrainedtype
∀
a.
C⇒
τ
ischaracterized simplybyfv
(
C)
fv(
τ
)
(i.e. bythefactthatthereisatypevariablethatoccursinC butnotinτ
)[2,5].Inthesequelweconsidermulti-parametertypeclasses(MPTCs),andHaskellasitisdefinedinGHC[6]withextensions related toMPTCs (when we refer tostandard Haskell, we meanHaskell 98 orHaskell 2010).MPTCs are recognizedas a natural extension to Haskell, that shouldbe incorporated into thelanguage. This hasbeen recognized asearly asin the originalpaperrelatedtotypeclasses[7].Thishasnothappened,however,mainlybecauseofproblemsrelatedtoambiguity, namelythattheuseofoverloadedsymbolswerethoughttointroduceexpressionswithambiguoustypes.
In ordertointroduce supportforMPTCs, thedefinitionofambiguity inGHCwas changedso thatambiguity could be avoided.AmbiguityofaconstrainedtypeC
⇒
τ
was changedtoadefinitionbasedonthepropertyofreachabilityoftype variables occurring in C,from theset of type variables occurringin thesimple typeτ
, where reachabilityis definedas follows:Definition2.Avariablea
∈
fv(
C)
iscalledreachablefrom,orwithrespectto,asetoftypevariables V ifa∈
V orifa∈
π
forsomeπ
∈
C suchthatthereexistsb∈
fv(
π
)
suchthatbisreachable.a∈
fv(
C)
iscalledunreachableifitisnotreachable. The set of reachable and unreachable type variables of constraint set C from V are denoted respectively byreachableVars
(
C,
V)
andunreachableVars(
C,
V)
.The subset of constraints with reachable and of unreachable type variables of constraint set C fromV are denoted respectivelybyCrV andCuV.
WealsosaythattypevariablesW arereachableinconstrainedtypeC
⇒
τ
ifW⊆
reachableVars(
C,
fv(
τ
))
(andsimilarly forunreachabletypevariablesandifW isatypevariableinsteadofasetoftypevariables).For example,fortype
σ
= ∀
c,
e.
Coll c e⇒
c, variablee is reachablefrom{
c}
,the setof type variables ofthe simple type (c)ofσ
;fortypeσ
= ∀
a.
(
Show a,
Read a)
⇒
(
String→
String)
,variablea isunreachablefromtheemptysetoftype variablesofthesimpletype(String→
String)ofσ
.Itiseasytoseethat,forallC, V wehavethat:
fv
(
C)
=
reachableVars(
C,
V)
∪
unreachableVars(
C,
V)
and reachableVars(
C,
V)
∩
unreachableVars(
C,
V)
= ∅
.
Forexample,bothtypevariablesaandbarereachableinconstrainedtype
(
F ab,
Oa)
⇒
b,sinceboccursinthesimple typepart(b),andaoccursintheconstraintF ab,whichcontainsb.Wealsouse,inthispaper,thefollowing:
Definition3.Overloading(ofsymbolsthatoriginatetheconstraints) inconstraintset D occurringinanexpressionwitha constrainedtypeC
⇒
τ
isresolvedifalltypevariablesinD⊆
C areunreachablefromfv(
τ
)
.For example, overloading in constraint set
{
F ab,
Oa}
, as well as in both constraints in this constraint set, of type(
F ab,
Oa)
⇒
bisyetunresolved,andoverloadingisresolvedforanyconstraintsetthatoccursinaconstraintsetonatype wherethesimpletypehasnotypevariables,as{
Show a,
Read a}
on∀
a.
(
Show a,
Read a)
⇒
String→
String.GHCdefinesan expression asambiguousby ambiguity ofits type C
⇒
τ
,which doesnot meansimplythe existence ofanunreachablevariableinC,withrespecttothesetoftype variablesoccurringinτ
,buttakesintoaccounttheuseof functionaldependencies[8–10].FollowingHaskell’sopen-worldassumption,accordingtowhichinstancesmaybeaddedto awell-typedprogramwithoutcausingatypeerror,ambiguityofaconstrainedtypeC⇒
τ
ischaracterizedbytheexistence ofatypevariableinC thatisnotuniquelydeterminedfromthesetoftypevariablesinthesimpletypeτ
[11].Informally,thisuniquedeterminationspecifiesthat,foreachtypevariable
α
thatisinC butnotinτ
,theremustexist a functionaldependencyβ
→
α
, forsomeβ
inτ
(or asimilar unique determination specifiedvia type families, instead of functional dependencies). In this paper we useβ
→
α
, instead ofβ
→
α
, used in Haskell, to indicate a functional dependency(toavoidconfusionwiththenotationusedtodenotefunctions).Thisuniquedeterminationhasbeenformalizedin[8,10],uponwhichtheformalizationofopen-worldambiguitybelow isbased.
Considerthat:
1. sequencesofconstraintsandoftypescanbeindexeddirectlybytypeclassparameters(i.e. typevariablenames),taken intoaccount that toeach type class parameterthere isa corresponding integer,which givesits positioninthe class declaration;
2. X
→
Y denotesafunctionaldependencyfromthesetoftypevariables X tothesetoftypevariablesY,specifyingthat thevaluesinY aredeterminedbythoseinX;3. Fd
(
A)
denotesthesetoffunctionaldependenciesoftypeclass A.Then,foranyconstraintsetC,thereisasetofinducedfunctionaldependenciesofC,givenby:
IFd
(
C)
= {
fv(
tX)
→
fv(
tY)
|
A t∈
C, (
X→
Y)
∈
Fd(
A)
}
ThetransitiveclosureofV withrespecttoIFd
(
C)
,denotedbyVI+Fd(C),definessetoftypevariablesinCthatareuniquely determinedfromV.Forexample,given
class
F a b|
b→
awhere
...
(thatspecifiesfunctionaldependency b→
a),we havethat{
b}
+IFd(F)
= {
a,
b}
.Wehave:
Definition4 (Open-worldambiguity). A type
∀
a.
C⇒
τ
is called open-worldambiguous (abbreviated asow-ambiguous) if(
a∩
fv(
P))
fv(
τ
)
+IFd(C).For example,constrained type
(
F ab,
Oa)
⇒
b is ow-ambiguous. To prevent this ambiguity, programmers can use a functionaldependency(b→
a)inthedeclarationofclass F.Figuring outwhich functionaldependencies(ortype functions)needto bespecifiedfordealing withambiguityerrors canbeavoidedwithdelayed-closureambiguity,asexplainedinSection4.
Wedefinenowthesetofconstraintsformedbyclassandinstancedeclarationsthatoccurinaprogram,calledaprogram theory(atermborrowedfrom[12]),andconstraintsetprovability(entailment),inaprogramtheory.
Definition5. Aprogram theory P is a set of axiomsof first-order logic,generated from class andinstance declarations occurringintheprogram,asfollows(whereC
⇒
π
isconsideredsyntacticallyequivalenttoπ
ifC isempty):•
Foreachclassdeclarationclass
C⇒
TC a1. . .
anwhere ...
theprogramtheorycontainsthefollowingformulaifC isnotempty:
∀
a.
C⇒
TC awherea
=
a1. . .
an.•
Foreachinstancedeclarationinstance
C⇒
TC t1. . .
tnwhere ...
theprogramtheorycontainsthefollowingformula:∀
a.
C⇒
TC t1. . .
tnwherea
=
fv(
t1)
∪
. . .
fv(
tn)
∪
fv(
C)
;ifC isempty,thentheinstancedeclarationisoftheformP⊢e∅
(ent0)
(∀a.C⇒π)∈P P⊢e{(C⇒π)[a→τ]}
(inst0)
P⊢eC P⊢e{C⇒π}
P⊢e{π}
(mp0)
P⊢eC P⊢eD
P⊢eC∪D
(conj0)
Fig. 1.Constraint set entailment.
andtheprogramtheorycontainstheformula:
∀
a.
TC t1. . .
tnThe property that a set of constraints C is entailed by a program theory P, written as P
⊢
eC, is defined in Fig. 1. Following[13,14],entailmentisobtainedfromclosedconstraintscontainedinaprogramtheory P.3. Ambiguityandconstrainedpolymorphism
Both the open-world and the standard definitions consider as ambiguous an expression e that might be used in a program context wheredistinctinstances existforan overloadedsymbol that occursine.The motivationforthisisthat a coherentsemantics for e, obtainedby using type derivations that derive thetype obtainedby considering the chosen instancetypesareselectedforeachoverloadedsymbol,doesnotexist(becausedistinctsemanticsvaluescouldbegivenby consideringsuchdistinctinstances).
However:
1. if the expressionis effectively usedin a context where overloadingis resolved and thereis a constraint onthe ex-pression’s type forwhich distinct instances exist,then, andonlythen, a type-error, characterizing ambiguity, canbe detected;
2. theexpression maybe used onlyin contexts whereoverloading isresolved in such a waythat thereexists a single instanceforthetypeofeachoverloadedsymbolusedintheexpression.
Thisindicatesaprematurenessofambiguitydetectionbecauseofthepossibilityofanexpressionbeingusedinacontext where two distinct types exist forsome used overloaded symbol. Such possibilityis what both the open-worldandthe standarddefinitionsofambiguityconsider,albeitindifferentways,asshownintheremainderofthissection.
Thestandarddefinitionofambiguityconsiderstheexistinginstancesbutclosestheworldforexpressionswithout consid-eringwhetheroverloadinghasbeenresolvedornot.Weconsideranexamplebelow(Example 2).Theopen-worlddefinition ofambiguitydisregardstheexistenceornotofinstancesintherelevantcontext,andconsidersambiguitybythepossibility ofinsertinganyinstancedefinitions.
Example1. Consider the canonical ambiguous expression in Haskell (cf. e.g. [2,4]):
(
show.
read)
,called e0 for further reference(where“.
”denotesfunctioncomposition).This expression is considered ambiguousin Haskell, irrespective of the context in which it occurs. This is related to thefact thatinstancesdefinitionsare global,i.e. arealwayspresentinanyscope.However, defaultscouldbespecified(in this example,forShow, Read) inorder toavoid ambiguity.In standardHaskell, defaultsare restricted,in arather ad-hoc way, forconstraintsets that includea constrainton class Num. Subsection4.2 describestheuse ofdefaults foravoiding ambiguityofconstraintsets,andconsidersanextensionfortheuseofdefaultsunderdelayed-closureambiguity(Section4). Underdelayed-closureambiguity,thetype ofe0 isString
→
String ifthecontexthasonlyoneinstanceofShow andRead; otherwisethereisatypeerror(unsatisfiability ifthereisnosuchinstance,ambiguityiftherearetwoormore).The factthat thesimpletype inthetype ofe0 cannot bechanged byplacinge0 inanothercontext characterizesthat ambiguity (and unsatisfiability) of e0 should be checked,that is, it should be verified whether there exists or not only one instancethat satisfiestheconstraints onthetype oftheexpression.Ifthere isonlyone instance,theconstraintsare satisfied, andcan thenbe removed from thetype of theexpression (in the example,thetype of
(
show.
read)
canbe simplifiedfrom∀
a.
(
Show a,
Read a)
⇒
(
String→
String)
toString→
String.Although both open-world andstandard definitions ofambiguity both disregard whetheroverloading isor is not yet resolvedandbothanticipatethetestofambiguity,theydisagreeinkeyaspects:thereareexpressionsthatareunambiguous accordingtothestandarddefinitionbutow-ambiguousandvice-versa.
Examplesofthefirstcaseoccurbothwhenoverloadingisandisnotresolved.e0 isanexampleofwhenoverloadingis resolved:itisalwaysow-ambiguous,andstandardambiguitydependsontheexistenceoftwoormoreinstancesof
(
Show a,Example2. Considerexpression
((+)
1)
—whichinHaskell canbewritten as(1+)
—,inaprogram withclassesSumandNumLit,giveninaprogramwiththefollowingclasses,where
1
isconsideredtohavetype∀
a.
NumLit a⇒
a:class
Sum a b cwhere
(+)::
a→
b→
cclass
NumLit awhere ...
Wehavethat
(1+)
isconsideredow-ambiguous,butthestandarddefinitionwouldconsideritambiguousonlyifthere existtwoormoreinstancesofthetypeof(1+)
,namely∀
a,
b,
c.
(
NumLit a,
Sum abc)
⇒
b→
c,intheprogram.Forexample, let P0 be aprogramtheorythat containsinstances Sum Int Float Float,Sum Float Float Float,NumLit Int andNumLit Float. Then(1+)
isconsideredambiguous,accordingtothestandarddefinition,in P0.Both open-worldandstandardambiguitydisregard thatoverloadingisnotyetresolved, andthat thetestofambiguity should not be done yet,since thetype ofthe polymorphic expression
(1+)
can still change depending on the context whereitisused(inthiscase,bothdisagreewithdelayed-closure,describedinthenext section;remember:overloadingis notyetresolvedforanexpressionoftype∀
a,
b,
c.
(
NumLit a,
Sum abc)
⇒
b→
c).4. Delayed-closureambiguity
In thissection we present an approach for dealing withambiguity in Haskell that uses the presence of unreachable variables in a constraint forcharacterizing overloading resolution (or, more precisely, for characterizing that overloading shouldhavebeenresolved),insteadofcharacterizingambiguity.
Informally,insteadofissuinganambiguityerror,thepresenceofanunreachabletypevariableafromthesetoftype vari-ablesinfv
(
τ
)
,inaconstrainedtype∀
a.
C⇒
τ
,triggersatestofwhetherthisvariablecanbeinstantiated(i.e. eliminated), becauseoftheexistenceofasingleinstancethatcanbeusedtoinstantiateit.Weusethefollowingforthis.Definition6.Consider constrainedtype C
⇒
τ
, program theory P,and that type variable a occurs inπ
∈
C andis un-reachablewithrespectto fv(
τ
)
andconsidera substitutionφ
,withdomainrestrictedto unreachabletype variables inC, suchthat P⊢
eφ (
C)
.Thenφ
iscalleda satisfyingsubstitutionforC inP.AuniquesatisfyingsubstitutionforC inP iscalledan improvementsubstitutionofC inP andφ (
C)
theimprovedconstraint.Wecannowdefinedelayed-closureambiguity,asfollows.
Definition7.Type
∀
a.
C⇒
τ
isdelayed-closureambiguous,withrespecttoaprogramtheoryP,ifunreachableVars(
C,
fv(
τ
))
=
∅
andthereexist atleasttwosatisfyingsubstitutionsforC inP.IfthereexistsnosatisfyingsubstitutionforC in P thenC iscalledunsatisfiableinP.
We callimprovement (cf.[15]) the processof substitutinga constrainedtype C
⇒
τ
,ina givenprogram theory P,byφ (
C)
⇒
τ
,whereφ (
C)
istheimprovedconstraintofC in P.Example3.Considertype
(
F a Bool)
⇒
Bool,inaprogramwiththefollowinginstances(formingaprogramtheory P):instance
C a=>
F a Boolwhere ...
instance
C Charwhere ...
Substitution
(
a→
Char)
istheimprovementsubstitutionfor(
F a Bool)
in P,and(
F Char Bool)
istheimprovedconstraint of(
F a Bool)
inP.Example4.ConsideraclassicalexampleofMPTCswithfunctionaldependencies,1namelymatrixmultiplication. Considerthefollowingtypes:
data
Vector=
Vector Int Intdata
Matrix=
Matrix Vector VectorConsideralsothefollowing:
class
Mult a b cwhere
(*) ::
a->
b->
cinstance
Mult Matrix Matrix Matrixwhere ...
instance
Mult Matrix Vector Vectorwhere ...
Thetypeofxinthefollowingexample:
m1
,
m2,
m3:: Matrix
x
= (
m1*
m2) *
m3isinferredtobeMult Matrix Matrix a,Mult aMatrix b
⇒
b.Type variablea isreachablefromb,andthus, withdelayed-closureambiguity, thereisno checkingforambiguity(no ambiguityarises,relievingtheprogrammerfromhavingtofigureoutifandwhichfunctionaldependenciescouldsolvethe problem).
Ifx isusedinacontextthattype Matrixisinferred forit (orofcourseifxisdeclaredtobe oftype Matrix), thenthe type:
Mult Matrix Matrix a
,
Mult a Matrix Matrix⇒
MatrixcanbesimplifiedtoMatrix,ifthereisasingleinstanceof(Mult Matrix Matrix a)inthecurrentcontext;ifanotherinstance of(Mult Matrix Matrix a)existsinthiscontext,thenwehaveambiguity(inthiscase,thecompilercanreportahelpfulerror message,informingthattherearetwoormoreinstancesofMult Matrix Matrix ainthecontext).
Thepossibilityofaddingfurtherinstancesispossibleuntiloverloadingresolution.Thishappenswhenthecontextcannot anymore change(instantiate)thetypeoftheexpression,becauseoftheexistenceofunreachabletypevariablesinthe con-straint.Theprogrammercanthenrelyonthetypeinferencealgorithmtoinstantiateunreachabletypevariables(whenever there existsasingle instanceinthecontext forsuch instantiation),relieving theprogrammerfromhavingtofigure outif andwhichfunctionaldependenciescouldsolvehisproblem.
4.1. Discussion
Haskell’s open-worldhas a notable characteristic that a well-typed program never becomes untypeable by the intro-duction ofnewinstancedeclarations.Delayed-closurerestrictsthisadvantage toexpressions forwhichoverloadingisnot resolved;whenoverloadingisresolved,theworldisclosed,i.e. existingdefinitionsofoverloadednamesareconsidered,by checkingambiguityandunsatisfiability.
This seems a significant disadvantage, but let usconsider further aspects. Withdelayed-closure ambiguity more pro-gramsbecomewell-typedandambiguitybecomeseasiertounderstand:underdelayed-closure,ambiguityisnotasyntactic property ofa type,andit doesnot meanapossibility, ofusingan expression inacontext wheretwo ormoreinstances forthetypeoftheexpressionmightexist.Itmeanstheactualfactthat thereexisttwoormoreinstances,when overload-ing isresolved.Thisagreeswiththeusualunderstandingofambiguityinanaturallanguage,thatconsiders ambiguityfor concrete sentences,that maybeinterpreted indistinct ways. Inourview the mostimportantaspectisthat ambiguityis distinguishedfromoverloadingresolution.Ambiguityistestedonlyafteroverloadingresolution.Thenotionof unsatisfiabil-itybecomes arelatednotion,thatrefers tothenonexistenceofinstancesforentailmentofaconstraintset.Variablesina constraintareeitherallreachableorallunreachable.Iftheyareunreachable,theconstraintcanberemoved(inthecaseof singleentailment)orthereisatype-error(ambiguityincasesoftwoormore,unsatisfiabilityinthecaseofnoentailment). Anotherslightcounter weightinfavor of delayed-closureambiguityisthefactthat ityields amoresymmetrictreatment: forexpressionsforwhichoverloadingisresolved,removalofaninstancedeclarationmaycauseunsatisfiabilityandinsertion maycauseambiguity.
The use of delayed-closure ambiguity in Haskell would benefit by two significant changes in Haskell: the ability to control exportation andimportation ofinstancesin modules andthepossibilityof specifyingdefaultsforconstraintsets. A mechanism for specifying defaults is considered in the next subsection. There are several proposals for allowing the controlinstancescopevisibilityinmodules(seee.g.[16–18]),andadetaileddiscussionisleftforfuturework.
4.2. Defaults
DefaultscanbespecifiedinstandardHaskellbutonlyforconstraintsetswhereallconstraintsconsistofclassesdeclared intheHaskellPreludeandoneofthemisclassNum.Consequencesofthisarethatpredefinedclassesingeneralandclass
Num in particular have to be distinguished by Haskell compilers and, more significantly, an exceptional rule is created, withoutastrongtechnicalreasonforrestrictingdefaultstospecificclasses.Themotivationistoavoidsome frequentuses oftypeannotations.
DistinctproposalsrelatedtochangingthewayofhandlingdefaultsinGHCcanbeconsultedat:
http://ghc.haskell.org/trac/haskell-prime/wiki/Defaulting
Theseincludeaproposalforremovingthepossibilityofspecifyingdefaultsaltogether.Webasicallyfollowthebasicproposal (number2)relatedtothepossibilityofspecifyingdefaultsforMPTCs.
Adefaultclausemayspecifyadefaultfora constraint,whichmaybe atype expression(notonlyatype)ofanykind. Forexample,wecanhave:
default (
Read a)
Intdefault (
Monad m) []
Theremustexistasingledefaultclauseforeachconstraint.Weconsiderthatthenamesgivenindefaultclausesmaybe subjecttorenamingsubstitutions(forexample,
default (
Read a)
Intanddefault (
Read b)
Intareequivalent).Default application is only considered forconstraint sets with unreachable variables, and the only resultof applying defaults is the removal of constraints (sincea constraintwhich contains an unreachable type variable can only contain unreachabletypevariables).Constraintsetremovalisdonepersetofunreachablevariables,asfollows.
Definition8(ConstraintSetRemovalviaDefaulting).AconstraintsetC can beremovedfromC
∪
D⇒
τ
by applicationofa defaultifandonlyifthefollowinghold:1. fv
(
C)
∩
(
fv(
D)
∪
t v(
τ
))
= ∅
; i.e. C hasonly unreachable variables, and they do not occur in (the subset that is not removed)D;2. thereexistsa satisfyingsubstitution
φ
forC intheprogramtheory suchthat,foreach constraint Aτ
in C,thereisa defaultclauseoftheformdefault (
Aτ
)
ρ
inthecurrentmoduleandφ (
τ
)
=
ρ
.Forexample,consider defaultclauses
default (
Read a)
Int anddefault (
Show a)
Int ina givenmodule,witha programtheory P suchthatP⊢
e(
Show Int,
Read Int,
Show Bool,
Read Bool)
.LetC= {
Read a,
Show a}
.ThenCcanberemoved inthismodulefromconstrainedtypeC⇒
τ
ifa∈
/
fv(
τ
)
((
a→
Int)
isthesatisfyingsubstitutionforC usedinthedefinition above).However,ifthedefaultclauseswere,forexample,
default (
Read a)
Intanddefault (
Show a)
Bool,thenC cannot be removed. Substitution(
a→
Int)
cannot be used since there is no default clausedefault
Show a Int; analogously,(
a→
Bool)
cannotbeusedsincethereisnodefaultclausedefault (
Read a)
Bool.5. Satisfiability
Thissection contains adescription ofconstraintsetsatisfiability, includingadiscussion ofdecidability,basedon work alreadypresentedin[19].
Following [15],
⌊
C⌋
P is used to denote the set of satisfiable instances of constraint set C with respect to program theory P:⌊
C⌋
P= {
φ (
C)
|
P⊢
eφ (
C)
}
Example5.Asanexample,consider:
P
= {∀
a,
b.
D a b⇒
C[
a]
b,
D Bool[
Bool]
}
We havethat
⌊
C aa⌋
P= ⌊
C[
Bool] [
Bool]
⌋
P.Both constraints DBool[
Bool] ⇒
C[
Bool] [
Bool]
and C[
Bool] [
Bool]
are membersof⌊
C aa⌋
P andalsomembersof⌊
C[
Bool] [
Bool]
⌋
P.A proof that P
⊢
e{
C[
Bool] [
Bool]
}
holds can be given fromthe entailment rules given in Fig. 1, since this is the conclusion ofrule (mp
0) withpremises P⊢
e{
DBool[
Bool]
}
and P⊢
e{
DBool[Bool]⇒
C[
Bool] [
Bool]
}
, andthesetwo premisescanbederivedbyusingrule(inst
0).Equalityofconstraintsets isconsidered modulo typevariable renaming.Thatis, constraintsets C, D arealsoequal if thereexistsarenamingsubstitution
φ
that canbeappliedto C tomakeφ
C and D equal.φ
isa renamingsubstitution if forallα
∈
dom(
S)
wehavethatφ (
α
)
=
β
,forsometypevariableβ /
∈
dom(φ)
.Constraint setsatisfiability is ingeneral an undecidableproblem [20]. Itis restrictedin thiswork so that it becomes decidable,asdescribedbelow.
The restriction is based ona measure of constraints, givenby a so-called constraint-head-valuefunction, based on a measureofthe sizesoftypesinthisconstrainthead.Essentially,thesequence ofconstraintsthatunifywithaconstraint axiominrecursivecallsofthefunctionthatcheckssatisfiabilityorsimplificationofatypeconstraintissuchthateitherthe sizes oftypesof each constraintinthissequence isdecreasingor thereexists atleastone type parameterposition with decreasingsize.
Thedefinitionoftheconstraint-head-valuefunctionisbasedontheuseofaconstraintvalue
ν
(
π
)
thatgivesthenumber ofoccurrencesoftypevariablesandtypeconstructorsinπ
,definedasfollows:ν
(
Cτ
1· · ·
τ
n)
=
n
i=1ν
(
τ
i)
ν
(
T)
=
1ν
(
α
)
=
1C⊢satsP,Fail∅
(fail1)
∅ ⊢Psats, {id}(empty1)
{π} ∪C⊢satsP, S
{π} ⊢satsP, S0
S= {φ′φ|φ∈S0, φ′∈S1, φ (C)⊢Psats, S1}
(conj1)
sats1(π,P, )
S=
φ′φ (φ,D,π
′)∈, φ′∈S0,
D⊢satsP,[π′,φπ]S0
{π} ⊢Psats, S (inst1)
Fig. 2.Decidable constraint set satisfiability.
Consider computation ofsatisfiability ofagivenconstraintset C withrespectto programtheory P andconsider that, during the process of checking satisfiability of a constraint
π
∈
C, a constraintπ
′ unifies with the head of constraint∀
α
.
C0⇒
π
0 in P,withunifyingsubstitutionφ
.Then,foranyconstraintπ
1 that,inthisprocessofcheckingsatisfiabilityofπ
,alsounifieswithπ
0,wherethe correspondingunifyingsubstitutionisφ
1,thefollowingisrequired,forsatisfiabilityofπ
tohold:1.
ν
(φ
π
′)
islessthanν
(φ
1
π
1)
or,ifν
(φ
π
′)
=
ν
(φ
1π
1)
,thenφ
π
′=
π
′′,forallπ
′′thathasthesameconstraintvalueasπ
′andhasunifiedwithπ
0 inprocessofcheckingforsatisfiabilityof
π
,or 2.ν
(φ
π
′)
isgreaterthanν
(φ
1
π
1)
butthenthereisatypeargumentpositionsuchthatthenumberoftypevariablesand constructors,inthisargumentposition,ofconstraintsthatunifywithπ
0 decreases.Moreprecisely, constrain-head-value-function
associatesa pair(
I,
)
to eachconstraint(
∀
α
.
P0⇒
π
0)
∈
P,where I isatupleofconstraintvaluesandisasetofconstraints.Let0(
π
0)
=
(
I0,
∅
)
foreachconstraintaxiom∀
α
.
P0⇒
π
0∈
P, whereI0isatupleofn+
1 valuesequalto∞
,alargeenoughconstraintvaluedefinedsothat∞
>
ν
(
π
)
foranyconstraintπ
intheprogramtheory.Decidabilityisguaranteedbydefiningtheoperationofupdating
(
π
0)
=
(
I,
)
,denotedby[
π
0,
π
]
,asfollows,where I=
(
v0,
v1,
. . . ,
vn)
andπ
=
Cτ
1· · ·
τ
n:[
π
0,
π
] =
Fail ifv′i
= −
1 fori=
0, . . . ,
n ′ otherwisewhere
′(
π
0)
=
((
v′0,
v′1, . . . ,
v′n),
∪ {
π
}
)
′(
x)
=
(
x)
forx=
π
0v′ 0
=
⎧
⎨
⎩
ν
(
π
)
ifν
(
π
) <
v0orν
(
π
)
=
v0andπ
∈
/
−
1 otherwisefori
=
1, . . . ,
n v′i
=
ν
(
τ
i)
ifν
(
τ
i) <
vi−
1 otherwiseLetsats1
π
,
P,
)
holdif=
(φ
|
fv(π), φ
C0,
π
0)
(
mgu∀
α
.
C0⇒
π
0)
∈
P,
(
π
=
π
0, φ)
holdswheremgu isthemostgeneral(least)unifierrelation[21]: mgu
(
T
,
φ)
isdefinedtoholdbetweenasetofpairsofsimple typesorconstraintsT
andasubstitutionφ
ifi)φ
isaunifierofevery pairinT
(i.e.φ
τ
=
φ
τ
′forevery(
τ
,
τ
′)
∈
T
,and analogously forpairsofsimpleconstraints(
π
,
π
′)
∈
T
),andii)itistheleastsuch unifier(i.e.ifφ
′isaunifierofallpairs inT
,thenφ
≤
φ
′).ThesetofsatisfyingsubstitutionsforC withrespecttotheprogramtheory P isgivenby
S
,such thatC⊢
P,0sats
S
holds, asdefinedinFig. 2.ThefollowingexamplesillustratethedefinitionofconstraintsetsatisfiabilityasdefinedinFig. 2.Let
(
π
).
Iand(
π
).
denotethefirstandsecondcomponentsof(
π
)
,respectively.sats1
(
π
,
P,
{
φ
|
∅,
{
Eq[I]
}
,
π
0}
), φ
= [
a1→
[I]
]
S
0= {
φ
1◦
id|
φ
1∈
S
1,
Eq[I]
⊢
satsP,1S
1}
π
⊢
P,0sats
S
0where
1=
0[π
0,
π
]
,whichimpliesthat1(
π
0)
=
((
3,
3),
{
π
}
)
,sinceν
(
π
)
=
3,anda1 isafreshtypevariable;then:sats1
(
Eq[I]
, ,
{
φ
′|
∅,
{
EqI
}
,
π
0}
), φ
′= [
a2→
I
]
S
1= {
φ
2◦
id|
φ
2∈
S
2,
EqI
⊢
satsP,2S
2}
Eq
[I]
⊢
P,1sats
S
1where
2=
1[π
0,
Eq[I]
]
,whichimpliesthat 2(
π
0)
=
((
2,
2),
2)
,with2= {
π
,
Eq[I]
}
)
,sinceν
(
Eq[I]
)
=
2 is less than1(
π
0).
I.
v0=
3;then:sats1 Eq
I
,
P,
{
(
id,
∅
,
EqI
)
}
S
2= {
φ
3◦
id|
φ
3∈
S
3,
∅ ⊢
satsP,3S
3}
Eq
I
⊢
P,2sats
S
2where
3=
2[EqI
,
EqI
]
andS
3= {
id}
by(SEmpty
1).Thefollowingillustratesacaseofsatisfiabilityinvolvingaconstraint
π
′ thatunifieswithaconstraintheadπ
0suchthat
ν
(
π
′)
isgreaterthantheupperboundassociatedtoπ
0,whichisthefirstcomponentof
(
π
0).
I.Example7.Considersatisfiabilityof
π
=
AI
(
T3I
)
inprogramtheory P= {
A(
T a)
I
,
∀
a,
b.
A(
T2a)
b⇒
Aa(
T b)
}
.Wehave, whereπ
0=
Aa(
T b)
:sats1
π
,
P,
{
(φ
|
∅,
{
π
1}
,
π
0)
}
φ
= [
a1→
I
,
b1→
T2I
]
π
1=
A(
T2I
) (
T2I
)
S
0= {
φ
1◦
id|
φ
1∈
S
1,
π
1⊢
satsP,1S
1}
π
⊢
P,0sats
S
0where
1=
0[π
0,
π
]
,whichimpliesthat1(
π
0).
I=
(
5,
1,
4)
;then:sats1
π
1, ,
{
(φ
′|
∅,
{
π
2}
,
π
0)
}
φ
′= [
a2→
T2I
,
b2→
TI
]
π
2=
A(
T4I
) (
TI
)
S
1= {
φ
2◦ [
a1→
T2a2] |
φ
2∈
S
2,
π
2⊢
satsP,2S
2}
π
1⊢
satsP,1S
1where
2=
1[π
0,
π
1].Sinceν
(
π
1)
=
6>
5=
1(
π
0).
I.
v0,wehavethat2(
π
0).
I=
(
−
1,
−
1,
3)
.Again,
π
2 unifies withπ
0, withunifying substitutionφ
′= [
a3→
T4I
,
b2→
I
]
, and updating 3=
2[π
0,
π
2] gives 3(
π
0).
I=
(
−
1,
−
1,
2)
.Satisfiability is then finally tested forπ
3=
A(
T6I
)
I
,that unifies with A(
T a)
I
,returningS
3=
{[
a3→
T5I
]|
∅}
= {
id}
.Constraintπ
isthussatisfiable,withS
0= {
id}
.Thefollowingexampleillustratesacasewheretheinformationkeptinthesecondcomponentof
(
π
0)
isrelevant.Example8. Considerthesatisfiability of
π
=
A(
T2I
)
F
inprogramtheory P= {
AI
(
T2F
),
∀
a,
b.
Aa(
T b)
⇒
A(
T a)
b}
and letπ
0=
A(
T a)
b.Then:sats1
(
π
,
P,
{
φ
|
∅,
{
π
1}
,
π
0}
)
φ
= [
a1→
(
TI
),
b1→
F
]
π
1=
A(
TI
) (
TF
)
S
0= {
φ
1◦
id|
φ
1∈
S
1,
π
1⊢
satsP,1S
1}
π
⊢
P,0sats
S
0where
1=
0[π
0,
π
]
,giving1(
π
0)
=
((
4,
3,
1),
{
π
}
)
;then:sats1
(
π
1,
P,
{
φ
′|
∅,
{
π
2}
,
π
0}
)
φ
′= [
a2→
I
,
b2→
TF
]
,
π
2=
AI
(
T2F
)
S
1= {
φ
2◦
id|
φ
2∈
S
2,
π
2⊢
satsP,2S
2}
V=fv(τ) Cu V⊢
P,0
sats{φ}
C⇒τ⊢PimprCrV⇒τ
Fig. 3.Constraint set improvement.
where
2=
1[π
0,
π
1].Sinceν
(
π
1)
=
4,whichisequaltothefirstcomponentof1(
π
0).
I,andπ
1isnotin1(
π
0).
,we obtainthatS
2= {
id}
andπ
isthussatisfiable(sincesats1(
AI
(
T2F
),
P)
= {
(
id,
∅
,
AI
(
T2F
)
}
).Since satisfiability of type class constraints is in general undecidable [20],there exist satisfiable instances which are considered tobe unsatisfiableaccordingtothe definitionofFig. 2.Examples canbeconstructedby encoding instancesof solvablePostCorrespondenceproblemsbymeansofconstraintsetsatisfiability,usingG. Smith’sscheme[20].
ToprovethatsatisfiabilityasdefinedinFig. 2isdecidable,considerthatthereexistfinitelymanyconstraintsinprogram theory P,andthat,foranyconstraint
π
thatunifieswithπ
0,wehave,bythedefinitionof[
π
0,
π
]
,that(
π
0)
isupdated so asto includeanewvalue in itssecond component(otherwise[
π
0,
π
]
=
Failandsatisfiability yields∅
asthe setof satisfyingsubstitutionsfortheoriginalconstraint).Theconclusion followsfromthefactthat(
π
0)
canhaveonlyfinitely manydistinctvalues,foranyπ
0.5.1. Improvement
Inthispaper,improvementfiltersoutconstraintswithunreachable typevariables(remember thatthepresenceof un-reachable type variables in a constraint is an indication that overloading has been resolved) from a constraint C, on a constrained type C
⇒
τ
. Improvementtestssatisfiability on Cufv(τ) (the subset ofconstraintsof C withunreachable type variables)andremovesCu
fv(τ) ifeachconstraintinthissubsethasasinglesatisfyingsubstitution. Iftheset
S
ofsatisfiableinstancesofCufv(τ)hasmorethanoneelement,orifitisempty,thereisnoimprovedconstraint (improvementisapartialrelation).ImprovementisdefinedinFig. 3(
0 isasdefinedinsection5,page10).5.2. Contextreduction
Contextreductionisaprocessthatreducesaconstraint
π
intoconstraintsetD accordingtoamatchinginstanceforπ
in therelevantprogramtheory P:ifthereexists(
∀
α
.
C⇒
π
′)
∈
P suchthatφ (
π
′)
=
π
,forsomeφ
suchthatφ (
C)
reducestoD;ifthereisnomatchinginstancefor
π
ornoreductionofφ (
C)
ispossible,thenπ
reducesto(aconstraintsetcontaining only)itself.As an example of a context reduction, consider an instancedeclaration that introduces
∀
a.
Eq a⇒
Eq[
a]
in program theory P;thenEq[
a]
isreducedtoEq a.Contextreductioncanalsooccurduetothepresenceofsuperclassclassdeclarations,butwe onlyconsiderthecaseof instance declarationsinthispaper,whichisthe morecomplexprocess.The treatmentofreducing constraintsduetothe existenceofsuperclassesisstandard;seee.g.[2,5,3].
Contextreductionusesmatches,definedasfollows:
matches
π
, (
P,
′), )
holds if=
(φ (
C0),
π
0,
′)
mgm(
∀
α
.
C0⇒
π
0)
∈
P,
(
π
0=
π
, φ),
′=
[
π
0,
π
]
wheremgmisanalogoustomgu butdenotesthemostgeneralmatchingsubstitution,insteadofthemostgeneralunifier. Thethirdparameterofmatchesiseitheremptyorasingletonset,sinceoverlappinginstances[22]arenotconsidered. Context reduction, definedin Fig. 4, uses rules ofthe form C
⊢
redP,D;
′, meaning that either C reducesto D under programtheory P andleastconstraintvaluefunction,causingtobeupdated to′,orC⊢
redP,FailC;
Fail.Failureisused todefineareductionofaconstraintsettoitself.Theleastconstraintvaluefunctionisusedasinthedefinitionofsatstoguaranteethatcontextreductionisadecidable relation.
An empty constraintset reducesto itself (
red
). Rule (conj
) specifies that constraintset simplificationworks, unlike constraintset satisfiability,by performinga unionoftheresultofsimplifying separatelyeach constraintintheconstraint set.Toseethatarulesimilarto(conj
)cannotbeusedinthecaseofconstraintsetsatisfiability,considerasimpleexample, ofsatisfiability ofC= {
Aa,
Ba}
in P= {
AInt,
ABool,
BInt,
BChar}
.SatisfiabilityofC yields asingle substitutionwhereamapstoInt,nottheunionofcomputingsatisfiabilityforAa andBaseparately.
Rule (
inst
) specifiesthat if thereexists a constraintaxiom∀
α
.
C⇒
Aτ
, such that Aτ
matches withan input con-straintπ
,thenπ
reducestoanyconstraintset D thatC reducesto.∅ ⊢Pred,∅;(red)
{π} ⊢Pred,C;1D⊢redP,1D′;′
{π} ∪D⊢redP,C∪D′;′ (conj)
matchesπ, (P, ),{(C,π′, ′)}
C⊢Pred,′D;′′
{π} ⊢redP,D;′′ (inst)
matchesπ, (P, ),{(C,π′, ′)}
C⊢redP,′D;Fail
{π} ⊢Pred,{π};Fail (stop0)
matchesπ, (P, ),{(C,π′,Fail)}
{π} ∪C⊢redP,{π} ∪C;Fail (stop)
Fig. 4.Context reduction.
Expressions e::=x|λx.e|e e|letx=eine
Fig. 5.Context-free syntax of core Haskell expressions.
Class Name A,B
Type variable a,b,α, β Type constructor T
Simple Constraint π ::=Aτ
Unquantified Constraint ψ ::=C⇒π
Constraint θ ::= ∀α. ψ
Set of Unquantified Constraints C,D
Constrained Type δ ::=C⇒τ
Simple Type τ,ρ,ǫ ::=α|T|τ τ
Type σ ::= ∀α. δ
Program Theory P,Q
Fig. 6.Types, constraints and meta-variable usage.
6. Typesystem
Inthissectionwepresentatypesystemforacore-Haskelllanguagethatadoptsdelayed-closureambiguity.
Weuse acontext-free syntaxofcoreHaskell expressions,giveninFig. 5, wheremeta-variablexrepresentsa variable. Meta-variables x, y, z denote variables and e an expression, possibly primed or subscripted. We call the language core Haskell(notcoreML)becauseexpressions areconsideredtobe typedinaprogram theory(asdefinedinSection 1),with informationaboutoverloadedsymbolsgeneratedfromclassandinstancedeclarations.
Acontext-freesyntax ofconstrainedtypesispresentedinFig. 6,wheremeta-variableusage isalsoindicated.For sim-plicityandfollowingcommonpractice,kindsarenot consideredintype expressions(andthustypeexpressionswhichare notsimpletypesarenotdistinguishedfromsimpletypes).Also,typeexpressionvariablesarecalledsimplytypevariables.
Weassume that aprogram theory ispartofa typingcontext
Ŵ
,andcan bedenoted by PŴ.The initial,globaltyping contextunderwhichprogramexpressions areconsideredtobe typedcontainallassumptions x:
σ
,wherexisa member of a type class A (declared asclass
C⇒
Aα
where
...
x::
τ
...
) andσ
= ∀
α
.
Aa⇒
τ
is the type obtained includinginα
typevariablesinfv(
τ
)
∪
α
∪
fv(
C)
.Weuse:
Ŵ(
x)
= {
σ
|
(
x:
σ
)
∈
Ŵ,
for someσ
}
Ŵ
⊖
x=
Ŵ
− {
(
x:
σ
)
∈
Ŵ
}
Ŵ,
x:
σ
=
(Ŵ
⊖
x)
∪ {
x:
σ
}
Apartialorderontypes,constraintsandtypingcontextsisdefinedinFig. 7.
Notethattype orderingdisregardsconstraintsetsatisfiability.Satisfiabilityisonlyimportantwhenconsideringwhether aconstraintset C can beremoved froma constrainedtype C
,
D⇒
τ
(C canbe removed ifandonlyifoverloadingforChasbeenresolvedandthereexistsasinglesatisfyingsubstitutionforC;seeFig. 8).