Afewdetailshavebeenomittedabove.InChapter3,welearnedthatinductivetypefamiliesmayhaveboth%\index{parameters}%_parameters_andregulararguments.Withinan[in]clause,aparameterpositionmusthavethewildcard[_]written,insteadofavariable.(Ingeneral,Coquseswildcard[_]'seithertoindicatepatternvariablesthatwillnotbementionedagainortoindicatepositionswherewewouldliketypeinferencetoinfertheappropriateterms.)Furthermore,recentCoqversionsareaddingmoreandmoreheuristicstoinferdependent[match]annotationsincertainconditions.Thegeneralannotationinferenceproblemisundecidable,sotherewillalwaysbeseriouslimitationsonhowmuchworktheseheuristicscando.Whenindoubtaboutwhyaparticulardependent[match]isfailingtotype-check,addanexplicit[return]annotation!Atthatpoint,themechanicalrulesketchedinthissectionwillprovideacompleteaccountof%``%#"#what the type checker is thinking.#"#%''%Besuretoavoidthecommonpitfallofwritinga[return]annotationthatdoesnotmentionanyvariablesboundby[in]or[as];sucha[match]willneverrefinetypingrequirementsbasedonwhichpatternhasmatched.(Onesimpleexceptiontothisruleisthat,whenthediscrimineeisavariable,thatsamevariablemaybetreatedasifitwererepeatedasan[as]clause.)*)
Afewdetailshavebeenomittedabove.InChapter3,welearnedthatinductivetypefamiliesmayhaveboth%\index{parameters}%_parameters_andregulararguments.Withinan[in]clause,aparameterpositionmusthavethewildcard[_]written,insteadofavariable.(Ingeneral,Coquseswildcard[_]'seithertoindicatepatternvariablesthatwillnotbementionedagainortoindicatepositionswherewewouldliketypeinferencetoinfertheappropriateterms.)Furthermore,recentCoqversionsareaddingmoreandmoreheuristicstoinferdependent[match]annotationsincertainconditions.Thegeneralannotationinferenceproblemisundecidable,sotherewillalwaysbeseriouslimitationsonhowmuchworktheseheuristicscando.Whenindoubtaboutwhyaparticulardependent[match]isfailingtotype-check,addanexplicit[return]annotation!Atthatpoint,themechanicalrulesketchedinthissectionwillprovideacompleteaccountof"what the type checker is thinking."Besuretoavoidthecommonpitfallofwritinga[return]annotationthatdoesnotmentionanyvariablesboundby[in]or[as];sucha[match]willneverrefinetypingrequirementsbasedonwhichpatternhasmatched.(Onesimpleexceptiontothisruleisthat,whenthediscrimineeisavariable,thatsamevariablemaybetreatedasifitwererepeatedasan[as]clause.)*)
Aquickwordofencouragement:Afterwritingthiscode,evenIdonotunderstandtheprecisedetailsofhowbalancingworks!IconsultedChrisOkasaki'spaper%``%#"#Red-Black Trees in a Functional Setting#"#%''~\cite{Okasaki}%andtranscribedthecodetousedependenttypes.Luckily,thedetailsarenotsoimportanthere;typesalonewilltellusthatinsertionpreservesbalanced-ness,andwewillprovethatinsertionproducestreescontainingtherightkeys.*)
Aquickwordofencouragement:Afterwritingthiscode,evenIdonotunderstandtheprecisedetailsofhowbalancingworks!IconsultedChrisOkasaki'spaper"Red-Black Trees in a Functional Setting"%\cite{Okasaki}%andtranscribedthecodetousedependenttypes.Luckily,thedetailsarenotsoimportanthere;typesalonewilltellusthatinsertionpreservesbalanced-ness,andwewillprovethatinsertionproducestreescontainingtherightkeys.*)
Definitionbalance1n(a:rtreen)(data:nat)c2:=
matchainrtreenreturnrbtreec2n
...
...
@@ -574,7 +578,7 @@ Definition balance1 n (a : rtree n) (data : nat) c2 :=