import Base import ./dependency_registration.bend as Reg import ./dependency_registry_program.bend as Prog import ./dependency_plan.bend as Plan import ./dependency_executor.bend as Exec type Scope is Data: Scope{members:List<&2,U32>,children:List<&2,Scope>} type Compiled<-World:Type,-Owners:Type,-Error:Data,-Out:Data,-dispatch:Owners -> World -> U32 -> Owners & (World & Exec.Invocation)> is Type: Compiled{registry:Reg.Registry,scope:Scope} def Scoped(~Error:Data,~Out:Data,~World:Type,~Owners:Type,~dispatch:Prog.Dispatch(~World,~Owners,~Error,~Out)) -> Type: Compiled def ids(nodes:List<&2,Plan.Node>) -> List<&2,U32>: match nodes: case []:[] case Plan.Node{id,_} <> rest:id <> ids(rest) def lifted(~World:Type,~Owners:Type,~Error:Data,~Out:Data,~dispatch:Prog.Dispatch(~World,~Owners,~Error,~Out),registry:Reg.Registry) -> Compiled: match registry: case Reg.Registry{owners,+nodes}:Compiled{Reg.Registry{owners,nodes},Scope{ids(nodes),[]}} def compile_embed(~Error:Data,~Out:Data,~World:Type,~Owners:Type,~dispatch:Prog.Dispatch(~World,~Owners,~Error,~Out),registry:Reg.Registry) -> Compiled: lifted(~World,~Owners,~Error,~Out,~dispatch,registry) def joined(~Error:Data,~Out:Data,~World:Type,~L:Type,~R:Type,~left:Prog.Dispatch(~World,~L,~Error,~Out),~right:Prog.Dispatch(~World,~R,~Error,~Out),leftScope:Scope,rightScope:Scope,registry:Reg.Registry,Error,Out,Reg.paired_dispatch(~World,~L,~R,~Error,~Out,~left,~right)>) -> Compiled,Error,Out,Reg.paired_dispatch(~World,~L,~R,~Error,~Out,~left,~right)>: match registry: case Reg.Registry{owners,+nodes}:Compiled{Reg.Registry{owners,nodes},Scope{ids(nodes),[leftScope,rightScope]}} def compile_join(~Error:Data,~Out:Data,~World:Type,~L:Type,~R:Type,~left:Prog.Dispatch(~World,~L,~Error,~Out),~right:Prog.Dispatch(~World,~R,~Error,~Out),l:Compiled,r:Compiled) -> Compiled,Error,Out,Reg.paired_dispatch(~World,~L,~R,~Error,~Out,~left,~right)>: match l r: case Compiled{lr,+ls} Compiled{rr,+rs}:joined(~Error,~Out,~World,~L,~R,~left,~right,ls,rs,Reg.paired(~World,~L,~R,~Error,~Out,~left,~right,lr,rr)) def compile_empty(~Error:Data,~Out:Data,~World:Type) -> Compiled: lifted(~World,~Unit,~Error,~Out,~Reg.empty_dispatch(~World,~Error,~Out),Reg.empty(~World,~Error,~Out)) def compile(~World:Type,~Owners:Type,~Error:Data,~Out:Data,~dispatch:Prog.Dispatch(~World,~Owners,~Error,~Out),~selection:Prog.Selection(~World,~Owners,~Error,~Out,~dispatch),owners:Owners) -> Compiled: selection(Scoped(~Error,~Out),compile_embed(~Error,~Out),compile_join(~Error,~Out),compile_empty(~Error,~Out),owners) type Nested is Data: Nested{path:List<&2,U32>,edges:List<&2,Plan.Edge>} type NestError is Data: UnknownScope{path:List<&2,U32>} InvalidEdges{path:List<&2,U32>,reason:Plan.Invalid} type Nesting is Data: Nesting{edges:List<&2,Plan.Edge>,invalid:Maybe<&2,NestError>} def child_selected(first:Bool,current:Scope,next:Maybe<&2,Scope>) -> Maybe<&2,Scope>: match first: case True{}:Some{current} case False{}:next def child(children:List<&2,Scope>,+index:U32) -> Maybe<&2,Scope>: match children: case []:None{} case scope <> rest:child_selected(U32.is_eq(index,0),scope,child(rest,U32.sub(index,1))) def scoped(path:List<&2,U32>,scope:Maybe<&2,Scope>) -> Maybe<&2,Scope>: match path scope: case [] scope:scope case _ None{}:None{} case index <> rest Some{Scope{_,children}}:scoped(rest,child(children,index)) def members(values:List<&2,U32>) -> List<&2,Plan.Node>: match values: case []:[] case id <> rest:Plan.Node{id,[]} <> members(rest) def checked_endpoints(path:List<&2,U32>,value:Maybe<&2,Plan.Invalid>) -> Maybe<&2,NestError>: match value: case None{}:None{} case Some{reason}:Some{InvalidEdges{path,reason}} def checked_scope(+path:List<&2,U32>,edges:List<&2,Plan.Edge>,scope:Maybe<&2,Scope>) -> Maybe<&2,NestError>: match scope: case None{}:Some{UnknownScope{path}} case Some{Scope{ids,_}}:checked_endpoints(path,Plan.endpoints(~String,edges,members(ids))) def first_nesting(first:Maybe<&2,NestError>,next:Maybe<&2,NestError>) -> Maybe<&2,NestError>: match first: case None{}:next case Some{reason}:Some{reason} def nested_next(edges:List<&2,Plan.Edge>,invalid:Maybe<&2,NestError>,next:Nesting) -> Nesting: match next: case Nesting{later,error}:Nesting{List.append(&2,Plan.Edge,edges,later),first_nesting(invalid,error)} def nested(items:List<&2,Nested>,+scope:Scope) -> Nesting: match items: case []:Nesting{[],None{}} case Nested{+path,+edges} <> rest:nested_next(edges,checked_scope(path,edges,scoped(path,Some{scope})),nested(rest,scope))