Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file zipper.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314(* This file is part of 'travesty'.
Copyright (c) 2018, 2019 by Matt Windsor
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. *)openBaseopenZipper_typesmoduletypeBasic_cell=sigtype'at[@@derivingsexp]valmake:'a->'atvaldata:'at->'avalof_data_list:'alist->'atlistvalto_data_list:'atlist->'alistincludeTravesty.Traversable_types.S1withtype'at:='atendletrev_transferamount~src~dst=ifInt.(List.lengthsrc<amount)thenNoneelseletto_transfer,src'=List.split_nsrcamountinletdst'=List.rev_appendto_transferdstinSome(src',dst')moduleMake(Cell:Basic_cell)=structtype'at={left:'aCell.tlist;right:'aCell.tlist}[@@derivingfields,sexp]letmake~left~right={left=Cell.of_data_listleft;right=Cell.of_data_listright}letof_listlst=make~left:[]~right:lstletleft_listzipper=Cell.to_data_list(leftzipper)letright_listzipper=Cell.to_data_list(rightzipper)letto_listzipper=List.rev_append(left_listzipper)(right_listzipper)letheadzipper=List.hd(rightzipper)letset_head_cell_on_rightrightnew_head=match(right,new_head)with|[],None->[]|[],Somehead'->[head']|_::rest,None->rest|_::rest,Somehead'->head'::restletset_head_cellzippernew_head={zipperwithright=set_head_cell_on_rightzipper.rightnew_head}letpushzipper~value={zipperwithright=Cell.makevalue::zipper.right}letleft_lengthzipper=List.lengthzipper.leftletright_lengthzipper=List.lengthzipper.rightletis_at_startzipper=List.is_emptyzipper.leftletis_at_endzipper=List.is_emptyzipper.right(* We split On_monad into two bits so we can use the option-monad
specialisation of some of the monadic operations to define some of the
others. *)moduleOn_monad_base(M:Monad.S)=structmoduleCM=Cell.On_monad(M)moduleCO=Cell.On_monad(Option)letpop_mzipper~on_empty=matchzipper.rightwith|[]->on_emptyzipper|x::xs->M.return(Cell.datax,{zipperwithright=xs})endmoduleOn_option_base=On_monad_base(Option)letpop_optzipper=On_option_base.pop_m~on_empty:(Fn.constNone)zipperletpeek_opt?(steps=0)zipper=letopenOption.Let_syntaxinlet%mapcell=ifsteps<0thenList.nthzipper.left(Int.abssteps-1)elseList.nthzipper.rightstepsinCell.datacellmoduleOn_monad(M:Monad.S)=structincludeOn_monad_base(M)letpeek_m?stepszipper~on_empty=matchpeek_opt?stepszipperwith|Somev->M.returnv|None->on_emptyzipperletstep_m?(steps=1)zipper~on_empty=letamount=Int.absstepsinmatchOrdering.of_int(Int.comparesteps0)with|Less->(matchrev_transferamount~src:zipper.left~dst:zipper.rightwith|Some(l,r)->M.return{left=l;right=r}|None->on_emptyzipper)|Equal->M.returnzipper|Greater->(matchrev_transferamount~src:zipper.right~dst:zipper.leftwith|Some(r,l)->M.return{left=l;right=r}|None->on_emptyzipper)letpush_left_mzipper~value~on_empty=pushzipper~value|>step_m~on_emptyletmap_m_head_cellzipper~f~on_empty=matchheadzipperwith|None->on_emptyzipper|Someh->M.(fh>>|set_head_cellzipper)letmap_m_headzipper~f~on_empty=map_m_head_cellzipper~f:M.(funh->CM.map_m~fh>>|CO.sequence_m)~on_emptyendmoduleOn_ident=On_monad(Monad.Ident)moduleOn_error=On_monad(Or_error)moduleOn_option=On_monad(Option)letto_two_listszipper=(left_listzipper,right_listzipper)letmap_head=On_ident.map_m_head~on_empty:Fn.idletpopzipper=On_error.pop_mzipper~on_empty:(fun_->Or_error.error_string"Tried to pop an exhausted zipper")letstep?stepszipper=On_error.step_m?stepszipper~on_empty:(funzipper->Or_error.error_s[%message"Zipper stepping went out of bounds"~steps:(Option.value~default:1steps:int)~left_bound:(left_lengthzipper:int)~right_bound:(right_lengthzipper:int)])(* Pushing left shouldn't fail, since the right list will always be
nonempty after the push. *)letpush_left=On_ident.push_left_m~on_empty:(fun_->assertfalse)endmodulePlain_cell:Basic_cell=structincludeSingletonletmake=Fn.idletdata=Fn.idletof_data_list=Fn.idletto_data_list=Fn.idendmodulePlain:S=Make(Plain_cell)moduleMake_marked_cell(B:Basic_mark)=structmoduleMark=structmoduleM=structincludeBincludeComparator.Make(B)endincludeMmoduleSet=structtypet=Set.M(M).t[@@derivingsexp]endendtype'at={data:'a;marks:Mark.Set.t}[@@derivingfields,sexp](** ['a t] is the type of one cell. Each cell contains the data at the
given zipper location, as well as any marks that have been attached to
the cell for later recall. *)letmakedata={data;marks=Set.empty(moduleMark)}letof_data_list=List.map~f:makeletto_data_list=List.map~f:dataletmarkcell~mark={cellwithmarks=Set.addcell.marksmark}moduleT=Travesty.Traversable.Make1(structtypenonrec'at='atmoduleOn_monad(M:Monad.S)=structletmap_mcell~f=M.(fcell.data>>|fund->{cellwithdata=d})endend)include(T:moduletypeofTwithtype'at:='at)endmoduleMake_marked(Mark:Basic_mark)=structmoduleCell=Make_marked_cell(Mark)moduleMain=Make(Cell)(* Don't include On_monad, as we extend it. *)include(Main:S_non_monadicwithtype'at='aMain.t)moduleOn_monad(M:Monad.S)=structincludeMain.On_monad(M)letmark_mzipper~mark~on_empty=map_m_head_cellzipper~f:(funh->M.return(Some(Cell.mark~markh)))~on_emptyletrecall_mzipper~mark~on_empty=letrecmuzipper'=matchList.hdzipper'.Main.rightwith|SomehwhenSet.mem(Cell.marksh)mark->M.returnzipper'|Some_|None->M.(step_m~steps:(-1)zipper'~on_empty>>=mu)inmuzipperletdelete_to_mark_mzipper~mark~on_empty=letopenM.Let_syntaxinlet%maprecalled_zipper=recall_mzipper~mark~on_emptyinletamount_to_delete=left_lengthzipper-left_lengthrecalled_zipperin{zipperwithleft=List.dropzipper.leftamount_to_delete}letrecfold_m_untilzipper~f~init~finish=letopenM.Let_syntaxinmatchpop_optzipperwith|None->finishinitzipper|Some(hd,zipper')->(match%bindfinithdzipper'with|`Stopfinal->M.returnfinal|`Dropaccum->fold_m_untilzipper'~f~init:accum~finish|`Mark(mark,hd',accum)->pushzipper'~value:hd'|>mark_m~mark~on_empty:M.return>>=step_m~on_empty:M.return>>=fold_m_until~f~init:accum~finish|`Swap(hd',accum)->push_left_mzipper'~value:hd'~on_empty:M.return>>=fold_m_until~f~init:accum~finish)endmoduleOn_ident=On_monad(Monad.Ident)moduleOn_error=On_monad(Or_error)moduleOn_option=On_monad(Option)letmarkzipper~mark=On_error.mark_mzipper~mark~on_empty:(fun_->Or_error.error_string"Tried to mark an exhausted zipper")letmark_not_foundmark=Or_error.error_s[%message"Couldn't find requested mark"~mark:(mark:Mark.t)]letrecallzipper~mark=On_error.recall_mzipper~mark~on_empty:(fun_->mark_not_foundmark)letdelete_to_markzipper~mark=On_error.delete_to_mark_mzipper~mark~on_empty:(fun_->mark_not_foundmark)letfold_until=On_ident.fold_m_untilendmoduleInt_mark_zipper:S_markedwithtypemark:=int=Make_marked(Int)