--import Debug.Trace

-- Checking (top-level)
check :: (?globals :: Globals)
  => AST () ()
  -> IO (Either (NonEmpty CheckerError) (AST () Type))
check ast@(AST dataDecls defs imports hidden name) =
  evalChecker (initState { allHiddenNames = hidden }) $ (do
      _    <- checkNameClashes ast
      _    <- runAll checkTyCon (Primitives.dataTypes ++ dataDecls)
      _    <- runAll checkDataCons (Primitives.dataTypes ++ dataDecls)
      defs <- runAll kindCheckDef defs
      let defCtxt = map (\(Def _ name _ tys) -> (name, tys)) defs
      defs <- runAll (checkDef defCtxt) defs
      pure $ AST dataDecls defs imports hidden name)
check :: (?globals :: Globals)
  => AST () ()
  -> IO (Either (NonEmpty CheckerError) (AST () Type))
check ast@(AST dataDecls defs imports hidden name) =
  evalChecker (initState { allHiddenNames = hidden }) $ (do
      _    <- checkNameClashes ast
      _    <- runAll checkTyCon (Primitives.dataTypes ++ dataDecls)
      _    <- runAll checkDataCons (Primitives.dataTypes ++ dataDecls)
      defs <- runAll kindCheckDef defs
      let defCtxt = map (\(Def _ name _ tys) -> (name, tys)) defs
      defs <- runAll (checkDef defCtxt) defs
      pure $ AST dataDecls defs imports hidden name)
check ast@(AST dataDecls defs imports hidden name) =
  evalChecker (initState { allHiddenNames = hidden }) $ (do
      _    <- checkNameClashes ast
      _    <- runAll checkTyCon (Primitives.dataTypes ++ dataDecls)
      _    <- runAll checkDataCons (Primitives.dataTypes ++ dataDecls)
      defs <- runAll kindCheckDef defs
      let defCtxt = map (\(Def _ name _ tys) -> (name, tys)) defs
      defs <- runAll (checkDef defCtxt) defs
      pure $ AST dataDecls defs imports hidden name)