initScript :: MonadError ASTError m => AST -> m InitScript
initScript ast = InitScript <$> (fromMaybe False <$> maybeOne ManyOnUpdates onUpdates)
                            <*> allActions
  where
    instructions = map instruction ast

    onUpdates = mapMaybe onUpdate' instructions
    onUpdate' (OnUpdate x) = Just x
    onUpdate' _            = Nothing

    allActions = pure $ mapMaybe action' instructions
    action' (Action x) = Just x
    action' _          = Nothing

    maybeOne _       [x] = return $ Just x
    maybeOne _       []  = return Nothing
    maybeOne manyErr _   = throwError manyErr
initScript :: MonadError ASTError m => AST -> m InitScript
initScript ast = InitScript <$> (fromMaybe False <$> maybeOne ManyOnUpdates onUpdates)
                            <*> allActions
  where
    instructions = map instruction ast

    onUpdates = mapMaybe onUpdate' instructions
    onUpdate' (OnUpdate x) = Just x
    onUpdate' _            = Nothing

    allActions = pure $ mapMaybe action' instructions
    action' (Action x) = Just x
    action' _          = Nothing

    maybeOne _       [x] = return $ Just x
    maybeOne _       []  = return Nothing
    maybeOne manyErr _   = throwError manyErr
initScript ast = InitScript <$> (fromMaybe False <$> maybeOne ManyOnUpdates onUpdates)
                            <*> allActions
  where
    instructions = map instruction ast

    onUpdates = mapMaybe onUpdate' instructions
    onUpdate' (OnUpdate x) = Just x
    onUpdate' _            = Nothing

    allActions = pure $ mapMaybe action' instructions
    action' (Action x) = Just x
    action' _          = Nothing

    maybeOne _       [x] = return $ Just x
    maybeOne _       []  = return Nothing
    maybeOne manyErr _   = throwError manyErr