123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133(************************************************************************)(* * The Coq Proof Assistant / The Coq Development Team *)(* v * Copyright INRIA, CNRS and contributors *)(* <O___,, * (see version control and CREDITS file for authors & dates) *)(* \VV/ **************************************************************)(* // * This file is distributed under the terms of the *)(* * GNU Lesser General Public License Version 2.1 *)(* * (see LICENSE file for the text of the license) *)(************************************************************************)type'at=|Nil|Consof'a*'at|Defaultofint*'at(* Invariant: no two consecutive [Default] nodes, the integer is always > 0 *)letempty=Nilletconsxl=Cons(x,l)letdefaultnnl=ifInt.equaln0thenlelsematchlwith|Nil|Cons_->Default(n,l)|Default(m,l)->Default(n+m,l)letdefaultl=matchlwith|Nil|Cons_->Default(1,l)|Default(m,l)->Default(m+1,l)letcons_optol=matchowith|None->defaultl|Somex->consxlletis_empty=function|Nil->true|Cons_|Default_->falseletis_default=function|Nil->true|Default(_,Nil)->true|Cons_|Default_->falseletview=function|Nil->None|Cons(x,l)->Some(Somex,l)|Default(1,l)->Some(None,l)|Default(n,l)->Some(None,Default(n-1,l))letrecto_listl=matchlwith|Nil->[]|Cons(x,l)->Somex::to_listl|Default(n,l)->letl=to_listlinletreciteratenl=ifn<=0thenlelseiterate(n-1)(None::l)initeratenlletof_full_listl=List.fold_rightconslemptyletequaleql1l2=leteqo1o2=matcho1,o2with|None,None->true|Somex1,Somex2->eqx1x2|Some_,None|None,Some_->falseinCList.for_all2eqeq(to_listl1)(to_listl2)letcomparecmpl1l2=CList.compare(Option.comparecmp)(to_listl1)(to_listl2)letlengthl=letreclengthn=function|Nil->n|Cons(_,l)->length(n+1)l|Default(k,l)->length(k+n)linlength0lmoduleSkip=structletreciterf=function|Nil->()|Cons(x,l)->let()=fxiniterfl|Default(_,l)->iterflletrecmapf=function|Nil->Nil|Cons(x,l)->Cons(fx,mapfl)|Default(n,l)->Default(n,mapfl)letrecfoldfaccu=function|Nil->accu|Cons(x,l)->foldf(faccux)l|Default(_,l)->foldfacculletrecfor_allfl=matchlwith|Nil->true|Cons(x,l)->fx&&for_allfl|Default(_,l)->for_allflletrecexistsfl=matchlwith|Nil->false|Cons(x,l)->fx||existsfl|Default(_,l)->existsflendmoduleSmart=structletrecmapfl=matchlwith|Nil->empty|Cons(x,r)->letx'=fxinletr'=mapfrinifx'==x&&r'==rthenlelseconsx'r'|Default(n,r)->letr'=mapfrinifr'==rthenlelseDefault(n,r')letrecfold_left_mapfaccul0=matchl0with|Nil->accu,empty|Cons(x,l)->letaccu,x'=faccuxinletaccu,l'=fold_left_mapfacculinletr=ifx'==x&&l'==lthenl0elseCons(x',l')inaccu,r|Default(n,l)->letaccu,l'=fold_left_mapfacculinletr=ifl'==lthenl0elseDefault(n,l')inaccu,rend