import Base import ./ordinary_app_registration_fields.bend as Stored # Advanced application carrier pairing actual affine registration Token # with its detached Fields. Fields alone do not replace registration authority; # keep token and metadata from the same registration together. type Record is Type: Record{token:Token,fields:Stored.Fields} # Return the record owner together with its one detached Fields entry. # Does not invoke the registration, advance its cursor or read payloads. def observed(~S:Data,~Token:Type,record:Record) -> Record & List<&2,Stored.Fields>: match record: case Record{token,+fields}:(Record{token,fields},[fields]) def second(~S:Data,~Left:Type,~Right:Type,left:Left,before:List<&2,Stored.Fields>,result:Right & List<&2,Stored.Fields>) -> (Left & Right) & List<&2,Stored.Fields>: match result: case (right,after):((left,right),List.append(&2,Stored.Fields,before,after)) def first(~S:Data,~Left:Type,~Right:Type,rightFold:Right -> Right & List<&2,Stored.Fields>,right:Right,result:Left & List<&2,Stored.Fields>) -> (Left & Right) & List<&2,Stored.Fields>: match result: case (left,before):second(~S,~Left,~Right,left,before,rightFold(right)) # Advanced metadata fold over heterogeneous owners in left/right order. # Returns both owners and appended detached fields; supplied folds must # preserve their owned registrations rather than fabricate authority. def pair(~S:Data,~Left:Type,~Right:Type,leftFold:Left -> Left & List<&2,Stored.Fields>,rightFold:Right -> Right & List<&2,Stored.Fields>,owners:Left & Right) -> (Left & Right) & List<&2,Stored.Fields>: match owners: case (left,right):first(~S,~Left,~Right,rightFold,right,leftFold(left)) # Unit-owner base metadata fold, returning the same Unit and no fields. # Does not register a system or infer metadata from arbitrary owners. def empty(~S:Data,unit:Unit) -> Unit & List<&2,Stored.Fields>: (unit,[]) # Closed advanced adapter composing two metadata folds with the same # owner-threading and left/right order as pair; does not execute systems. def fold_pair(~S:Data,~Left:Type,~Right:Type,~left:Left -> Left & List<&2,Stored.Fields>,~right:Right -> Right & List<&2,Stored.Fields>,owners:Left & Right) -> (Left & Right) & List<&2,Stored.Fields>: pair(~S,~Left,~Right,left,right,owners)