pvar
    :: R.Region
    -> Env.Environment
    -> String
    -> Int
    -> Result.ResultErr Var.Canonical
pvar region env var actualArgs =
  case Set.toList `fmap` Map.lookup var (Env._patterns env) of
    Just [value] ->
        foundArgCheck value

    Just values ->
        preferLocals' region env fst "pattern" values var
          `Result.andThen` foundArgCheck

    Nothing ->
        notFound region "pattern" (Map.keys (Env._patterns env)) var
  where
    foundArgCheck (name, expectedArgs) =
        if actualArgs == expectedArgs
          then Result.var name
          else Result.err (A.A region (Error.argMismatch name expectedArgs actualArgs))


-- FOUND
pvar
    :: R.Region
    -> Env.Environment
    -> String
    -> Int
    -> Result.ResultErr Var.Canonical
pvar region env var actualArgs =
  case Set.toList `fmap` Map.lookup var (Env._patterns env) of
    Just [value] ->
        foundArgCheck value

    Just values ->
        preferLocals' region env fst "pattern" values var
          `Result.andThen` foundArgCheck

    Nothing ->
        notFound region "pattern" (Map.keys (Env._patterns env)) var
  where
    foundArgCheck (name, expectedArgs) =
        if actualArgs == expectedArgs
          then Result.var name
          else Result.err (A.A region (Error.argMismatch name expectedArgs actualArgs))


-- FOUND
pvar region env var actualArgs =
  case Set.toList `fmap` Map.lookup var (Env._patterns env) of
    Just [value] ->
        foundArgCheck value

    Just values ->
        preferLocals' region env fst "pattern" values var
          `Result.andThen` foundArgCheck

    Nothing ->
        notFound region "pattern" (Map.keys (Env._patterns env)) var
  where
    foundArgCheck (name, expectedArgs) =
        if actualArgs == expectedArgs
          then Result.var name
          else Result.err (A.A region (Error.argMismatch name expectedArgs actualArgs))


-- FOUND