12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667open!Importinclude(structtype'at='aref[@@deriving_inlinecompare,equal,sexp,sexp_grammar]letcompare:'a.('a->'a->int)->'at->'at->int=compare_refletequal:'a.('a->'a->bool)->'at->'at->bool=equal_reflett_of_sexp:'a.(Sexplib0.Sexp.t->'a)->Sexplib0.Sexp.t->'at=ref_of_sexpletsexp_of_t:'a.('a->Sexplib0.Sexp.t)->'at->Sexplib0.Sexp.t=sexp_of_reflet(t_sexp_grammar:'aSexplib0.Sexp_grammar.t->'atSexplib0.Sexp_grammar.t)=fun_'a_sexp_grammar->ref_sexp_grammar_'a_sexp_grammar;;[@@@end]end:sigtype'at='aref[@@deriving_inlinecompare,equal,sexp,sexp_grammar]includePpx_compare_lib.Comparable.S1withtype'at:='atincludePpx_compare_lib.Equal.S1withtype'at:='atincludeSexplib0.Sexpable.S1withtype'at:='atvalt_sexp_grammar:'aSexplib0.Sexp_grammar.t->'atSexplib0.Sexp_grammar.t[@@@end]end)(* In the definition of [t], we do not have [[@@deriving compare, sexp]] because
in general, syntax extensions tend to use the implementation when available rather than
using the alias. Here that would lead to use the record representation [ { mutable
contents : 'a } ] which would result in different (and unwanted) behavior. *)type'at='aref={mutablecontents:'a}externalcreate:'a->'at="%makemutable"external(!):'at->'a="%field0"external(:=):'at->'a->unit="%setfield0"letswapt1t2=lettmp=!t1int1:=!t2;t2:=tmp;;letreplacetf=t:=f!tletset_temporarilyta~f=letrestore_to=!tint:=a;Exn.protect~f~finally:(fun()->t:=restore_to);;moduleAnd_value=structtypet=T:'aref*'a->t[@@derivingsexp_of]letset(T(r,a))=r:=aletsetsts=List.iterts~f:setletsnapshot(T(r,_))=T(r,!r)letsnapshotsts=List.mapts~f:snapshotendletsets_temporarilyand_values~f=letrestore_to=And_value.snapshotsand_valuesinAnd_value.setsand_values;Exn.protect~f~finally:(fun()->And_value.setsrestore_to);;