--------------------------------------------------------------------------------


-- Copyright © 2015 Nikita Volkov

-- Copyright © 2018 Remy Goldschmidt

-- Copyright © 2020 chessai

--

-- Permission is hereby granted, free of charge, to any person

-- obtaining a copy of this software and associated documentation

-- files (the "Software"), to deal in the Software without

-- restriction, including without limitation the rights to use,

-- copy, modify, merge, publish, distribute, sublicense, and/or sell

-- copies of the Software, and to permit persons to whom the

-- Software is furnished to do so, subject to the following

-- conditions:

--

-- The above copyright notice and this permission notice shall be

-- included in all copies or substantial portions of the Software.

--

-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

-- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES

-- OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND

-- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT

-- HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,

-- WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING

-- FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

-- OTHER DEALINGS IN THE SOFTWARE.


--------------------------------------------------------------------------------


{-# LANGUAGE CPP                        #-}
{-# LANGUAGE DeriveFoldable             #-}
{-# LANGUAGE DerivingStrategies         #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PolyKinds                  #-}
{-# LANGUAGE RoleAnnotations            #-}
{-# LANGUAGE TemplateHaskell            #-}

--------------------------------------------------------------------------------


-- | This module exports the 'Refined' type with its

--   constructor. This is very risky! In particular, the 'Data.Coerce.Coercible'

--   instances will be visible throughout the importing module.

--   It is usually better to build the necessary coercions locally

--   using the utilities in "Refined.Unsafe", but in some cases

--   it may be more convenient to write a separate module that

--   imports this one and exports some large coercion.

module Refined.Unsafe.Type
  ( Refined(Refined)
  ) where

import           Control.DeepSeq              (NFData)
import           Data.Hashable (Hashable)
import qualified Language.Haskell.TH.Syntax   as TH

-- | A refinement type, which wraps a value of type @x@.

--

--   @since 0.1.0.0

newtype Refined (p :: k) x
  = Refined x -- ^ @since 0.1.0.0

  deriving newtype
    ( Refined p x -> Refined p x -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (p :: k) x. Eq x => Refined p x -> Refined p x -> Bool
/= :: Refined p x -> Refined p x -> Bool
$c/= :: forall k (p :: k) x. Eq x => Refined p x -> Refined p x -> Bool
== :: Refined p x -> Refined p x -> Bool
$c== :: forall k (p :: k) x. Eq x => Refined p x -> Refined p x -> Bool
Eq -- ^ @since 0.1.0.0

    , Refined p x -> Refined p x -> Bool
Refined p x -> Refined p x -> Ordering
Refined p x -> Refined p x -> Refined p x
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 {k} {p :: k} {x}. Ord x => Eq (Refined p x)
forall k (p :: k) x. Ord x => Refined p x -> Refined p x -> Bool
forall k (p :: k) x.
Ord x =>
Refined p x -> Refined p x -> Ordering
forall k (p :: k) x.
Ord x =>
Refined p x -> Refined p x -> Refined p x
min :: Refined p x -> Refined p x -> Refined p x
$cmin :: forall k (p :: k) x.
Ord x =>
Refined p x -> Refined p x -> Refined p x
max :: Refined p x -> Refined p x -> Refined p x
$cmax :: forall k (p :: k) x.
Ord x =>
Refined p x -> Refined p x -> Refined p x
>= :: Refined p x -> Refined p x -> Bool
$c>= :: forall k (p :: k) x. Ord x => Refined p x -> Refined p x -> Bool
> :: Refined p x -> Refined p x -> Bool
$c> :: forall k (p :: k) x. Ord x => Refined p x -> Refined p x -> Bool
<= :: Refined p x -> Refined p x -> Bool
$c<= :: forall k (p :: k) x. Ord x => Refined p x -> Refined p x -> Bool
< :: Refined p x -> Refined p x -> Bool
$c< :: forall k (p :: k) x. Ord x => Refined p x -> Refined p x -> Bool
compare :: Refined p x -> Refined p x -> Ordering
$ccompare :: forall k (p :: k) x.
Ord x =>
Refined p x -> Refined p x -> Ordering
Ord -- ^ @since 0.1.0.0

    , Int -> Refined p x -> Int
Refined p x -> Int
forall a. Eq a -> (Int -> a -> Int) -> (a -> Int) -> Hashable a
forall {k} {p :: k} {x}. Hashable x => Eq (Refined p x)
forall k (p :: k) x. Hashable x => Int -> Refined p x -> Int
forall k (p :: k) x. Hashable x => Refined p x -> Int
hash :: Refined p x -> Int
$chash :: forall k (p :: k) x. Hashable x => Refined p x -> Int
hashWithSalt :: Int -> Refined p x -> Int
$chashWithSalt :: forall k (p :: k) x. Hashable x => Int -> Refined p x -> Int
Hashable -- ^ @since 0.6.3

    , Refined p x -> ()
forall a. (a -> ()) -> NFData a
forall k (p :: k) x. NFData x => Refined p x -> ()
rnf :: Refined p x -> ()
$crnf :: forall k (p :: k) x. NFData x => Refined p x -> ()
NFData -- ^ @since 0.5

    )
  deriving stock
    ( Int -> Refined p x -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (p :: k) x. Show x => Int -> Refined p x -> ShowS
forall k (p :: k) x. Show x => [Refined p x] -> ShowS
forall k (p :: k) x. Show x => Refined p x -> String
showList :: [Refined p x] -> ShowS
$cshowList :: forall k (p :: k) x. Show x => [Refined p x] -> ShowS
show :: Refined p x -> String
$cshow :: forall k (p :: k) x. Show x => Refined p x -> String
showsPrec :: Int -> Refined p x -> ShowS
$cshowsPrec :: forall k (p :: k) x. Show x => Int -> Refined p x -> ShowS
Show -- ^ @since 0.1.0.0

    )
  deriving stock
    ( forall a. Refined p a -> Bool
forall k (p :: k) a. Eq a => a -> Refined p a -> Bool
forall k (p :: k) a. Num a => Refined p a -> a
forall k (p :: k) a. Ord a => Refined p a -> a
forall k (p :: k) m. Monoid m => Refined p m -> m
forall k (p :: k) a. Refined p a -> Bool
forall k (p :: k) a. Refined p a -> Int
forall k (p :: k) a. Refined p a -> [a]
forall k (p :: k) a. (a -> a -> a) -> Refined p a -> a
forall k (p :: k) m a. Monoid m => (a -> m) -> Refined p a -> m
forall k (p :: k) b a. (b -> a -> b) -> b -> Refined p a -> b
forall k (p :: k) a b. (a -> b -> b) -> b -> Refined p a -> b
forall m a. Monoid m => (a -> m) -> Refined p a -> m
forall a b. (a -> b -> b) -> b -> Refined p 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
product :: forall a. Num a => Refined p a -> a
$cproduct :: forall k (p :: k) a. Num a => Refined p a -> a
sum :: forall a. Num a => Refined p a -> a
$csum :: forall k (p :: k) a. Num a => Refined p a -> a
minimum :: forall a. Ord a => Refined p a -> a
$cminimum :: forall k (p :: k) a. Ord a => Refined p a -> a
maximum :: forall a. Ord a => Refined p a -> a
$cmaximum :: forall k (p :: k) a. Ord a => Refined p a -> a
elem :: forall a. Eq a => a -> Refined p a -> Bool
$celem :: forall k (p :: k) a. Eq a => a -> Refined p a -> Bool
length :: forall a. Refined p a -> Int
$clength :: forall k (p :: k) a. Refined p a -> Int
null :: forall a. Refined p a -> Bool
$cnull :: forall k (p :: k) a. Refined p a -> Bool
toList :: forall a. Refined p a -> [a]
$ctoList :: forall k (p :: k) a. Refined p a -> [a]
foldl1 :: forall a. (a -> a -> a) -> Refined p a -> a
$cfoldl1 :: forall k (p :: k) a. (a -> a -> a) -> Refined p a -> a
foldr1 :: forall a. (a -> a -> a) -> Refined p a -> a
$cfoldr1 :: forall k (p :: k) a. (a -> a -> a) -> Refined p a -> a
foldl' :: forall b a. (b -> a -> b) -> b -> Refined p a -> b
$cfoldl' :: forall k (p :: k) b a. (b -> a -> b) -> b -> Refined p a -> b
foldl :: forall b a. (b -> a -> b) -> b -> Refined p a -> b
$cfoldl :: forall k (p :: k) b a. (b -> a -> b) -> b -> Refined p a -> b
foldr' :: forall a b. (a -> b -> b) -> b -> Refined p a -> b
$cfoldr' :: forall k (p :: k) a b. (a -> b -> b) -> b -> Refined p a -> b
foldr :: forall a b. (a -> b -> b) -> b -> Refined p a -> b
$cfoldr :: forall k (p :: k) a b. (a -> b -> b) -> b -> Refined p a -> b
foldMap' :: forall m a. Monoid m => (a -> m) -> Refined p a -> m
$cfoldMap' :: forall k (p :: k) m a. Monoid m => (a -> m) -> Refined p a -> m
foldMap :: forall m a. Monoid m => (a -> m) -> Refined p a -> m
$cfoldMap :: forall k (p :: k) m a. Monoid m => (a -> m) -> Refined p a -> m
fold :: forall m. Monoid m => Refined p m -> m
$cfold :: forall k (p :: k) m. Monoid m => Refined p m -> m
Foldable -- ^ @since 0.2

    )

-- | @since 0.3.0.0

type role Refined nominal nominal

-- | @since 0.1.0.0

instance (TH.Lift x) => TH.Lift (Refined p x) where
  lift :: forall (m :: * -> *). Quote m => Refined p x -> m Exp
lift (Refined x
a) = [|Refined a|]
#if MIN_VERSION_template_haskell(2,16,0)
  liftTyped :: forall (m :: * -> *).
Quote m =>
Refined p x -> Code m (Refined p x)
liftTyped (Refined x
a) = [||Refined a||]
#endif