step table sh@(S (t:ts) [] oo ru) = case t of
  List _ -> S ts [s2t t] ([]:oo) StackApp
  Atom x -> case findBegin table x of
    NoBegin   -> S ts [s2t t] ([]:oo) StackApp
    -- x is the first sub-op, and the stack is empty
    Begin pt1 -> go pt1
    MissingBegin xs -> rule sh (failure $ xs `MissingBefore` x)
    AmbiguousBegin amb -> rule sh (failure $ Ambiguity amb)
  where
    go pt1
      | leftOpen pt1 && isInitial ru =
      rule sh (failure $ MissingSubBefore $ symbol pt1)
      | leftOpen pt1 =
      S ts [Part pt1] oo StackOp
      | rightHole pt1 == Just SExpression =
      S ts [Part pt1] ([]:oo) StackL
      | otherwise =
      S ts [Part pt1] oo StackL

-- Everything is done and fine.
step table sh@(S (t:ts) [] oo ru) = case t of
  List _ -> S ts [s2t t] ([]:oo) StackApp
  Atom x -> case findBegin table x of
    NoBegin   -> S ts [s2t t] ([]:oo) StackApp
    -- x is the first sub-op, and the stack is empty
    Begin pt1 -> go pt1
    MissingBegin xs -> rule sh (failure $ xs `MissingBefore` x)
    AmbiguousBegin amb -> rule sh (failure $ Ambiguity amb)
  where
    go pt1
      | leftOpen pt1 && isInitial ru =
      rule sh (failure $ MissingSubBefore $ symbol pt1)
      | leftOpen pt1 =
      S ts [Part pt1] oo StackOp
      | rightHole pt1 == Just SExpression =
      S ts [Part pt1] ([]:oo) StackL
      | otherwise =
      S ts [Part pt1] oo StackL

-- Everything is done and fine.
step table sh@(S (t:ts) [] oo ru) = case t of
  List _ -> S ts [s2t t] ([]:oo) StackApp
  Atom x -> case findBegin table x of
    NoBegin   -> S ts [s2t t] ([]:oo) StackApp
    -- x is the first sub-op, and the stack is empty
    Begin pt1 -> go pt1
    MissingBegin xs -> rule sh (failure $ xs `MissingBefore` x)
    AmbiguousBegin amb -> rule sh (failure $ Ambiguity amb)
  where
    go pt1
      | leftOpen pt1 && isInitial ru =
      rule sh (failure $ MissingSubBefore $ symbol pt1)
      | leftOpen pt1 =
      S ts [Part pt1] oo StackOp
      | rightHole pt1 == Just SExpression =
      S ts [Part pt1] ([]:oo) StackL
      | otherwise =
      S ts [Part pt1] oo StackL

-- Everything is done and fine.