• Nenhum resultado encontrado

Carlos Camarãoa, Lucília Figueiredob, Rodrigo Ribeiroc, ∗

N/A
N/A
Protected

Academic year: 2019

Share "Carlos Camarãoa, Lucília Figueiredob, Rodrigo Ribeiroc, ∗"

Copied!
19
0
0

Texto

(1)

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 as

1

+

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

(2)

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,sincetypevariablee

doesnotoccurinthesimpletypecomponentoftheconstrainedtypeColl c e

c(despitebeingreachable).In delayed-closure ambiguity, types withonly reachable type variables are not checked forambiguity, since overloadingis still unresolvedandmayberesolvedlater,dependingonaprogramcontextinwhichitoccurs.

(3)

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 a

where

(==) ::

a

a

Bool

(/=) ::

a

a

Bool x

==

y

=

not

(

x

/=

y

)

x

/=

y

=

not

(

x

==

y

)

TheclassdeclarationofEqspecifiesalsoso-calleddefaultdefinitions.Adefaultdefinitionofanamexisassumedtobe giveninaninstancedefinitionthatdoesnotspecifyitselfadefinitionforx.

InstancesoftypeclassEqdefiningequalityandinequalityofoperations,denotedby

(==)

and

(/=)

,forvaluesoftypes

IntandBool,canthenbegivenasfollows,assumingthatprimEqIntisaprimitivefunctionfortestingequalityofvaluesof typeInt:

instance

Eq Int

where

(==) =

primEqInt

instance

Eq Bool

where

True

==

True

=

True False

==

False

=

True

_

== _

=

False

Itis well-knownthat itispossibleto exploreinfinitaryconstrainedpolymorphism inHaskell,forexampleby defining equalityforaninfinitenumberoftypesoflists,asfollows:

instance

Eq a

Eq

[

a

] where

[]

== []

=

True

(

a

:

x

) == (

b

:

y

) = (a==b) && (x==y)

_

== _

=

False

Asa consequenceofthisinstancedefinition,every listformedbyelements whichcanbecomparedforequalitycanitself becomparedforequality.

Polymorphicfunctionsmaybedefinedbytheuseofpolymorphicoverloadedsymbols;forexample:

member

_ []

=

False

member a

(

b

:

x

) = (

a

==

b

) ||

member a x

Thetype ofmember is

a

.

Eq a

a

[

a

]

Bool.Constraint Eq arestrictsmember tobe appliedonlyfortypesthatare instancesoftypeclassEq.

Atypeclasscanbedefinedasasubclassofanexistingtypeclass.Forexample:

class

Eq a

Ord a

where

(4)

defines Ord asa subclassofEq,which meansthat every type that is an instanceofOrd must alsobe an instance ofEq. Considerthefollowingexample:

search

_ []

=

False

search

a

(

b

:

x

)

| (

a

==

b

)

=

True

| (

a

<

b

)

=

False

|

otherwise

= search

a x

Thetypeofsearchis

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 simplyby

fv

