checkUnify :: Peg () -> Peg ()
checkUnify m = do
  PegState s _ _ _ _ _ p <- get
  if topIs (isList ||. (== W "]")) s
    then m
    else case maybeAny (\x -> ((,) x) <$> unify (trim isStackVar s) (x ++ [S "_rest"]) []) p of
           Nothing -> --trace (show $ map showStack p) $
                      pushAnc s >> m >> popAnc >> return ()
           Just (s', b) -> do sv <- newSVar
                              trace (showStack s ++ " == " ++
                                     showStack s') $ return ()
                              trace (show b) $ return ()
                              setStack [sv]
                              addConstraint ([sv], substAll b s')
                              mapM_ substBinding b
  --where unifyS a b = trace (showStack a ++ " == " ++ showStack b) $ unify a b
checkUnify :: Peg () -> Peg ()
checkUnify m = do
  PegState s _ _ _ _ _ p <- get
  if topIs (isList ||. (== W "]")) s
    then m
    else case maybeAny (\x -> ((,) x) <$> unify (trim isStackVar s) (x ++ [S "_rest"]) []) p of
           Nothing -> --trace (show $ map showStack p) $
                      pushAnc s >> m >> popAnc >> return ()
           Just (s', b) -> do sv <- newSVar
                              trace (showStack s ++ " == " ++
                                     showStack s') $ return ()
                              trace (show b) $ return ()
                              setStack [sv]
                              addConstraint ([sv], substAll b s')
                              mapM_ substBinding b
  --where unifyS a b = trace (showStack a ++ " == " ++ showStack b) $ unify a b
checkUnify m = do
  PegState s _ _ _ _ _ p <- get
  if topIs (isList ||. (== W "]")) s
    then m
    else case maybeAny (\x -> ((,) x) <$> unify (trim isStackVar s) (x ++ [S "_rest"]) []) p of
           Nothing -> --trace (show $ map showStack p) $
                      pushAnc s >> m >> popAnc >> return ()
           Just (s', b) -> do sv <- newSVar
                              trace (showStack s ++ " == " ++
                                     showStack s') $ return ()
                              trace (show b) $ return ()
                              setStack [sv]
                              addConstraint ([sv], substAll b s')
                              mapM_ substBinding b
  --where unifyS a b = trace (showStack a ++ " == " ++ showStack b) $ unify a b