Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file strat.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231(**************************************************************************)(* *)(* Ocamlgraph: a generic graph library for OCaml *)(* Copyright (C) 2004-2010 *)(* Sylvain Conchon, Jean-Christophe Filliatre and Julien Signoles *)(* *)(* This software is free software; you can redistribute it and/or *)(* modify it under the terms of the GNU Library General Public *)(* License version 2.1, with the special exception on linking *)(* described in file LICENSE. *)(* *)(* This software is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)(* *)(**************************************************************************)(* Signature for graphs *)moduletypeG=sigtypetmoduleV:Sig.ORDERED_TYPEtypevertex=V.tvalmem_vertex:t->vertex->boolvalsucc:t->vertex->vertexlistvalfold_vertex:(vertex->'a->'a)->t->'a->'avalfold_succ:(vertex->'a->'a)->t->vertex->'a->'aend(* Signature for graph add-ons: an initial vertex, final vertices
and membership of vertices to either true or false,
i.e. first or second player *)moduletypePLAYER=sigtypettypevertexvalget_initial:t->vertexvalis_final:t->vertex->boolvalturn:t->vertex->boolend(* Signature for strategies : for a given state, the strategy tells
which state to go to *)moduletypeSTRAT=sigtypettypevertexvalempty:tvaladd:t->vertex->vertex->tvalnext:t->vertex->vertex(* Raises Invalid_argument if vertex's image is not defined *)end(* Implements strategy algorithms on graphs *)moduleAlgo(G:G)(P:PLAYERwithtypevertex=G.vertex)(S:STRATwithtypevertex=G.vertex):sig(* coherent_player g p returns true iff
the completion p is coherent w.r.t.
the graph g *)valcoherent_player:G.t->P.t->bool(* coherent_strat g s returns true iff
the strategy s is coherent w.r.t.
the graph g *)valcoherent_strat:G.t->S.t->bool(* game g p a b returns true iff a wins in g
given the completion p (i.e. the game
goes through a final state). *)valgame:G.t->P.t->S.t->S.t->bool(* strategy g p s returns true iff s wins in g
given the completion p, whatever strategy
plays the other player. *)valstrategy:G.t->P.t->S.t->bool(* strategyA g p returns true iff there
exists a winning stragegy for the true
player. In this case, the winning
strategy is provided. *)valstrategyA:G.t->P.t->(bool*S.t)end=structmoduleSetV=Set.Make(G.V)letreceql1l2=matchl1,l2with[],[]->true|e1::l1',e2::l2'->(G.V.comparee1e2=0)&&(eql1'l2')|_->falseletreceq_memil1l2=matchl1,l2with[],[]->(true,false)|e1::l1',e2::l2'->ifG.V.comparee1e2=0thenifG.V.comparee1i=0then(eql1'l2',true)elseeq_memil1'l2'else(false,false)|_->(false,false)letpuitgv=matchG.succgvwith[]->true|_->falseletget_finalsgp=letfal=ifP.is_finalpathena::lelselinG.fold_vertexfg[]letcoherent_playergp=G.mem_vertexg(P.get_initialp)letcoherent_stratgs=letfvb=tryletv'=S.nextsvinb&&(G.mem_vertexgv')withInvalid_argument_->trueinG.fold_vertexfgtrueletgame_pab=letrecgame_auxlpi=letcontinuex=trygame_aux(SetV.addpil)(S.nextxpi)withInvalid_argument_->falsein(P.is_finalppi)||(ifSetV.mempilthenfalseelseifP.turnppithencontinueaelsecontinueb)ingame_auxSetV.empty(P.get_initialp)letattract1gpsl=letfvl1=ifnot(List.memvl1)thenifP.turnpvthentryifList.mem(S.nextsv)l1thenv::l1elsel1withInvalid_argument_->l1elseifpuitgvthenl1elseifG.fold_succ(funv'b->b&&(List.memv'l1))gvtruethenv::l1elsel1elsel1inG.fold_vertexfglletstrategygps=letrecstrategy_auxl1l2=let(b1,b2)=eq_mem(P.get_initialp)l1l2inifb1thenb2elsestrategy_aux(attract1gpsl1)l1inletfinaux=get_finalsgpinstrategy_aux(attract1gpsfinaux)finauxletattractgp(l,l')=letfv(l1,l1')=ifnot(List.memvl1)thenifP.turnpvthenletf'v'l2=(matchl2with[]->ifList.memv'l1then[v']else[]|_->l2)in(matchG.fold_succf'gv[]with[]->(l1,l1')|v'::_->(v::l1,S.addl1'vv'))elseifpuitgvthen(l1,l1')elseifG.fold_succ(funv'b->b&&(List.memv'l1))gvtruethen(v::l1,l1')else(l1,l1')else(l1,l1')inG.fold_vertexfg(l,l')letstrategyAgp=letrecstrategyA_auxl1l2f=let(b1,b2)=eq_mem(P.get_initialp)l1l2inifb1then(b2,f)elselet(new_l1,new_f)=attractgp(l1,f)instrategyA_auxnew_l1l1new_finletfinaux=get_finalsgpinlet(l,r)=attractgp(finaux,S.empty)instrategyA_auxlfinauxr;;end