effect.tri (2684B)
1 !import prelude * 2 !import io * 3 !import consumers_structural as S 4 !export * 5 -- Generic one-shot external operation / named receiver boundary for SCP. 6 -- No HTTP, path, route, or filesystem semantics live in this driver. 7 8 -- Step commands: Done value or Await operation argument receiver environment. 9 -- Resumption inputs: (1, value) on success; (0, message) on failure. 10 -- Receiver lookup, including the initial call, uses the same registry. 11 done value = pair 0 value 12 request operation argument receiver environment = 13 pair 1 (pair operation (pair argument (pair receiver environment))) 14 15 lookup_ self entries name = lazyList (_ : nothing) (entry rest : lazyBool (_ : just (snd entry)) (_ : self rest name) (equal? (fst entry) name)) entries 16 lookup entries name = y lookup_ entries name 17 18 resume self step receivers operations operation argument nextReceiver retained = 19 lazyMaybe (_ : pure (err "unknown receiver" t)) 20 (program : lazyMaybe (_ : pure (err "unknown operation" t)) 21 (handler : onResult_ (handler argument) 22 (problem : self (pair 0 problem) step receivers operations program retained) 23 (value : self (pair 1 value) step receivers operations program retained)) 24 (lookup operations operation)) 25 (lookup receivers nextReceiver) 26 27 interpretRequest self step receivers operations payload = matchPair (operation rest : matchPair (argument more : matchPair 28 (nextReceiver retained : resume self step receivers operations 29 operation argument nextReceiver retained) more) rest) payload 30 interpretCommand self step receivers operations command = matchPair (tag payload : lazyBool (_ : pure (ok payload t)) 31 (_ : lazyBool (_ : interpretRequest self step receivers operations payload) 32 (_ : pure (err "unknown command" t)) (equal? tag 1)) 33 (equal? tag 0)) command 34 35 -- Carry the already-resolved executable through IO. Resolution still happens 36 -- before invoking a handler; resumption must not scan the registry again. 37 run_ self input step receivers operations program environment = 38 lazyResult 39 (message _ : pure (err message t)) 40 (command _ : interpretCommand self step receivers operations command) 41 (step (S.makeConsumer environment (snd program)) input) 42 -- Consume the invocation input before the (possibly static) receiver 43 -- registry drives the fixed point during top-level normalization. 44 receiverFromInput receiver input = triage receiver (_ : receiver) (_ _ : receiver) input 45 run step receivers operations receiver input environment = 46 lazyMaybe (_ : pure (err "unknown receiver" t)) 47 (program : y run_ input step receivers operations program environment) 48 (lookup receivers (receiverFromInput receiver input))