tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

list.tri (1243B)


      1 !import recursion_support *
      2 !export listContinue
      3 !export listStop
      4 !export listFoldTailUntil
      5 !export listFoldUntil
      6 
      7 -- Decisions carry exact booleans, not numbers or truthy trees.
      8 listContinue state = pair (t t) state
      9 listStop state = pair t state
     10 
     11 -- step head tail state -> decision. Continue consumes the current cell;
     12 -- Stop commits its state but retains the whole unconsumed suffix, including
     13 -- that cell. Return (final state, remaining input), with no tail reconstruction.
     14 -- Empty input never calls step. Malformed list tails/decisions halt without
     15 -- losing the current input or committing a malformed decision's state.
     16 listFoldTailUntil input initial step = (let rec
     17   walk [] state = pair state []
     18   walk whole{[head | tail]} state = (let
     19     decide ((t t), next) = walk tail next
     20     decide (t, next) = pair next whole
     21     decide decision = pair state whole
     22     in decide (step head tail state))
     23   walk remaining state = pair state remaining
     24   in walk input initial)
     25 
     26 -- Ordinary early-stopping fold: callbacks that do not need the original tail
     27 -- use this adapter rather than repeating an unused argument in every clause.
     28 listFoldUntil input initial step = (listFoldTailUntil
     29   input initial (head tail state : step head state))