123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318(** Applicatives model computations in which values computed by subcomputations cannot
affect what subsequent computations will take place.
Relative to monads, this restriction takes power away from the user of the interface
and gives it to the implementation. In particular, because the structure of the
entire computation is known, one can augment its definition with some description of
that structure.
For more information, see:
{v
Applicative Programming with Effects.
Conor McBride and Ross Paterson.
Journal of Functional Programming 18:1 (2008), pages 1-13.
http://staff.city.ac.uk/~ross/papers/Applicative.pdf
v} *)open!ImportmoduletypeBasic=sigtype'atvalreturn:'a->'atvalapply:('a->'b)t->'at->'bt(** The following identities ought to hold for every Applicative (for some value of =):
- identity: [return Fn.id <*> t = t]
- composition: [return Fn.compose <*> tf <*> tg <*> tx = tf <*> (tg <*> tx)]
- homomorphism: [return f <*> return x = return (f x)]
- interchange: [tf <*> return x = return (fun f -> f x) <*> tf]
Note: <*> is the infix notation for apply. *)(** The [map] argument to [Applicative.Make] says how to implement the applicative's
[map] function. [`Define_using_apply] means to define [map t ~f = return f <*> t].
[`Custom] overrides the default implementation, presumably with something more
efficient.
Some other functions returned by [Applicative.Make] are defined in terms of [map],
so passing in a more efficient [map] will improve their efficiency as well. *)valmap:[`Define_using_apply|`Customof('at->f:('a->'b)->'bt)]endmoduletypeBasic_using_map2=sigtype'atvalreturn:'a->'atvalmap2:'at->'bt->f:('a->'b->'c)->'ctvalmap:[`Define_using_map2|`Customof('at->f:('a->'b)->'bt)]endmoduletypeApplicative_infix=sigtype'atval(<*>):('a->'b)t->'at->'bt(** same as [apply] *)val(<*):'at->unitt->'atval(*>):unitt->'at->'atval(>>|):'at->('a->'b)->'btendmoduletypeFor_let_syntax=sigtype'atvalreturn:'a->'atvalmap:'at->f:('a->'b)->'btvalboth:'at->'bt->('a*'b)tincludeApplicative_infixwithtype'at:='atendmoduletypeS=sigincludeFor_let_syntaxvalapply:('a->'b)t->'at->'btvalmap2:'at->'bt->f:('a->'b->'c)->'ctvalmap3:'at->'bt->'ct->f:('a->'b->'c->'d)->'dtvalall:'atlist->'alisttvalall_unit:unittlist->unittvalall_ignore:unittlist->unitt[@@deprecated"[since 2018-02] Use [all_unit]"]moduleApplicative_infix:Applicative_infixwithtype'at:='atendmoduletypeLet_syntax=sigtype'atmoduleOpen_on_rhs_intf:sigmoduletypeSendmoduleLet_syntax:sigvalreturn:'a->'atincludeApplicative_infixwithtype'at:='atmoduleLet_syntax:sigvalreturn:'a->'atvalmap:'at->f:('a->'b)->'btvalboth:'at->'bt->('a*'b)tmoduleOpen_on_rhs:Open_on_rhs_intf.Sendendend(** Argument lists and associated N-ary map and apply functions. *)moduletypeArgs=sigtype'aarg(** the underlying applicative *)(** ['f] is the type of a function that consumes the list of arguments and returns an
['r]. *)type('f,'r)t(** the empty argument list **)valnil:('r,'r)t(** prepend an argument *)valcons:'aarg->('f,'r)t->('a->'f,'r)t(** infix operator for [cons] *)val(@>):'aarg->('f,'r)t->('a->'f,'r)t(** Transform argument values in some way. For example, one can label a function
argument like so:
{[
step ~f:(fun f x -> f ~foo:x) : ('a -> 'r1, 'r2) t -> (foo:'a -> 'r1, 'r2) t
]} *)valstep:('f1,'r)t->f:('f2->'f1)->('f2,'r)t(** The preferred way to factor out an [Args] sub-sequence:
{[
let args =
Foo.Args.(
bar "A"
(* TODO: factor out the common baz qux sub-sequence *)
@> baz "B"
@> qux "C"
@> zap "D"
@> nil
)
]}
is to write a function that prepends the sub-sequence:
{[
let baz_qux remaining_args =
Foo.Args.(
baz "B"
@> qux "C"
@> remaining_args
)
]}
and splice it back into the original sequence using [@@] so that things line up
nicely:
{[
let args =
Foo.Args.(
bar "A"
@> baz_qux
@@ zap "D"
@> nil
)
]} *)valmapN:f:'f->('f,'r)t->'rargvalapplyN:'farg->('f,'r)t->'rargend[@@deprecated"[since 2018-09] Use [ppx_let] instead."]moduletypeBasic2=sigtype('a,'e)tvalreturn:'a->('a,_)tvalapply:('a->'b,'e)t->('a,'e)t->('b,'e)tvalmap:[`Define_using_apply|`Customof(('a,'e)t->f:('a->'b)->('b,'e)t)]endmoduletypeBasic2_using_map2=sigtype('a,'e)tvalreturn:'a->('a,_)tvalmap2:('a,'e)t->('b,'e)t->f:('a->'b->'c)->('c,'e)tvalmap:[`Define_using_map2|`Customof(('a,'e)t->f:('a->'b)->('b,'e)t)]endmoduletypeS2=sigtype('a,'e)tvalreturn:'a->('a,_)tvalapply:('a->'b,'e)t->('a,'e)t->('b,'e)tvalmap:('a,'e)t->f:('a->'b)->('b,'e)tvalmap2:('a,'e)t->('b,'e)t->f:('a->'b->'c)->('c,'e)tvalmap3:('a,'e)t->('b,'e)t->('c,'e)t->f:('a->'b->'c->'d)->('d,'e)tvalall:('a,'e)tlist->('alist,'e)tvalall_unit:(unit,'e)tlist->(unit,'e)tvalall_ignore:(unit,'e)tlist->(unit,'e)t[@@deprecated"[since 2018-02] Use [all_unit]"]valboth:('a,'e)t->('b,'e)t->('a*'b,'e)tmoduleApplicative_infix:sigval(<*>):('a->'b,'e)t->('a,'e)t->('b,'e)tval(<*):('a,'e)t->(unit,'e)t->('a,'e)tval(*>):(unit,'e)t->('a,'e)t->('a,'e)tval(>>|):('a,'e)t->('a->'b)->('b,'e)tendincludemoduletypeofApplicative_infixendmoduletypeArgs2=sigtype('a,'e)argtype('f,'r,'e)tvalnil:('r,'r,_)tvalcons:('a,'e)arg->('f,'r,'e)t->('a->'f,'r,'e)tval(@>):('a,'e)arg->('f,'r,'e)t->('a->'f,'r,'e)tvalstep:('f1,'r,'e)t->f:('f2->'f1)->('f2,'r,'e)tvalmapN:f:'f->('f,'r,'e)t->('r,'e)argvalapplyN:('f,'e)arg->('f,'r,'e)t->('r,'e)argend[@@deprecated"[since 2018-09] Use [ppx_let] instead."]moduletypeApplicative=sigmoduletypeApplicative_infix=Applicative_infixmoduletypeArgs=Args[@@warning"-3"][@@deprecated"[since 2018-09] Use [ppx_let] instead."]moduletypeArgs2=Args2[@@warning"-3"][@@deprecated"[since 2018-09] Use [ppx_let] instead."]moduletypeBasic=BasicmoduletypeBasic2=Basic2moduletypeBasic2_using_map2=Basic2_using_map2moduletypeBasic_using_map2=Basic_using_map2moduletypeLet_syntax=Let_syntaxmoduletypeS=SmoduletypeS2=S2moduleArgs_to_Args2(X:Args):Args2withtype('a,'e)arg='aX.argwithtype('f,'r,'e)t=('f,'r)X.t[@@warning"-3"]moduleS2_to_S(X:S2):Swithtype'at=('a,unit)X.tmoduleS_to_S2(X:S):S2withtype('a,'e)t='aX.tmoduleMake(X:Basic):Swithtype'at:='aX.tmoduleMake2(X:Basic2):S2withtype('a,'e)t:=('a,'e)X.tmoduleMake_let_syntax(X:For_let_syntax)(Intf:sigmoduletypeSend)(Impl:Intf.S):Let_syntaxwithtype'at:='aX.twithmoduleOpen_on_rhs_intf:=IntfmoduleMake_using_map2(X:Basic_using_map2):Swithtype'at:='aX.tmoduleMake2_using_map2(X:Basic2_using_map2):S2withtype('a,'e)t:=('a,'e)X.tmoduleMake_args(X:S):Argswithtype'aarg:='aX.t[@@warning"-3"][@@deprecated"[since 2018-09] Use [ppx_let] instead."]moduleMake_args2(X:S2):Args2withtype('a,'e)arg:=('a,'e)X.t[@@warning"-3"][@@deprecated"[since 2018-09] Use [ppx_let] instead."](** The following functors give a sense of what Applicatives one can define.
Of these, [Of_monad] is likely the most useful. The others are mostly didactic. *)(** Every monad is Applicative via:
{[
let apply mf mx =
mf >>= fun f ->
mx >>| fun x ->
f x
]} *)moduleOf_monad(M:Monad.S):Swithtype'at:='aM.tmoduleCompose(F:S)(G:S):Swithtype'at='aF.tG.tmodulePair(F:S)(G:S):Swithtype'at='aF.t*'aG.tend