module Unison.Names.ResolutionResult
  ( ResolutionError (..),
    ResolutionFailure (..),
    ResolutionResult,
    getAnnotation,
  )
where

import Unison.Name (Name)
import Unison.Names (Names)
import Unison.Prelude
import Unison.Reference (TypeReference)
import Unison.Referent (Referent)
import Unison.HashQualified (HashQualified)

data ResolutionError ref
  = NotFound
  | -- Contains:
    --
    --   1. The namespace names
    --   2. The refs among those that we could be referring to
    --   3. The local names that we could be referring to
    --
    -- The size of set (2.) + the size of set (3.) is at least 2 (otherwise there wouldn't be any ambiguity).
    Ambiguous Names (Set ref) (Set Name)
  deriving (ResolutionError ref -> ResolutionError ref -> Bool
(ResolutionError ref -> ResolutionError ref -> Bool)
-> (ResolutionError ref -> ResolutionError ref -> Bool)
-> Eq (ResolutionError ref)
forall ref.
Eq ref =>
ResolutionError ref -> ResolutionError ref -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall ref.
Eq ref =>
ResolutionError ref -> ResolutionError ref -> Bool
== :: ResolutionError ref -> ResolutionError ref -> Bool
$c/= :: forall ref.
Eq ref =>
ResolutionError ref -> ResolutionError ref -> Bool
/= :: ResolutionError ref -> ResolutionError ref -> Bool
Eq, Eq (ResolutionError ref)
Eq (ResolutionError ref) =>
(ResolutionError ref -> ResolutionError ref -> Ordering)
-> (ResolutionError ref -> ResolutionError ref -> Bool)
-> (ResolutionError ref -> ResolutionError ref -> Bool)
-> (ResolutionError ref -> ResolutionError ref -> Bool)
-> (ResolutionError ref -> ResolutionError ref -> Bool)
-> (ResolutionError ref
    -> ResolutionError ref -> ResolutionError ref)
-> (ResolutionError ref
    -> ResolutionError ref -> ResolutionError ref)
-> Ord (ResolutionError ref)
ResolutionError ref -> ResolutionError ref -> Bool
ResolutionError ref -> ResolutionError ref -> Ordering
ResolutionError ref -> ResolutionError ref -> ResolutionError ref
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall ref. Ord ref => Eq (ResolutionError ref)
forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> Bool
forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> Ordering
forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> ResolutionError ref
$ccompare :: forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> Ordering
compare :: ResolutionError ref -> ResolutionError ref -> Ordering
$c< :: forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> Bool
< :: ResolutionError ref -> ResolutionError ref -> Bool
$c<= :: forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> Bool
<= :: ResolutionError ref -> ResolutionError ref -> Bool
$c> :: forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> Bool
> :: ResolutionError ref -> ResolutionError ref -> Bool
$c>= :: forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> Bool
>= :: ResolutionError ref -> ResolutionError ref -> Bool
$cmax :: forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> ResolutionError ref
max :: ResolutionError ref -> ResolutionError ref -> ResolutionError ref
$cmin :: forall ref.
Ord ref =>
ResolutionError ref -> ResolutionError ref -> ResolutionError ref
min :: ResolutionError ref -> ResolutionError ref -> ResolutionError ref
Ord, Int -> ResolutionError ref -> ShowS
[ResolutionError ref] -> ShowS
ResolutionError ref -> String
(Int -> ResolutionError ref -> ShowS)
-> (ResolutionError ref -> String)
-> ([ResolutionError ref] -> ShowS)
-> Show (ResolutionError ref)
forall ref. Show ref => Int -> ResolutionError ref -> ShowS
forall ref. Show ref => [ResolutionError ref] -> ShowS
forall ref. Show ref => ResolutionError ref -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall ref. Show ref => Int -> ResolutionError ref -> ShowS
showsPrec :: Int -> ResolutionError ref -> ShowS
$cshow :: forall ref. Show ref => ResolutionError ref -> String
show :: ResolutionError ref -> String
$cshowList :: forall ref. Show ref => [ResolutionError ref] -> ShowS
showList :: [ResolutionError ref] -> ShowS
Show)

-- | ResolutionFailure represents the failure to resolve a given name.
data ResolutionFailure annotation
  = TypeResolutionFailure (HashQualified Name) annotation (ResolutionError TypeReference)
  | TermResolutionFailure (HashQualified Name) annotation (ResolutionError Referent)
  deriving (ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
(ResolutionFailure annotation
 -> ResolutionFailure annotation -> Bool)
-> (ResolutionFailure annotation
    -> ResolutionFailure annotation -> Bool)
-> Eq (ResolutionFailure annotation)
forall annotation.
Eq annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall annotation.
Eq annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
== :: ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
$c/= :: forall annotation.
Eq annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
/= :: ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
Eq, Eq (ResolutionFailure annotation)
Eq (ResolutionFailure annotation) =>
(ResolutionFailure annotation
 -> ResolutionFailure annotation -> Ordering)
-> (ResolutionFailure annotation
    -> ResolutionFailure annotation -> Bool)
-> (ResolutionFailure annotation
    -> ResolutionFailure annotation -> Bool)
-> (ResolutionFailure annotation
    -> ResolutionFailure annotation -> Bool)
-> (ResolutionFailure annotation
    -> ResolutionFailure annotation -> Bool)
-> (ResolutionFailure annotation
    -> ResolutionFailure annotation -> ResolutionFailure annotation)
-> (ResolutionFailure annotation
    -> ResolutionFailure annotation -> ResolutionFailure annotation)
-> Ord (ResolutionFailure annotation)
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
ResolutionFailure annotation
-> ResolutionFailure annotation -> Ordering
ResolutionFailure annotation
-> ResolutionFailure annotation -> ResolutionFailure annotation
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall annotation.
Ord annotation =>
Eq (ResolutionFailure annotation)
forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Ordering
forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> ResolutionFailure annotation
$ccompare :: forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Ordering
compare :: ResolutionFailure annotation
-> ResolutionFailure annotation -> Ordering
$c< :: forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
< :: ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
$c<= :: forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
<= :: ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
$c> :: forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
> :: ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
$c>= :: forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
>= :: ResolutionFailure annotation
-> ResolutionFailure annotation -> Bool
$cmax :: forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> ResolutionFailure annotation
max :: ResolutionFailure annotation
-> ResolutionFailure annotation -> ResolutionFailure annotation
$cmin :: forall annotation.
Ord annotation =>
ResolutionFailure annotation
-> ResolutionFailure annotation -> ResolutionFailure annotation
min :: ResolutionFailure annotation
-> ResolutionFailure annotation -> ResolutionFailure annotation
Ord, Int -> ResolutionFailure annotation -> ShowS
[ResolutionFailure annotation] -> ShowS
ResolutionFailure annotation -> String
(Int -> ResolutionFailure annotation -> ShowS)
-> (ResolutionFailure annotation -> String)
-> ([ResolutionFailure annotation] -> ShowS)
-> Show (ResolutionFailure annotation)
forall annotation.
Show annotation =>
Int -> ResolutionFailure annotation -> ShowS
forall annotation.
Show annotation =>
[ResolutionFailure annotation] -> ShowS
forall annotation.
Show annotation =>
ResolutionFailure annotation -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall annotation.
Show annotation =>
Int -> ResolutionFailure annotation -> ShowS
showsPrec :: Int -> ResolutionFailure annotation -> ShowS
$cshow :: forall annotation.
Show annotation =>
ResolutionFailure annotation -> String
show :: ResolutionFailure annotation -> String
$cshowList :: forall annotation.
Show annotation =>
[ResolutionFailure annotation] -> ShowS
showList :: [ResolutionFailure annotation] -> ShowS
Show, (forall a b.
 (a -> b) -> ResolutionFailure a -> ResolutionFailure b)
-> (forall a b. a -> ResolutionFailure b -> ResolutionFailure a)
-> Functor ResolutionFailure
forall a b. a -> ResolutionFailure b -> ResolutionFailure a
forall a b. (a -> b) -> ResolutionFailure a -> ResolutionFailure b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> ResolutionFailure a -> ResolutionFailure b
fmap :: forall a b. (a -> b) -> ResolutionFailure a -> ResolutionFailure b
$c<$ :: forall a b. a -> ResolutionFailure b -> ResolutionFailure a
<$ :: forall a b. a -> ResolutionFailure b -> ResolutionFailure a
Functor, (forall m. Monoid m => ResolutionFailure m -> m)
-> (forall m a. Monoid m => (a -> m) -> ResolutionFailure a -> m)
-> (forall m a. Monoid m => (a -> m) -> ResolutionFailure a -> m)
-> (forall a b. (a -> b -> b) -> b -> ResolutionFailure a -> b)
-> (forall a b. (a -> b -> b) -> b -> ResolutionFailure a -> b)
-> (forall b a. (b -> a -> b) -> b -> ResolutionFailure a -> b)
-> (forall b a. (b -> a -> b) -> b -> ResolutionFailure a -> b)
-> (forall a. (a -> a -> a) -> ResolutionFailure a -> a)
-> (forall a. (a -> a -> a) -> ResolutionFailure a -> a)
-> (forall a. ResolutionFailure a -> [a])
-> (forall a. ResolutionFailure a -> Bool)
-> (forall a. ResolutionFailure a -> Int)
-> (forall a. Eq a => a -> ResolutionFailure a -> Bool)
-> (forall a. Ord a => ResolutionFailure a -> a)
-> (forall a. Ord a => ResolutionFailure a -> a)
-> (forall a. Num a => ResolutionFailure a -> a)
-> (forall a. Num a => ResolutionFailure a -> a)
-> Foldable ResolutionFailure
forall a. Eq a => a -> ResolutionFailure a -> Bool
forall a. Num a => ResolutionFailure a -> a
forall a. Ord a => ResolutionFailure a -> a
forall m. Monoid m => ResolutionFailure m -> m
forall a. ResolutionFailure a -> Bool
forall a. ResolutionFailure a -> Int
forall a. ResolutionFailure a -> [a]
forall a. (a -> a -> a) -> ResolutionFailure a -> a
forall m a. Monoid m => (a -> m) -> ResolutionFailure a -> m
forall b a. (b -> a -> b) -> b -> ResolutionFailure a -> b
forall a b. (a -> b -> b) -> b -> ResolutionFailure a -> b
forall (t :: * -> *).
(forall m. Monoid m => t m -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall m a. Monoid m => (a -> m) -> t a -> m)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall a b. (a -> b -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall b a. (b -> a -> b) -> b -> t a -> b)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. (a -> a -> a) -> t a -> a)
-> (forall a. t a -> [a])
-> (forall a. t a -> Bool)
-> (forall a. t a -> Int)
-> (forall a. Eq a => a -> t a -> Bool)
-> (forall a. Ord a => t a -> a)
-> (forall a. Ord a => t a -> a)
-> (forall a. Num a => t a -> a)
-> (forall a. Num a => t a -> a)
-> Foldable t
$cfold :: forall m. Monoid m => ResolutionFailure m -> m
fold :: forall m. Monoid m => ResolutionFailure m -> m
$cfoldMap :: forall m a. Monoid m => (a -> m) -> ResolutionFailure a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> ResolutionFailure a -> m
$cfoldMap' :: forall m a. Monoid m => (a -> m) -> ResolutionFailure a -> m
foldMap' :: forall m a. Monoid m => (a -> m) -> ResolutionFailure a -> m
$cfoldr :: forall a b. (a -> b -> b) -> b -> ResolutionFailure a -> b
foldr :: forall a b. (a -> b -> b) -> b -> ResolutionFailure a -> b
$cfoldr' :: forall a b. (a -> b -> b) -> b -> ResolutionFailure a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> ResolutionFailure a -> b
$cfoldl :: forall b a. (b -> a -> b) -> b -> ResolutionFailure a -> b
foldl :: forall b a. (b -> a -> b) -> b -> ResolutionFailure a -> b
$cfoldl' :: forall b a. (b -> a -> b) -> b -> ResolutionFailure a -> b
foldl' :: forall b a. (b -> a -> b) -> b -> ResolutionFailure a -> b
$cfoldr1 :: forall a. (a -> a -> a) -> ResolutionFailure a -> a
foldr1 :: forall a. (a -> a -> a) -> ResolutionFailure a -> a
$cfoldl1 :: forall a. (a -> a -> a) -> ResolutionFailure a -> a
foldl1 :: forall a. (a -> a -> a) -> ResolutionFailure a -> a
$ctoList :: forall a. ResolutionFailure a -> [a]
toList :: forall a. ResolutionFailure a -> [a]
$cnull :: forall a. ResolutionFailure a -> Bool
null :: forall a. ResolutionFailure a -> Bool
$clength :: forall a. ResolutionFailure a -> Int
length :: forall a. ResolutionFailure a -> Int
$celem :: forall a. Eq a => a -> ResolutionFailure a -> Bool
elem :: forall a. Eq a => a -> ResolutionFailure a -> Bool
$cmaximum :: forall a. Ord a => ResolutionFailure a -> a
maximum :: forall a. Ord a => ResolutionFailure a -> a
$cminimum :: forall a. Ord a => ResolutionFailure a -> a
minimum :: forall a. Ord a => ResolutionFailure a -> a
$csum :: forall a. Num a => ResolutionFailure a -> a
sum :: forall a. Num a => ResolutionFailure a -> a
$cproduct :: forall a. Num a => ResolutionFailure a -> a
product :: forall a. Num a => ResolutionFailure a -> a
Foldable, Functor ResolutionFailure
Foldable ResolutionFailure
(Functor ResolutionFailure, Foldable ResolutionFailure) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> ResolutionFailure a -> f (ResolutionFailure b))
-> (forall (f :: * -> *) a.
    Applicative f =>
    ResolutionFailure (f a) -> f (ResolutionFailure a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> ResolutionFailure a -> m (ResolutionFailure b))
-> (forall (m :: * -> *) a.
    Monad m =>
    ResolutionFailure (m a) -> m (ResolutionFailure a))
-> Traversable ResolutionFailure
forall (t :: * -> *).
(Functor t, Foldable t) =>
(forall (f :: * -> *) a b.
 Applicative f =>
 (a -> f b) -> t a -> f (t b))
-> (forall (f :: * -> *) a. Applicative f => t (f a) -> f (t a))
-> (forall (m :: * -> *) a b.
    Monad m =>
    (a -> m b) -> t a -> m (t b))
-> (forall (m :: * -> *) a. Monad m => t (m a) -> m (t a))
-> Traversable t
forall (m :: * -> *) a.
Monad m =>
ResolutionFailure (m a) -> m (ResolutionFailure a)
forall (f :: * -> *) a.
Applicative f =>
ResolutionFailure (f a) -> f (ResolutionFailure a)
forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ResolutionFailure a -> m (ResolutionFailure b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ResolutionFailure a -> f (ResolutionFailure b)
$ctraverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ResolutionFailure a -> f (ResolutionFailure b)
traverse :: forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> ResolutionFailure a -> f (ResolutionFailure b)
$csequenceA :: forall (f :: * -> *) a.
Applicative f =>
ResolutionFailure (f a) -> f (ResolutionFailure a)
sequenceA :: forall (f :: * -> *) a.
Applicative f =>
ResolutionFailure (f a) -> f (ResolutionFailure a)
$cmapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ResolutionFailure a -> m (ResolutionFailure b)
mapM :: forall (m :: * -> *) a b.
Monad m =>
(a -> m b) -> ResolutionFailure a -> m (ResolutionFailure b)
$csequence :: forall (m :: * -> *) a.
Monad m =>
ResolutionFailure (m a) -> m (ResolutionFailure a)
sequence :: forall (m :: * -> *) a.
Monad m =>
ResolutionFailure (m a) -> m (ResolutionFailure a)
Traversable)

getAnnotation :: ResolutionFailure a -> a
getAnnotation :: forall a. ResolutionFailure a -> a
getAnnotation = \case
  TypeResolutionFailure HashQualified Name
_ a
a ResolutionError TypeReference
_ -> a
a
  TermResolutionFailure HashQualified Name
_ a
a ResolutionError Referent
_ -> a
a

type ResolutionResult a r = Either (Seq (ResolutionFailure a)) r