{-# OPTIONS_GHC -Wno-orphans #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LinearTypes #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}

-- | This module exports instances of Consumable, Dupable and Movable
--
-- We export instances in this module to avoid a circular dependence
-- and keep things clean. Movable depends on the defintion of Ur yet
-- many instances of Movable which we might have put in the module with
-- Movable depend on Ur. So, we just put the instances of Movable and the
-- other classes (for cleanness) in this module to avoid this dependence.
module Data.Unrestricted.Internal.Instances where

import Data.Unrestricted.Internal.Consumable
import Data.Unrestricted.Internal.Dupable
import Data.Unrestricted.Internal.Movable
import Data.Unrestricted.Internal.Ur
import qualified Data.Functor.Linear.Internal.Functor as Data
import qualified Data.Functor.Linear.Internal.Applicative as Data
import GHC.Types hiding (Any)
import Data.Monoid.Linear
import Data.List.NonEmpty
import qualified Prelude
import qualified Unsafe.Linear as Unsafe
import Data.V.Linear ()

instance Consumable () where
  consume :: () %1 -> ()
consume () = ()

instance Dupable () where
  dupV :: forall (n :: Nat). KnownNat n => () %1 -> V n ()
dupV () = () -> V n ()
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure ()

instance Movable () where
  move :: () %1 -> Ur ()
move () = () -> Ur ()
forall a. a -> Ur a
Ur ()

instance Consumable Bool where
  consume :: Bool %1 -> ()
consume Bool
True = ()
  consume Bool
False = ()

instance Dupable Bool where
  dupV :: forall (n :: Nat). KnownNat n => Bool %1 -> V n Bool
dupV Bool
True = Bool -> V n Bool
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure Bool
True
  dupV Bool
False = Bool -> V n Bool
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure Bool
False

instance Movable Bool where
  move :: Bool %1 -> Ur Bool
move Bool
True = Bool -> Ur Bool
forall a. a -> Ur a
Ur Bool
True
  move Bool
False = Bool -> Ur Bool
forall a. a -> Ur a
Ur Bool
False

instance Consumable Int where
  -- /!\ 'Int#' is an unboxed unlifted data-types, therefore it cannot have any
  -- linear values hidden in a closure anywhere. Therefore it is safe to call
  -- non-linear functions linearly on this type: there is no difference between
  -- copying an 'Int#' and using it several times. /!\
  consume :: Int %1 -> ()
consume (I# Int#
i) = (Int# -> ()) %1 -> Int# %1 -> ()
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Int#
_ -> ()) Int#
i

instance Dupable Int where
  -- /!\ 'Int#' is an unboxed unlifted data-types, therefore it cannot have any
  -- linear values hidden in a closure anywhere. Therefore it is safe to call
  -- non-linear functions linearly on this type: there is no difference between
  -- copying an 'Int#' and using it several times. /!\
  dupV :: forall (n :: Nat). KnownNat n => Int %1 -> V n Int
dupV (I# Int#
i) = (Int# -> V n Int) %1 -> Int# %1 -> V n Int
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Int#
j -> Int -> V n Int
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure (Int# -> Int
I# Int#
j)) Int#
i

instance Movable Int where
  -- /!\ 'Int#' is an unboxed unlifted data-types, therefore it cannot have any
  -- linear values hidden in a closure anywhere. Therefore it is safe to call
  -- non-linear functions linearly on this type: there is no difference between
  -- copying an 'Int#' and using it several times. /!\
  move :: Int %1 -> Ur Int
move (I# Int#
i) = (Int# -> Ur Int) %1 -> Int# %1 -> Ur Int
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Int#
j -> Int -> Ur Int
forall a. a -> Ur a
Ur (Int# -> Int
I# Int#
j)) Int#
i

instance Consumable Double where
  -- /!\ 'Double#' is an unboxed unlifted data-types, therefore it cannot have any
  -- linear values hidden in a closure anywhere. Therefore it is safe to call
  -- non-linear functions linearly on this type: there is no difference between
  -- copying an 'Double#' and using it several times. /!\
  consume :: Double %1 -> ()
consume (D# Double#
i) = (Double# -> ()) %1 -> Double# %1 -> ()
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Double#
_ -> ()) Double#
i

instance Dupable Double where
  -- /!\ 'Double#' is an unboxed unlifted data-types, therefore it cannot have any
  -- linear values hidden in a closure anywhere. Therefore it is safe to call
  -- non-linear functions linearly on this type: there is no difference between
  -- copying an 'Double#' and using it several times. /!\
  dupV :: forall (n :: Nat). KnownNat n => Double %1 -> V n Double
dupV (D# Double#
i) = (Double# -> V n Double) %1 -> Double# %1 -> V n Double
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Double#
j -> Double -> V n Double
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure (Double# -> Double
D# Double#
j)) Double#
i

instance Movable Double where
  -- /!\ 'Double#' is an unboxed unlifted data-types, therefore it cannot have any
  -- linear values hidden in a closure anywhere. Therefore it is safe to call
  -- non-linear functions linearly on this type: there is no difference between
  -- copying an 'Double#' and using it several times. /!\
  move :: Double %1 -> Ur Double
move (D# Double#
i) = (Double# -> Ur Double) %1 -> Double# %1 -> Ur Double
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Double#
j -> Double -> Ur Double
forall a. a -> Ur a
Ur (Double# -> Double
D# Double#
j)) Double#
i

instance Consumable Char where
  consume :: Char %1 -> ()
consume (C# Char#
c) = (Char# -> ()) %1 -> Char# %1 -> ()
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Char#
_ -> ()) Char#
c

instance Dupable Char where
  dupV :: forall (n :: Nat). KnownNat n => Char %1 -> V n Char
dupV (C# Char#
c) = (Char# -> V n Char) %1 -> Char# %1 -> V n Char
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Char#
x -> Char -> V n Char
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure (Char# -> Char
C# Char#
x)) Char#
c

instance Movable Char where
  move :: Char %1 -> Ur Char
move (C# Char#
c) = (Char# -> Ur Char) %1 -> Char# %1 -> Ur Char
forall a b (p :: Multiplicity). (a %p -> b) %1 -> a %1 -> b
Unsafe.toLinear (\Char#
x -> Char -> Ur Char
forall a. a -> Ur a
Ur (Char# -> Char
C# Char#
x)) Char#
c

instance Consumable Ordering where
  consume :: Ordering %1 -> ()
consume Ordering
LT = ()
  consume Ordering
GT = ()
  consume Ordering
EQ = ()

instance Dupable Ordering where
  dup2 :: Ordering %1 -> (Ordering, Ordering)
dup2 Ordering
LT = (Ordering
LT, Ordering
LT)
  dup2 Ordering
GT = (Ordering
GT, Ordering
GT)
  dup2 Ordering
EQ = (Ordering
EQ, Ordering
EQ)

instance Movable Ordering where
  move :: Ordering %1 -> Ur Ordering
move Ordering
LT = Ordering -> Ur Ordering
forall a. a -> Ur a
Ur Ordering
LT
  move Ordering
GT = Ordering -> Ur Ordering
forall a. a -> Ur a
Ur Ordering
GT
  move Ordering
EQ = Ordering -> Ur Ordering
forall a. a -> Ur a
Ur Ordering
EQ

-- TODO: instances for longer primitive tuples
-- TODO: default instances based on the Generic framework

instance (Consumable a, Consumable b) => Consumable (a, b) where
  consume :: (a, b) %1 -> ()
consume (a
a, b
b) = a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume a
a () %1 -> () %1 -> ()
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` b %1 -> ()
forall a. Consumable a => a %1 -> ()
consume b
b

instance (Dupable a, Dupable b) => Dupable (a, b) where
  dupV :: forall (n :: Nat). KnownNat n => (a, b) %1 -> V n (a, b)
dupV (a
a, b
b) = (,) (a %1 -> b %1 -> (a, b)) -> V n a %1 -> V n (b %1 -> (a, b))
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> V n a
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV a
a V n (b %1 -> (a, b)) %1 -> V n b %1 -> V n (a, b)
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> b %1 -> V n b
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV b
b

instance (Movable a, Movable b) => Movable (a, b) where
  move :: (a, b) %1 -> Ur (a, b)
move (a
a, b
b) = (,) (a %1 -> b %1 -> (a, b)) -> Ur a %1 -> Ur (b %1 -> (a, b))
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
a Ur (b %1 -> (a, b)) %1 -> Ur b %1 -> Ur (a, b)
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> b %1 -> Ur b
forall a. Movable a => a %1 -> Ur a
move b
b

instance (Consumable a, Consumable b, Consumable c) => Consumable (a, b, c) where
  consume :: (a, b, c) %1 -> ()
consume (a
a, b
b, c
c) = a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume a
a () %1 -> () %1 -> ()
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` b %1 -> ()
forall a. Consumable a => a %1 -> ()
consume b
b () %1 -> () %1 -> ()
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` c %1 -> ()
forall a. Consumable a => a %1 -> ()
consume c
c

instance (Dupable a, Dupable b, Dupable c) => Dupable (a, b, c) where
  dupV :: forall (n :: Nat). KnownNat n => (a, b, c) %1 -> V n (a, b, c)
dupV (a
a, b
b, c
c) = (,,) (a %1 -> b %1 -> c %1 -> (a, b, c))
-> V n a %1 -> V n (b %1 -> c %1 -> (a, b, c))
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> V n a
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV a
a V n (b %1 -> c %1 -> (a, b, c))
%1 -> V n b %1 -> V n (c %1 -> (a, b, c))
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> b %1 -> V n b
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV b
b V n (c %1 -> (a, b, c)) %1 -> V n c %1 -> V n (a, b, c)
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> c %1 -> V n c
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV c
c

instance (Movable a, Movable b, Movable c) => Movable (a, b, c) where
  move :: (a, b, c) %1 -> Ur (a, b, c)
move (a
a, b
b, c
c) = (,,) (a %1 -> b %1 -> c %1 -> (a, b, c))
-> Ur a %1 -> Ur (b %1 -> c %1 -> (a, b, c))
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
a Ur (b %1 -> c %1 -> (a, b, c))
%1 -> Ur b %1 -> Ur (c %1 -> (a, b, c))
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> b %1 -> Ur b
forall a. Movable a => a %1 -> Ur a
move b
b Ur (c %1 -> (a, b, c)) %1 -> Ur c %1 -> Ur (a, b, c)
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> c %1 -> Ur c
forall a. Movable a => a %1 -> Ur a
move c
c

instance Consumable a => Consumable (Prelude.Maybe a) where
  consume :: Maybe a %1 -> ()
consume Maybe a
Prelude.Nothing = ()
  consume (Prelude.Just a
x) = a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume a
x

instance Dupable a => Dupable (Prelude.Maybe a) where
  dupV :: forall (n :: Nat). KnownNat n => Maybe a %1 -> V n (Maybe a)
dupV Maybe a
Prelude.Nothing = Maybe a -> V n (Maybe a)
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure Maybe a
forall a. Maybe a
Prelude.Nothing
  dupV (Prelude.Just a
x) = (a %1 -> Maybe a) -> V n a %1 -> V n (Maybe a)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.fmap a %1 -> Maybe a
forall a. a -> Maybe a
Prelude.Just (a %1 -> V n a
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV a
x)

instance Movable a => Movable (Prelude.Maybe a) where
  move :: Maybe a %1 -> Ur (Maybe a)
move (Maybe a
Prelude.Nothing) = Maybe a -> Ur (Maybe a)
forall a. a -> Ur a
Ur Maybe a
forall a. Maybe a
Prelude.Nothing
  move (Prelude.Just a
x) = (a %1 -> Maybe a) -> Ur a %1 -> Ur (Maybe a)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.fmap a %1 -> Maybe a
forall a. a -> Maybe a
Prelude.Just (a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
x)

instance (Consumable a, Consumable b) => Consumable (Prelude.Either a b) where
  consume :: Either a b %1 -> ()
consume (Prelude.Left a
a) = a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume a
a
  consume (Prelude.Right b
b) = b %1 -> ()
forall a. Consumable a => a %1 -> ()
consume b
b

instance (Dupable a, Dupable b) => Dupable (Prelude.Either a b) where
  dupV :: forall (n :: Nat). KnownNat n => Either a b %1 -> V n (Either a b)
dupV (Prelude.Left a
a) = (a %1 -> Either a b) -> V n a %1 -> V n (Either a b)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.fmap a %1 -> Either a b
forall a b. a -> Either a b
Prelude.Left (a %1 -> V n a
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV a
a)
  dupV (Prelude.Right b
b) = (b %1 -> Either a b) -> V n b %1 -> V n (Either a b)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.fmap b %1 -> Either a b
forall a b. b -> Either a b
Prelude.Right (b %1 -> V n b
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV b
b)

instance (Movable a, Movable b) => Movable (Prelude.Either a b) where
  move :: Either a b %1 -> Ur (Either a b)
move (Prelude.Left a
a) = (a %1 -> Either a b) -> Ur a %1 -> Ur (Either a b)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.fmap a %1 -> Either a b
forall a b. a -> Either a b
Prelude.Left (a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
a)
  move (Prelude.Right b
b) = (b %1 -> Either a b) -> Ur b %1 -> Ur (Either a b)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.fmap b %1 -> Either a b
forall a b. b -> Either a b
Prelude.Right (b %1 -> Ur b
forall a. Movable a => a %1 -> Ur a
move b
b)

instance Consumable a => Consumable [a] where
  consume :: [a] %1 -> ()
consume [] = ()
  consume (a
a:[a]
l) = a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume a
a () %1 -> () %1 -> ()
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` [a] %1 -> ()
forall a. Consumable a => a %1 -> ()
consume [a]
l

instance Dupable a => Dupable [a] where
  dupV :: forall (n :: Nat). KnownNat n => [a] %1 -> V n [a]
dupV [] = [a] -> V n [a]
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure []
  dupV (a
a:[a]
l) = (:) (a %1 -> [a] %1 -> [a]) -> V n a %1 -> V n ([a] %1 -> [a])
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> V n a
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV a
a V n ([a] %1 -> [a]) %1 -> V n [a] %1 -> V n [a]
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> [a] %1 -> V n [a]
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV [a]
l

instance Movable a => Movable [a] where
  move :: [a] %1 -> Ur [a]
move [] = [a] -> Ur [a]
forall a. a -> Ur a
Ur []
  move (a
a:[a]
l) = (:) (a %1 -> [a] %1 -> [a]) -> Ur a %1 -> Ur ([a] %1 -> [a])
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
a Ur ([a] %1 -> [a]) %1 -> Ur [a] %1 -> Ur [a]
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> [a] %1 -> Ur [a]
forall a. Movable a => a %1 -> Ur a
move [a]
l

instance Consumable a => Consumable (NonEmpty a) where
  consume :: NonEmpty a %1 -> ()
consume (a
x :| [a]
xs) = a %1 -> ()
forall a. Consumable a => a %1 -> ()
consume a
x () %1 -> () %1 -> ()
forall a b. Consumable a => a %1 -> b %1 -> b
`lseq` [a] %1 -> ()
forall a. Consumable a => a %1 -> ()
consume [a]
xs

instance Dupable a => Dupable (NonEmpty a) where
  dupV :: forall (n :: Nat). KnownNat n => NonEmpty a %1 -> V n (NonEmpty a)
dupV (a
x :| [a]
xs) = a %1 -> [a] %1 -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
(:|) (a %1 -> [a] %1 -> NonEmpty a)
-> V n a %1 -> V n ([a] %1 -> NonEmpty a)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> V n a
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV a
x V n ([a] %1 -> NonEmpty a) %1 -> V n [a] %1 -> V n (NonEmpty a)
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> [a] %1 -> V n [a]
forall a (n :: Nat). (Dupable a, KnownNat n) => a %1 -> V n a
dupV [a]
xs

instance Movable a => Movable (NonEmpty a) where
  move :: NonEmpty a %1 -> Ur (NonEmpty a)
move (a
x :| [a]
xs) = a %1 -> [a] %1 -> NonEmpty a
forall a. a -> [a] -> NonEmpty a
(:|) (a %1 -> [a] %1 -> NonEmpty a)
-> Ur a %1 -> Ur ([a] %1 -> NonEmpty a)
forall (f :: * -> *) a b. Functor f => (a %1 -> b) -> f a %1 -> f b
Data.<$> a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
x Ur ([a] %1 -> NonEmpty a) %1 -> Ur [a] %1 -> Ur (NonEmpty a)
forall (f :: * -> *) a b.
Applicative f =>
f (a %1 -> b) %1 -> f a %1 -> f b
Data.<*> [a] %1 -> Ur [a]
forall a. Movable a => a %1 -> Ur a
move [a]
xs

instance Consumable (Ur a) where
  consume :: Ur a %1 -> ()
consume (Ur a
_) = ()

instance Dupable (Ur a) where
  dupV :: forall (n :: Nat). KnownNat n => Ur a %1 -> V n (Ur a)
dupV (Ur a
a) = Ur a -> V n (Ur a)
forall (f :: * -> *) a. Applicative f => a -> f a
Data.pure (a -> Ur a
forall a. a -> Ur a
Ur a
a)

instance Movable (Ur a) where
  move :: Ur a %1 -> Ur (Ur a)
move (Ur a
a) = Ur a -> Ur (Ur a)
forall a. a -> Ur a
Ur (a -> Ur a
forall a. a -> Ur a
Ur a
a)

instance Prelude.Functor Ur where
  fmap :: forall a b. (a -> b) -> Ur a -> Ur b
fmap a -> b
f (Ur a
a) = b -> Ur b
forall a. a -> Ur a
Ur (a -> b
f a
a)

instance Prelude.Applicative Ur where
  pure :: forall a. a -> Ur a
pure = a -> Ur a
forall a. a -> Ur a
Ur
  Ur a -> b
f <*> :: forall a b. Ur (a -> b) -> Ur a -> Ur b
<*> Ur a
x = b -> Ur b
forall a. a -> Ur a
Ur (a -> b
f a
x)

instance Data.Functor Ur where
  fmap :: forall a b. (a %1 -> b) -> Ur a %1 -> Ur b
fmap a %1 -> b
f (Ur a
a) = b -> Ur b
forall a. a -> Ur a
Ur (a %1 -> b
f a
a)

instance Data.Applicative Ur where
  pure :: forall a. a -> Ur a
pure = a -> Ur a
forall a. a -> Ur a
Ur
  Ur a %1 -> b
f <*> :: forall a b. Ur (a %1 -> b) %1 -> Ur a %1 -> Ur b
<*> Ur a
x = b -> Ur b
forall a. a -> Ur a
Ur (a %1 -> b
f a
x)

instance Prelude.Foldable Ur where
  foldMap :: forall m a. Monoid m => (a -> m) -> Ur a -> m
foldMap a -> m
f (Ur a
x) = a -> m
f a
x

instance Prelude.Traversable Ur where
  sequenceA :: forall (f :: * -> *) a. Applicative f => Ur (f a) -> f (Ur a)
sequenceA (Ur f a
x) = (a -> Ur a) -> f a -> f (Ur a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.fmap a -> Ur a
forall a. a -> Ur a
Ur f a
x

-- Some stock instances
deriving instance Consumable a => Consumable (Sum a)
deriving instance Dupable a => Dupable (Sum a)
deriving instance Movable a => Movable (Sum a)
deriving instance Consumable a => Consumable (Product a)
deriving instance Dupable a => Dupable (Product a)
deriving instance Movable a => Movable (Product a)
deriving instance Consumable All
deriving instance Dupable All
deriving instance Movable All
deriving instance Consumable Any
deriving instance Dupable Any
deriving instance Movable Any

newtype MovableMonoid a = MovableMonoid a
  deriving (NonEmpty (MovableMonoid a) -> MovableMonoid a
MovableMonoid a -> MovableMonoid a -> MovableMonoid a
(MovableMonoid a -> MovableMonoid a -> MovableMonoid a)
-> (NonEmpty (MovableMonoid a) -> MovableMonoid a)
-> (forall b.
    Integral b =>
    b -> MovableMonoid a -> MovableMonoid a)
-> Semigroup (MovableMonoid a)
forall b. Integral b => b -> MovableMonoid a -> MovableMonoid a
forall a.
Semigroup a =>
NonEmpty (MovableMonoid a) -> MovableMonoid a
forall a.
Semigroup a =>
MovableMonoid a -> MovableMonoid a -> MovableMonoid a
forall a b.
(Semigroup a, Integral b) =>
b -> MovableMonoid a -> MovableMonoid a
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b. Integral b => b -> MovableMonoid a -> MovableMonoid a
$cstimes :: forall a b.
(Semigroup a, Integral b) =>
b -> MovableMonoid a -> MovableMonoid a
sconcat :: NonEmpty (MovableMonoid a) -> MovableMonoid a
$csconcat :: forall a.
Semigroup a =>
NonEmpty (MovableMonoid a) -> MovableMonoid a
<> :: MovableMonoid a -> MovableMonoid a -> MovableMonoid a
$c<> :: forall a.
Semigroup a =>
MovableMonoid a -> MovableMonoid a -> MovableMonoid a
Prelude.Semigroup, Semigroup (MovableMonoid a)
MovableMonoid a
Semigroup (MovableMonoid a)
-> MovableMonoid a
-> (MovableMonoid a -> MovableMonoid a -> MovableMonoid a)
-> ([MovableMonoid a] -> MovableMonoid a)
-> Monoid (MovableMonoid a)
[MovableMonoid a] -> MovableMonoid a
MovableMonoid a -> MovableMonoid a -> MovableMonoid a
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
forall {a}. Monoid a => Semigroup (MovableMonoid a)
forall a. Monoid a => MovableMonoid a
forall a. Monoid a => [MovableMonoid a] -> MovableMonoid a
forall a.
Monoid a =>
MovableMonoid a -> MovableMonoid a -> MovableMonoid a
mconcat :: [MovableMonoid a] -> MovableMonoid a
$cmconcat :: forall a. Monoid a => [MovableMonoid a] -> MovableMonoid a
mappend :: MovableMonoid a -> MovableMonoid a -> MovableMonoid a
$cmappend :: forall a.
Monoid a =>
MovableMonoid a -> MovableMonoid a -> MovableMonoid a
mempty :: MovableMonoid a
$cmempty :: forall a. Monoid a => MovableMonoid a
Prelude.Monoid)

instance (Movable a, Prelude.Semigroup a) => Semigroup (MovableMonoid a) where
  MovableMonoid a
a <> :: MovableMonoid a %1 -> MovableMonoid a %1 -> MovableMonoid a
<> MovableMonoid a
b = a %1 -> MovableMonoid a
forall a. a -> MovableMonoid a
MovableMonoid (Semigroup a => Ur a %1 -> Ur a %1 -> a
Ur a %1 -> Ur a %1 -> a
combine (a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
a) (a %1 -> Ur a
forall a. Movable a => a %1 -> Ur a
move a
b))
    where combine :: Prelude.Semigroup a => Ur a %1-> Ur a %1-> a
          combine :: Semigroup a => Ur a %1 -> Ur a %1 -> a
combine (Ur a
x) (Ur a
y) = a
x a -> a -> a
forall a. Semigroup a => a -> a -> a
Prelude.<> a
y
instance (Movable a, Prelude.Monoid a) => Monoid (MovableMonoid a)