(

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 by

reachableVars

(

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.

(5)

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

a

where

...

(thatspecifiesfunctionaldependency b

a),we havethat

{

b

}

+I

Fd(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):

Foreachclassdeclaration

class

C

TC a1

. . .

an

where ...

theprogramtheorycontainsthefollowingformulaifC isnotempty:

a

.

C

TC a

wherea

=

a1

. . .

an.

Foreachinstancedeclaration

instance

C

TC t1

. . .

tn

where ...

theprogramtheorycontainsthefollowingformula:

a

.

C

TC t1

. . .

tn

wherea

=

fv

(

t1

)

. . .

fv

(

tn

)

fv

(

C

)

;ifC isempty,thentheinstancedeclarationisoftheform

(6)

Pe

(ent0)

(∀a.C⇒π)∈P Pe{(C⇒π)[a→τ]}

(inst0)

PeC Pe{C⇒π}

Pe{π}

(mp0)

PeC PeD

PeCD

(conj0)

Fig. 1.Constraint set entailment.

andtheprogramtheorycontainstheformula:

a

.

TC t1

. . .

tn

The 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,

(7)

Example2. Considerexpression

((+)

1)

—whichinHaskell canbewritten as

(1+)

—,inaprogram withclassesSum

andNumLit,giveninaprogramwiththefollowingclasses,where

1

isconsideredtohavetype

a

.

NumLit a

a:

class

Sum a b c

where

(+)::

a

b

c

class

NumLit a

where ...

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 Bool

where ...

instance

C Char

where ...

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 Int

data

Matrix

=

Matrix Vector Vector

Consideralsothefollowing:

class

Mult a b c

where

(*) ::

a

->

b

->

c

instance

Mult Matrix Matrix Matrix

where ...

instance

Mult Matrix Vector Vector

where ...

(8)

Thetypeofxinthefollowingexample:

m1

,

m2

,

m3

:: Matrix

x

= (

m1

*

m2

) *

m3

isinferredtobeMult 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

Matrix

canbesimplifiedtoMatrix,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.

(9)

Adefaultclausemayspecifyadefaultfora constraint,whichmaybe atype expression(notonlyatype)ofanykind. Forexample,wecanhave:

default (

Read a

)

Int

default (

Monad m

) []

Theremustexistasingledefaultclauseforeachconstraint.Weconsiderthatthenamesgivenindefaultclausesmaybe subjecttorenamingsubstitutions(forexample,

default (

Read a

)

Intand

default (

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 defaultclauseoftheform

default (

A

τ

)

ρ

inthecurrentmoduleand

φ (

τ

)

=

ρ

.

Forexample,consider defaultclauses

default (

Read a

)

Int and

default (

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

)

Intand

default (

Show a

)

Bool,thenC cannot be removed. Substitution

(

a

Int

)

cannot be used since there is no default clause

default

Show a Int; analogously,

(

a

Bool

)

cannotbeusedsincethereisnodefaultclause

default (

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

ν

(

α

)

=

1

(10)

CsatsP,Fail

(fail1)

∅ ⊢Psats, {id}(empty1)

{π} ∪CsatsP, S

{π} ⊢satsP, S0

S= {φ′φ|φS0, φ′S1, φ (C)⊢Psats, S1}

(conj1)

sats1(π,P, )

S=

φ′φ (φ,D

), φS0,

DsatsP,[π′,φπ]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 isatupleofconstraintvaluesand

isasetofconstraints.Let

0

(

π

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 ifvi

= −

1 fori

=

0

, . . . ,

n

′ otherwise

where

(

π

0

)

=

((

v0

,

v1

, . . . ,

vn

),

∪ {

π

}

)

(

x

)

=

(

x

)

forx

=

π

0

v′ 0

=

ν

(

π

)

if

ν

(

π

) <

v0or

ν

(

π

)

=

v0and

π

/

1 otherwise

fori

=

1

, . . . ,

n v

i

=

ν

(

τ

i

)

if

ν

(

τ

i

) <

vi

1 otherwise

Letsats1

π

,

P

,

)

holdif

=

|

fv(π)

, φ

C0

,

π

0

)

(

mgu

α

.

C0

π

0

)

P

,

(

π

=

π

0

, φ)

holds

wheremgu isthemostgeneral(least)unifierrelation[21]: mgu

(

T

,

φ)

isdefinedtoholdbetweenasetofpairsofsimple typesorconstraints

T

andasubstitution

φ

ifi)

φ

isaunifierofevery pairin

T

(i.e.

φ

τ

=

φ

τ

forevery

(

τ

,

τ

)

T

,and analogously forpairsofsimpleconstraints

(

π

,

π

)

T

),andii)itistheleastsuch unifier(i.e.if

φ

isaunifierofallpairs in

T

,then

φ

φ

′).

ThesetofsatisfyingsubstitutionsforC withrespecttotheprogramtheory P isgivenby

S

,such thatC

P,0

sats

S

holds, asdefinedinFig. 2.

ThefollowingexamplesillustratethedefinitionofconstraintsetsatisfiabilityasdefinedinFig. 2.Let

(

π

).

Iand

(

π

).

denotethefirstandsecondcomponentsof

(

π

)

,respectively.

(11)

sats1

(

π

,

P

,

{

φ

|

,

{

Eq

[I]

}

,

π

0

}

), φ

= [

a1

[I]

]

S

0

= {

φ

1

id

|

φ

1

S

1

,

Eq

[I]

satsP,1

S

1

}

π

P,0

sats

S

0

where

1

=

0[

π

0

,

π

]

,whichimpliesthat

1

(

π

0

)

=

((

3

,

3

),

{

π

}

)

,since

ν

(

π

)

=

3,anda1 isafreshtypevariable;then:

sats1

(

Eq

[I]

, ,

{

φ

|

,

{

Eq

I

}

,

π

0

}

), φ

= [

a2

I

]

S

1

= {

φ

2

id

|

φ

2

S

2

,

Eq

I

satsP,2

S

2

}

Eq

[I]

P,1

sats

S

1

where

2

=

1[

π

0

,

Eq

[I]

]

,whichimpliesthat

2

(

π

0

)

=

((

2

,

2

),

2

)

,with

2

= {

π

,

Eq

[I]

}

)

,since

ν

(

Eq

[I]

)

=

2 is less than

1

(

π

0

).

I

.

v0

=

3;then:

sats1 Eq

I

,

P

,

{

(

id

,

,

Eq

I

)

}

S

2

= {

φ

3

id

|

φ

3

S

3

,

∅ ⊢

satsP,3

S

3

}

Eq

I

P,2

sats

S

2

where

3

=

2[Eq

I

,

Eq

I

]

and

S

3

= {

id

}

by(

SEmpty

1).

Thefollowingillustratesacaseofsatisfiabilityinvolvingaconstraint

π

thatunifieswithaconstrainthead

π

0suchthat

ν

(

π

)

isgreaterthantheupperboundassociatedto

π

0,whichisthefirstcomponentof

(

π

0

).

I.

Example7.Considersatisfiabilityof

π

=

A

I

(

T3

I

)

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

T2

I

]

