import Base import ./entity_key.bend as Key import ./world.bend as W import ./restore_data.bend as Basic import ./relation_types.bend as G import ./relation_graph.bend as Graph type Mapped is Data: Mapped{graph:G.Graph} MissingBinding{saved:Key.Key} def target(~S:Data,descriptor:G.Descriptor,source:W.Handle,saved:Key.Key,result:Maybe<&2,W.Handle>,graph:G.Graph,next:G.Graph -> Mapped) -> Mapped: match source result graph: case _ None{} _:MissingBinding{saved} case W.Handle{_,source} Some{W.Handle{_,target}} G.Graph{edges,inverses}:next(Graph.updated(~S,False{},edges,inverses,descriptor,source,target)) def source(~S:Data,descriptor:G.Descriptor,saved:Key.Key,+other:Key.Key,+bindings:List<&2,Basic.Binding>,result:Maybe<&2,W.Handle>,graph:G.Graph,next:G.Graph -> Mapped) -> Mapped: match result: case None{}:MissingBinding{saved} case Some{handle}:target(~S,descriptor,handle,other,Basic.lookup(~S,other,bindings),graph,next) def edges(~S:Data,values:List<&2,G.Edge>,+bindings:List<&2,Basic.Binding>,graph:G.Graph) -> Mapped: match values: case []:Mapped{graph} case G.Edge{descriptor,+saved,other} <> rest:source(~S,descriptor,saved,other,bindings,Basic.lookup(~S,saved,bindings),graph,graph => edges(~S,rest,bindings,graph)) def remap(~S:Data,graph:G.Graph,bindings:List<&2,Basic.Binding>) -> Mapped: match graph: case G.Graph{values,_}:edges(~S,values,bindings,G.Graph{[],[]})