Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file traversable.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269(* This file is part of 'travesty'.
Copyright (c) 2018, 2019, 2020 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. *)openBaseopenTraversable_types(** [Derived_ops_maker] is an internal module type used for implementing the
derived operations (fold-map, fold, iterate) in an arity-generic way. *)moduletypeDerived_ops_maker=sigincludeGeneric_types.GenericmoduleOn_monad(M:Monad.S):Basic_generic_on_monadwithmoduleM:=Mandtype'at:='atandtype'aelt:='aeltend(** [Derived_ops_monadic_gen] is an internal functor used to generate several
derived monadic operations (monadic fold-map, monadic iteration, etc)
from a monadic traversal in an arity-generic way. *)moduleDerived_ops_monadic_gen(I:Derived_ops_maker)(M:Monad.S)=struct(* We use the state monad to implement fold-map. *)moduleSM=State_transform.Make2(M)moduleIM=I.On_monad(M)letfold_map_m(typeacc)c~f~init=letmoduleSM'=State_transform.To_S(SM)(structtypet=accend)inletmoduleISM=I.On_monad(SM')inSM.run'(ISM.map_m~f:(funx->SM.Monadic.make(funs->fsx))c)initletfold_mc~init~f=M.(fold_map_m~initc~f:(funkx->fkx>>|funx'->(x',x))>>|fst)letiter_mc~f=M.(IM.map_m~f:(funx->M.(fx>>|fun()->x))c>>|fun_->())letmapi_m~fc=M.(fold_map_m~init:0c~f:(funkx->fkx>>|funx'->(k+1,x'))>>|snd)end(** Internal functor for generating several derived non-monadic,
non-[Container] operations (map, iterate) from a fold-map, generic over
both arity-0 and arity-1. *)moduleDerived_ops_gen(I:Derived_ops_maker)=struct(* As usual, we just use the monadic equivalents over the identity monad. *)moduleD=Derived_ops_monadic_gen(I)(Monad.Ident)letfold_map=D.fold_map_mletmapi=D.mapi_mend(* Basic-signature modules need a bit of rearrangement to fit in the derived
operation functors. *)(** Internal functor for rearranging arity-0 basics to derived-ops makers. *)moduleBasic0_to_derived_ops_maker(I:Basic0):Derived_ops_makerwithtype'at=I.tandtype'aelt=I.Elt.tandmoduleOn_monad=I.On_monad=structtype'at=I.ttype'aelt=I.Elt.tmoduleOn_monad=I.On_monadend(** Internal functor for rearranging arity-1 basics to derived-ops makers. *)moduleBasic1_to_derived_ops_maker(I:Basic1):Derived_ops_makerwithtype'at='aI.tandtype'aelt='aandmoduleOn_monad=I.On_monad=structtype'at='aI.ttype'aelt='amoduleOn_monad=I.On_monadend(** [Container_gen] is an internal functor used to generate the input to
[Container] functors in an arity-generic way. *)moduleContainer_gen(I:Derived_ops_maker):sigvalfold:'aI.t->init:'acc->f:('acc->'aI.elt->'acc)->'accvaliter:[>`Customof'aI.t->f:('aI.elt->unit)->unit]vallength:[>`Define_using_fold]end=structmoduleD=Derived_ops_monadic_gen(I)(Monad.Ident)letfold=D.fold_mletiter=`CustomD.iter_mletlength=`Define_using_foldendmoduleMake0_container(I:Basic0_container):S0withmoduleElt=I.Eltandtypet=I.t=structmoduleMaker=Basic0_to_derived_ops_maker(I)moduleElt=I.Elttypeelt=I.Elt.t(* We can implement the non-monadic map using the identity monad. *)moduleIdent=I.On_monad(Monad.Ident)letmap=Ident.map_mincludeDerived_ops_gen(Maker)include(I:Container.S0withtypet=I.tandtypeelt:=elt)moduleOn_monad(MS:Monad.S)=structincludeI.On_monad(MS)includeDerived_ops_monadic_gen(Maker)(MS)endmoduleWith_errors=On_monad(Or_error)endmoduleMake0(I:Basic0):S0withmoduleElt=I.Eltandtypet=I.t=Make0_container(structincludeIincludeContainer.Make0(structincludeIincludeContainer_gen(Basic0_to_derived_ops_maker(I))end)end)moduleMake1_container(I:Basic1_container):S1withtype'at='aI.t=structtypenonrec'at='aI.t(* [I] needs a bit of rearrangement to fit in the derived operation
functors (as above, but slightly differently). *)moduleMaker=Basic1_to_derived_ops_maker(I)moduleIdent=I.On_monad(Monad.Ident)letmap=Ident.map_mincludeDerived_ops_gen(Maker)moduleC:Container.S1withtype'at:='aI.t=IincludeCincludeMappable.Extend1(structtypenonrec'at='aI.tletmap=mapincludeCend)moduleOn_monad(MS:Monad.S)=structincludeI.On_monad(MS)includeDerived_ops_monadic_gen(Maker)(MS)(* [sequence_m] can't be defined on arity-0 containers. *)letsequence_mc=map_m~f:Fn.idcendmoduleWith_errors=On_monad(Base.Or_error)endmoduleMake1(I:Basic1):S1withtype'at='aI.t=Make1_container(structincludeIincludeContainer.Make(structtypenonrec'at='aI.tincludeContainer_gen(Basic1_to_derived_ops_maker(I))end)end)moduleChain0(Outer:S0)(Inner:S0withtypet:=Outer.Elt.t):S0withmoduleElt=Inner.Eltandtypet=Outer.t=Make0(structtypet=Outer.tmoduleElt=Inner.EltmoduleOn_monad(M:Monad.S)=structmoduleOM=Outer.On_monad(M)moduleIM=Inner.On_monad(M)letmap_mx~f=OM.map_mx~f:(IM.map_m~f)endend)moduleFix_elt(I:S1)(Elt:Equal.S):S0withtypet=Elt.tI.tandmoduleElt=Elt=Make0(structtypet=Elt.tI.tmoduleElt=Elt(* The [S0] fold-map has a strictly narrower function type than the [S1]
one, so we can just supply the same [On_monad]. *)moduleOn_monad(M:Monad.S)=I.On_monad(M)end)moduleConst(T:T)(Elt:Equal.S)=Make0(structtypet=T.tmoduleElt=EltmoduleOn_monad(M:Monad.S)=structletmap_m(x:t)~(f:Elt.t->Elt.tM.t):tM.t=ignoref;M.returnxendend)moduleHelpers(M:Monad.S)=structtype'atraversal='a->'aM.tletproc_variant0fv=M.(f()>>|fun()->v.Base.Variant.constructor)letproc_variant1fva=M.(fa>>|v.Base.Variant.constructor)letproc_variant2fvab=letopenM.Let_syntaxinlet%mapa',b'=f(a,b)inv.Base.Variant.constructora'b'letproc_variant3fvabc=letopenM.Let_syntaxinlet%mapa',b',c'=f(a,b,c)inv.Base.Variant.constructora'b'c'letproc_fieldfstatefield=letopenM.Let_syntaxinlet%bindcontainer=stateinletoriginal=Field.getfieldcontainerinlet%mapnval=foriginalinField.fsetfieldcontainernvalend