π

1

=

A

(

T2

I

) (

T2

I

)

S

0

= {

φ

1

id

|

φ

1

S

1

,

π

1

satsP,1

S

1

}

π

P,0

sats

S

0

where

1

=

0[

π

0

,

π

]

,whichimpliesthat

1

(

π

0

).

I

=

(

5

,

1

,

4

)

;then:

sats1

π

1

, ,

{

|

,

{

π

2

}

,

π

0

)

}

φ

= [

a2

T2

I

,

b2

T

I

]

π

2

=

A

(

T4

I

) (

T

I

)

S

1

= {

φ

2

◦ [

a1

T2a2

] |

φ

2

S

2

,

π

2

satsP,2

S

2

}

π

1

satsP,1

S

1

where

2

=

1[

π

0

,

π

1].Since

ν

(

π

1

)

=

6

>

5

=

1

(

π

0

).

I

.

v0,wehavethat

2

(

π

0

).

I

=

(

1

,

1

,

3

)

.

Again,

π

2 unifies with

π

0, withunifying substitution

φ

= [

a3

T4

I

,

b2

I

]

, and updating

3

=

2[

π

0

,

π

2] gives

3

(

π

0

).

I

=

(

1

,

1

,

2

)

.Satisfiability is then finally tested for

π

3

=

A

(

T6

I

)

I

,that unifies with A

(

T a

)

I

,returning

S

3

=

{[

a3

T5

I

]|

}

= {

id

}

.Constraint

π

isthussatisfiable,with

S

0

= {

id

}

.

Thefollowingexampleillustratesacasewheretheinformationkeptinthesecondcomponentof

(

π

0

)

isrelevant.

Example8. Considerthesatisfiability of

π

=

A

(

T2

I

)

F

inprogramtheory P

= {

A

I

(

T2

F

),

a

,

b

.

Aa

(

T b

)

A

(

T a

)

b

}

and let

π

0

=

A

(

T a

)

b.Then:

sats1

(

π

,

P

,

{

φ

|

,

{

π

1

}

,

π

0

}

)

φ

= [

a1

(

T

I

),

b1

F

]

π

1

=

A

(

T

I

) (

T

F

)

S

0

= {

φ

1

id

|

φ

1

S

1

,

π

1

satsP,1

S

1

}

π

P,0

sats

S

0

where

1

=

0[

π

0

,

π

]

,giving

1

(

π

0

)

=

((

4

,

3

,

1

),

{

π

}

)

;then:

sats1

(

π

1

,

P

,

{

φ

|

,

{

π

2

}

,

π

0

}

)

φ

= [

a2

I

,

b2

T

F

]

,

π

2

=

A

I

(

T2

F

)

S

1

= {

φ

2

id

|

φ

2

S

2

,

π

2

satsP,2

S

2

}

(12)

V=fv(τ) Cu V

P,0

sats{φ}

C⇒τ⊢PimprCrV⇒τ

Fig. 3.Constraint set improvement.

where

2

=

1[

π

0

,

π

1].Since

ν

(

π

1

)

=

4,whichisequaltothefirstcomponentof

1

(

π

0

).

I,and

π

1isnotin

1

(

π

0

).

,we obtainthat

S

2

= {

id

}

and

π

isthussatisfiable(sincesats1

(

A

I

(

T2

F

),

P

)

= {

(

id

,

,

A

I

(

T2

F

)

}

).

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 Cu

fv(τ) (the subset ofconstraintsof C withunreachable type variables)andremovesCu

fv(τ) ifeachconstraintinthissubsethasasinglesatisfyingsubstitution. Iftheset

S

ofsatisfiableinstancesofCu

fv(τ)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

)

reducesto

D;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

,causing

tobeupdated 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 substitutionwherea

mapstoInt,nottheunionofcomputingsatisfiabilityforAa andBaseparately.

Rule (

inst

) specifiesthat if thereexists a constraintaxiom

α

.

C

A

τ

, such that A

τ

matches withan input con-straint

π

,then

π

reducestoanyconstraintset D thatC reducesto.

(13)

∅ ⊢Pred,∅;(red)

{π} ⊢Pred,C;1DredP,1D′;′

{π} ∪DredP,CD;′ (conj)

matchesπ, (P, ),{(C,π′, )}

CPred,′D;′′

{π} ⊢redP,D;′′ (inst)

matchesπ, (P, ),{(C,π′, )}

CredP,′D;Fail

{π} ⊢Pred,{π};Fail (stop0)

matchesπ, (P, ),{(C,π′,Fail)}

{π} ∪CredP,{π} ∪C;Fail (stop)

Fig. 4.Context reduction.

Expressions e::=xx.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 as

class

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 ifandonlyifoverloadingforC

hasbeenresolvedandthereexistsasinglesatisfyingsubstitutionforC;seeFig. 8).

Imagem

Fig. 6. Types, constraints and meta-variable usage.
Fig. 7. Partial order on types, constraints and typing contexts.
Fig. 10. Type inference.
Fig. 11. Core Haskell semantics.

Referências

Documentos relacionados

[r]

Regarding the main factors associated with frequent readmissions, it was noticed that there is no concordance among the findings, as many studies did not

Treatment of B-ALL primary cells n=3 and cell lines n=2 with the highly specific CK2 inhibitor CX-4945 resulted in decreased PTEN phosphorylation at the CK2 target residue S380

As investigadoras sugerem como possível explicação para estes resultados baixos, o facto do desenvolvimento da competência prosódica de produção do Foco ainda não se

Além de situações relacionadas ao ambiente esco- lar, foi avaliado que Patrícia apresentava também avanços em relação a ser mais expressiva e espontânea com outras pessoas de

Foram detectados 17 compostos orgânicos voláteis, dos quais somente o isopreno foi emitido pela fonte biogênica local, pois, devido às condições meteorológicas

The truth is that, as we have seen, reduction of uncertainty may be associated with an increase in expected surprise and that means that problems of stability may arise when we

Da caracterização da população com diabetes tipo 2, prévia à introdução de VNI, destaque para a média etária superior a 64 anos, que enfatiza não só, a idade como