-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Overloaded pragmas as a plugin -- -- Implement Overloaded pragmas as a source plugin -- -- For example we can replace -- --
-- {-# LANGUAGE OverloadedStrings #-}
--
--
-- with
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Strings #-}
--
@package overloaded
@version 0.3
-- | Overloaded Categories, desugar Arrow into classes in this
-- module.
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Categories #-}
--
--
-- -- catAssoc -- :: CartesianCategory cat -- => cat (Product cat (Product cat a b) c) (Product cat a (Product cat b c)) -- catAssoc = proc ((x, y), z) -> identity -< (x, (y, z)) ---- -- are desugared to (a mess which is) -- --
-- fanout (proj1 %% proj1) (fanout (proj2 %% proj1) proj2) ---- -- If you are familiar with arrows-operators, this is similar to -- --
-- (fst . fst) &&& (snd . fst &&& snd) ---- -- expression. -- -- The catAssoc could be instantiated to cat = (->), -- or more interestingly for example instantiate it to STLC morphisms to -- get an expression like: -- --
-- Lam (Pair (Fst (Fst (Var Here))) (Pair (Snd (Fst (Var Here))) (Snd (Var Here)))) ---- -- proc notation is nicer than writing de Bruijn indices. -- -- This is very similar idea to Conal Elliott's Compiling to -- Categories work. This approach is syntactically more heavy, but -- works in more correct stage of compiler, before actual desugarer. -- -- As one more example, we implement the automatic differentiation, as in -- Conal's paper(s). To keep things simple we use -- --
-- newtype AD a b = AD (a -> (b, a -> b)) ---- -- representation, i.e. use ordinary maps to represent linear maps. We -- then define a function -- --
-- evaluateAD :: Functor f => AD a b -> a -> f a -> (b, f b) -- evaluateAD (AD f) x xs = let (y, f') = f x in (y, fmap f' xs) ---- -- which would allow to calculuate function value and derivatives in -- given directions. Then we can define simple quadratic function: -- --
-- quad :: AD (Double, Double) Double -- quad = proc (x, y) -> do -- x2 <- mult -< (x, x) -- y2 <- mult -< (y, y) -- plus -< (x2, y2) ---- -- It's not as simple as writing quad x y = x * x + y * y, but -- not too far. -- -- Then we can play with it. At origo everything is zero: -- --
-- let sqrthf = 1 / sqrt 2 -- in evaluateAD quad (0, 0) [(1,0), (0,1), (sqrthf, sqrthf)] = (0.0,[0.0,0.0,0.0]) ---- -- If we evaluate at some other point, we see things working: -- --
-- evaluateAD quad (1, 2) [(1,0), (0,1), (sqrthf, sqrthf)] = (5.0,[2.0,4.0,4.242640687119285]) ---- -- Obviously, if we would use inspectable representation for linear maps, -- as Conal describe, we'd get more benefits. And then arr -- wouldn't be definable! module Overloaded.Categories -- | A class for categories. Instances should satisfy the laws -- --
-- fromChar 'x' --class FromChar a fromChar :: FromChar a => Char -> a instance Overloaded.Chars.FromChar GHC.Types.Char module Overloaded.CodeLabels -- | Class for auto-spliced labels -- -- The labels #lbl is desugared into $$(codeFromlabel -- @"lbl") splice. -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:CodeLabels #-}
--
--
-- This feature is not very usable, see
-- https://gitlab.haskell.org/ghc/ghc/-/issues/18211
class IsCodeLabel (sym :: Symbol) a
codeFromLabel :: IsCodeLabel sym a => SpliceQ a
module Overloaded.CodeStrings
-- | Class for auto-spliced string literals
--
-- The string literals "beer" is desugared into
-- $$(codeFromString @"beer") splice.
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:CodeLabels #-}
--
--
-- This feature is not very usable, see
-- https://gitlab.haskell.org/ghc/ghc/-/issues/18211
class IsCodeString a
codeFromString :: IsCodeString a => String -> SpliceQ a
instance (a GHC.Types.~ GHC.Types.Char) => Overloaded.CodeStrings.IsCodeString [a]
instance Overloaded.CodeStrings.IsCodeString Data.ByteString.Internal.ByteString
-- | Overloaded "local" do-blocks.
--
-- Inspired by Local Do GHC-proposal. Yet because we do desugaring
-- in reader phase, we must have a bit more complicated setup.
--
-- The expressions like
--
-- -- ex2d :: IxStateT Identity Int String () -- ex2d = ixmonad.do -- _unused <- ixmodify show -- ixmodify reverse ---- -- are desugared into -- --
-- ex2b :: IxStateT Identity Int String () -- ex2b = -- ixmonad @Bind (ixmodify show) $ \_unused -> -- ixmodify reverse ---- -- Allowing to locally overload what do desugars to. -- -- The monad in this module is an example how to define a -- desugaring. We need to it this way, so the names are easily accessible -- in renamer phase. (I.e. constant, then transformation is pure, as we -- don't need to lookup them for each do-block). -- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Do #-}
--
module Overloaded.Do
data DoMethod
-- | return
Pure :: DoMethod
-- | >>
Then :: DoMethod
-- | >>=
Bind :: DoMethod
type Pure = 'Pure
type Then = 'Then
type Bind = 'Bind
class Monad' (method :: DoMethod) (ty :: Type)
monad :: Monad' method ty => ty
instance (ty GHC.Types.~ (a -> m a), GHC.Base.Applicative m) => Overloaded.Do.Monad' 'Overloaded.Do.Pure ty
instance (ty GHC.Types.~ (m a -> m b -> m b), GHC.Base.Applicative m) => Overloaded.Do.Monad' 'Overloaded.Do.Then ty
instance (ty GHC.Types.~ (m a -> (a -> m b) -> m b), GHC.Base.Monad m) => Overloaded.Do.Monad' 'Overloaded.Do.Bind ty
-- | Overloaded if-expression.
module Overloaded.If
-- | Class for Bool-like datastrucutres
--
-- An if--expression if b then t else e is desugared to
--
-- -- ifte (toBool b) t e ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:If #-}
--
class ToBool b
toBool :: ToBool b => b -> Bool
-- | ToBool overloaded if-expression.
ifte :: ToBool b => b -> a -> a -> a
instance Overloaded.If.ToBool GHC.Types.Bool
instance Overloaded.If.ToBool (GHC.Maybe.Maybe a)
instance Overloaded.If.ToBool (Data.Either.Either b a)
-- | Another way to desugar overloaded list literals. See Nil and
-- Cons.
--
-- An explicit list expression, e.g. [1, True] is desugared to
--
-- -- cons 1 (cons True nil) ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Lists #-}
--
module Overloaded.Lists
-- | Class for nil, []
--
-- See test-suite for ways to define instances for Map. There are
-- at-least two-ways.
class Nil a
nil :: Nil a => a
-- | Class for Cons :.
class Cons x ys zs | zs -> x ys
cons :: Cons x ys zs => x -> ys -> zs
infixr 5 `cons`
fromList :: (Nil xs, Cons x xs xs) => [x] -> xs
instance (a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Cons a [b] [c]
instance (a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Cons a [b] (GHC.Base.NonEmpty c)
instance (GHC.Classes.Ord a, a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Cons a (Data.Set.Internal.Set b) (Data.Set.Internal.Set c)
instance Overloaded.Lists.Cons GHC.Types.Int Data.IntSet.Internal.IntSet Data.IntSet.Internal.IntSet
instance (a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Cons a (Data.Sequence.Internal.Seq b) (Data.Sequence.Internal.Seq c)
instance (GHC.Classes.Ord k, k GHC.Types.~ k1, k GHC.Types.~ k2, v GHC.Types.~ v1, v GHC.Types.~ v2) => Overloaded.Lists.Cons (k, v) (Data.Map.Internal.Map k1 v1) (Data.Map.Internal.Map k2 v2)
instance (i GHC.Types.~ GHC.Types.Int, a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Cons (i, a) (Data.IntMap.Internal.IntMap b) (Data.IntMap.Internal.IntMap c)
instance (a GHC.Types.~ b, b GHC.Types.~ c, m GHC.Types.~ 'Data.Nat.S n) => Overloaded.Lists.Cons a (Data.Vec.Lazy.Vec n b) (Data.Vec.Lazy.Vec m c)
instance forall k (f :: k -> *) (g :: k -> *) (h :: k -> *) (xxs :: [k]) (x :: k) (xs :: [k]). (f GHC.Types.~ g, g GHC.Types.~ h, xxs GHC.Types.~ (x : xs)) => Overloaded.Lists.Cons (f x) (Data.SOP.NP.NP g xs) (Data.SOP.NP.NP h xxs)
instance forall k (f :: k -> *) (g :: k -> *) (h :: k -> *) (xsxss :: [[k]]) (xs :: [k]) (xss :: [[k]]). (f GHC.Types.~ g, g GHC.Types.~ h, xsxss GHC.Types.~ (xs : xss)) => Overloaded.Lists.Cons (Data.SOP.NP.NP f xs) (Data.SOP.NP.POP g xss) (Data.SOP.NP.POP h xsxss)
instance (a GHC.Types.~ b, a GHC.Types.~ c) => Overloaded.Lists.Cons a (Data.RAList.Internal.RAList b) (Data.RAList.Internal.RAList c)
instance (a GHC.Types.~ b, a GHC.Types.~ c) => Overloaded.Lists.Cons a (Data.RAList.Internal.RAList b) (Data.RAList.NonEmpty.Internal.NERAList c)
instance (b GHC.Types.~ 'Data.Bin.BP bb, bp GHC.Types.~ Data.Type.Bin.Pred bb, bb GHC.Types.~ Data.Type.Bin.Succ' bp) => Overloaded.Lists.Cons a (Data.RAVec.RAVec bp a) (Data.RAVec.RAVec b a)
instance (bp GHC.Types.~ Data.Type.Bin.Pred b, b GHC.Types.~ Data.Type.Bin.Succ' bp) => Overloaded.Lists.Cons a (Data.RAVec.RAVec bp a) (Data.RAVec.NonEmpty.NERAVec b a)
instance Overloaded.Lists.Nil [a]
instance Overloaded.Lists.Nil (Data.Set.Internal.Set a)
instance Overloaded.Lists.Nil Data.IntSet.Internal.IntSet
instance Overloaded.Lists.Nil (Data.Sequence.Internal.Seq a)
instance Overloaded.Lists.Nil (Data.Map.Internal.Map k v)
instance Overloaded.Lists.Nil (Data.IntMap.Internal.IntMap v)
instance (n GHC.Types.~ 'Data.Nat.Z) => Overloaded.Lists.Nil (Data.Vec.Lazy.Vec n a)
instance forall k (xs :: [k]) (f :: k -> *). (xs GHC.Types.~ '[]) => Overloaded.Lists.Nil (Data.SOP.NP.NP f xs)
instance forall k (xs :: [[k]]) (f :: k -> *). (xs GHC.Types.~ '[]) => Overloaded.Lists.Nil (Data.SOP.NP.POP f xs)
instance Overloaded.Lists.Nil (Data.RAList.Internal.RAList a)
instance (b GHC.Types.~ 'Data.Bin.BZ) => Overloaded.Lists.Nil (Data.RAVec.RAVec b a)
-- | Another way to desugar list literals.
--
-- An explicit list expression, e.g. [1, True] is desugared to
--
-- -- cons 1 (cons True nil) ---- -- This desugaring uses bidirectional functional dependencies to make -- cons infer more. The trade-off is that we can have strictly -- less instances. -- -- Enabled with: -- --
-- {-# OPTIONS_GHC -fplugin=Overloaded
-- -fplugin-opt=Overloaded:Lists=Overloaded.Lists.Bidi.nil=Overloaded.Lists.Bidi.cons
--
module Overloaded.Lists.Bidi
nil :: Nil a => a
-- | Bidirectional class for Cons :.
class Cons x ys zs => Cons x ys zs | zs -> x ys, x ys -> zs
cons :: Cons x ys zs => x -> ys -> zs
infixr 5 `cons`
fromList :: (Nil xs, Cons x xs xs) => [x] -> xs
instance (a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Bidi.Cons a [b] [c]
instance (GHC.Classes.Ord a, a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Bidi.Cons a (Data.Set.Internal.Set a) (Data.Set.Internal.Set a)
instance Overloaded.Lists.Bidi.Cons GHC.Types.Int Data.IntSet.Internal.IntSet Data.IntSet.Internal.IntSet
instance (a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Bidi.Cons a (Data.Sequence.Internal.Seq b) (Data.Sequence.Internal.Seq c)
instance (GHC.Classes.Ord k, k GHC.Types.~ k1, k GHC.Types.~ k2, v GHC.Types.~ v1, v GHC.Types.~ v2) => Overloaded.Lists.Bidi.Cons (k, v) (Data.Map.Internal.Map k1 v1) (Data.Map.Internal.Map k2 v2)
instance (i GHC.Types.~ GHC.Types.Int, a GHC.Types.~ b, b GHC.Types.~ c) => Overloaded.Lists.Bidi.Cons (i, a) (Data.IntMap.Internal.IntMap b) (Data.IntMap.Internal.IntMap c)
instance (a GHC.Types.~ b, b GHC.Types.~ c, m GHC.Types.~ 'Data.Nat.S n) => Overloaded.Lists.Bidi.Cons a (Data.Vec.Lazy.Vec n b) (Data.Vec.Lazy.Vec m c)
instance forall k (f :: k -> *) (g :: k -> *) (h :: k -> *) (xxs :: [k]) (x :: k) (xs :: [k]). (f GHC.Types.~ g, g GHC.Types.~ h, xxs GHC.Types.~ (x : xs)) => Overloaded.Lists.Bidi.Cons (f x) (Data.SOP.NP.NP g xs) (Data.SOP.NP.NP h xxs)
instance forall k (f :: k -> *) (g :: k -> *) (h :: k -> *) (xsxss :: [[k]]) (xs :: [k]) (xss :: [[k]]). (f GHC.Types.~ g, g GHC.Types.~ h, xsxss GHC.Types.~ (xs : xss)) => Overloaded.Lists.Bidi.Cons (Data.SOP.NP.NP f xs) (Data.SOP.NP.POP g xss) (Data.SOP.NP.POP h xsxss)
-- | Overloaded natural numbers.
module Overloaded.Naturals
-- | Class for Natural-like datastructures
--
-- A numeric literal 42 is desugared to
--
-- -- fromNatural 42 --class FromNatural a fromNatural :: FromNatural a => Natural -> a instance Overloaded.Naturals.FromNatural GHC.Natural.Natural instance Overloaded.Naturals.FromNatural GHC.Integer.Type.Integer -- | Another way to desugar overloaded numeric literals. See -- FromNumeral. module Overloaded.Numerals -- | Another way to desugar numerals. -- -- A numeric literal 123 is desugared to -- --
-- fromNumeral @123 ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Numerals #-}
--
--
-- One can do type-level computations with this.
class FromNumeral (n :: Nat) a
fromNumeral :: FromNumeral n a => a
-- | Default implementation of fromNumeral.
--
-- Usage example:
--
-- -- instance (KnownNat n, ...) => FromNumeral n MyType where -- fromNumeral = defaultFromNumeral @n --defaultFromNumeral :: forall n a. (KnownNat n, Integral a) => a instance Data.Type.BinP.SBinPI b => Overloaded.Numerals.PosFromNumeral 'Data.Nat.Z b instance (Data.Type.BinP.SBinPI bp, Overloaded.Numerals.PosFromNumeral n bp, Data.Type.Bin.Pred b GHC.Types.~ 'Data.Bin.BP bp, Data.Type.BinP.Succ bp GHC.Types.~ b) => Overloaded.Numerals.PosFromNumeral ('Data.Nat.S n) b instance (Overloaded.Numerals.PosFromNumeral (Data.Type.Nat.FromGHC n) b, Overloaded.Numerals.IsLess n (Data.Type.BinP.ToGHC b)) => Overloaded.Numerals.FromNumeral n (Data.BinP.PosP.PosP b) instance (Overloaded.Numerals.PosFromNumeral (Data.Type.Nat.FromGHC n) b, Overloaded.Numerals.IsLess n (Data.Type.BinP.ToGHC b)) => Overloaded.Numerals.FromNumeral n (Data.Bin.Pos.Pos ('Data.Bin.BP b)) instance Overloaded.Numerals.FinFromNumeral 'Data.Nat.Z ('Data.Nat.S m) instance Overloaded.Numerals.FinFromNumeral n m => Overloaded.Numerals.FinFromNumeral ('Data.Nat.S n) ('Data.Nat.S m) instance (Overloaded.Numerals.FinFromNumeral (Data.Type.Nat.FromGHC n) m, Overloaded.Numerals.IsLess n (Data.Type.Nat.ToGHC m)) => Overloaded.Numerals.FromNumeral n (Data.Fin.Fin m) instance (GHC.TypeNats.KnownNat n, Overloaded.Numerals.OverflowCheck n "Word8" (n GHC.TypeNats.<=? 255)) => Overloaded.Numerals.FromNumeral n GHC.Word.Word8 instance (GHC.TypeNats.KnownNat n, Overloaded.Numerals.OverflowCheck n "Word8" (n GHC.TypeNats.<=? 65535)) => Overloaded.Numerals.FromNumeral n GHC.Word.Word16 instance (GHC.TypeNats.KnownNat n, Overloaded.Numerals.OverflowCheck n "Word8" (n GHC.TypeNats.<=? 4294967295)) => Overloaded.Numerals.FromNumeral n GHC.Word.Word32 instance (GHC.TypeNats.KnownNat n, Overloaded.Numerals.OverflowCheck n "Word8" (n GHC.TypeNats.<=? 18446744073709551615)) => Overloaded.Numerals.FromNumeral n GHC.Word.Word64 instance GHC.TypeNats.KnownNat n => Overloaded.Numerals.FromNumeral n GHC.Natural.Natural instance GHC.TypeNats.KnownNat n => Overloaded.Numerals.FromNumeral n GHC.Integer.Type.Integer instance GHC.TypeNats.KnownNat n => Overloaded.Numerals.FromNumeral n GHC.Types.Int instance GHC.TypeNats.KnownNat n => Overloaded.Numerals.FromNumeral n Data.Nat.Nat instance GHC.TypeNats.KnownNat n => Overloaded.Numerals.FromNumeral n Data.Bin.Bin instance (GHC.TypeNats.KnownNat n, (1 GHC.TypeNats.<=? n) GHC.Types.~ 'GHC.Types.True) => Overloaded.Numerals.FromNumeral n Data.BinP.BinP -- | Overloaded plugin, which makes magic possible. module Overloaded.Plugin -- | Overloaded plugin. -- -- To enable plugin put the following at top of the module: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols #-}
--
--
-- At least one option is required, multiple can given either using
-- multiple -fplugin-opt options, or by separating options with
-- colon:
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols:Numerals #-}
--
--
-- Options also take optional desugaring names, for example
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Labels=Data.Generics.ProductFields.field #-}
--
--
-- to desugar OverloadedLabels directly into field from
-- generics-lens (no need to import orphan instance!)
--
--
-- {-# OPTIONS_GHC -ddump-simpl #-}
--
--
-- to avoid surprising segfaults.
--
--
-- class HasField {k} x r a | x r -> a where
-- hasField :: r -> (a -> r, a)
--
--
-- Suppose we have
--
--
-- data R y = MkR { foo :: [y] }
--
--
-- and foo in scope. We will solve constraints like
--
-- -- HasField "foo" (R Int) a ---- -- by emitting a new wanted constraint -- --
-- [Int] ~# a ---- -- and building a HasField dictionary out of selector -- foo appropriately cast. -- --
-- {-# LANGUAGE TemplateHaskellQuotes #-}
-- {-# OPTIONS_GHC -fplugin=Overloaded -fplugin-opt=Overloaded:IdiomBrackets #-}
--
-- data Tree a
-- = Leaf a
-- | Branch (Tree a) (Tree a)
-- deriving (Show)
--
-- instance Functor Tree where
-- fmap f (Leaf x) = Leaf (f x)
-- fmap f (Branch l r) = Branch (fmap f l) (fmap f r)
--
-- instance Traversable Tree where
-- traverse f (Leaf x) = [| Leaf (f x) |]
-- traverse f (Branch l r) = [| Branch (traverse f l) (traverse f r) |]
--
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:RebindableApplication #-}
--
-- let f = pure ((+) :: Int -> Int -> Int)
-- x = Just 1
-- y = Just 2
--
-- z = let ($) = (<*>) in f x y
-- in z
--
plugin :: Plugin
instance GHC.Show.Show Overloaded.Plugin.StrSym
instance GHC.Classes.Eq Overloaded.Plugin.StrSym
instance GHC.Show.Show Overloaded.Plugin.NumNat
instance GHC.Classes.Eq Overloaded.Plugin.NumNat
instance GHC.Show.Show Overloaded.Plugin.LabelOpt
instance GHC.Classes.Eq Overloaded.Plugin.LabelOpt
instance GHC.Show.Show a => GHC.Show.Show (Overloaded.Plugin.OnOff a)
instance GHC.Classes.Eq a => GHC.Classes.Eq (Overloaded.Plugin.OnOff a)
instance GHC.Show.Show Overloaded.Plugin.Options
instance GHC.Classes.Eq Overloaded.Plugin.Options
-- | Another way to desugar overloaded string literals. See
-- FromSymbol.
module Overloaded.Symbols
-- | Another way to desugar overloaded string literals using this class.
--
-- A string literal "example" is desugared to
--
-- -- fromSymbol @"example" ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols #-}
--
class FromSymbol (s :: Symbol) a
fromSymbol :: FromSymbol s a => a
instance (GHC.TypeNats.KnownNat y, GHC.TypeNats.KnownNat m, GHC.TypeNats.KnownNat d, Overloaded.Symbols.ParseDay s GHC.Types.~ '(y, m, d)) => Overloaded.Symbols.FromSymbol s Data.Time.Calendar.Days.Day
instance (GHC.TypeLits.KnownSymbol s, Overloaded.Symbols.SeqList (Data.Symbol.Ascii.Internal.ToList s)) => Overloaded.Symbols.FromSymbol s Data.ByteString.Internal.ByteString
instance (GHC.TypeLits.KnownSymbol s, Overloaded.Symbols.SeqList (Data.Symbol.Ascii.Internal.ToList s)) => Overloaded.Symbols.FromSymbol s Data.ByteString.Lazy.Internal.ByteString
instance (GHC.TypeLits.KnownSymbol s, a GHC.Types.~ GHC.Types.Char) => Overloaded.Symbols.FromSymbol s [a]
instance GHC.TypeLits.KnownSymbol s => Overloaded.Symbols.FromSymbol s Data.Text.Internal.Text
instance GHC.TypeLits.KnownSymbol s => Overloaded.Symbols.FromSymbol s Data.Text.Internal.Lazy.Text
-- | Overloaded type-level natural numbers.
module Overloaded.TypeNats
-- | A way to overload type level Nats.
--
-- A number type-literal 42 is desugared to
--
-- -- FromNat 42 ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:TypeNats #-}
--
class FromNatC a where {
type family FromNat (n :: Nat) :: a;
}
instance Overloaded.TypeNats.FromNatC GHC.Types.Nat
instance Overloaded.TypeNats.FromNatC Data.Nat.Nat
instance Overloaded.TypeNats.FromNatC Data.Bin.Bin
instance Overloaded.TypeNats.FromNatC Data.BinP.BinP
-- | Overloaded type-level symbols.
module Overloaded.TypeSymbols
-- | A way to overload type level Symbols.
--
-- A symbol type-literal "example" is desugared to
--
-- -- FromTypeSymbol "example" ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:TypeSymbols #-}
--
class FromTypeSymbolC a where {
type family FromTypeSymbol (s :: Symbol) :: a;
}
instance Overloaded.TypeSymbols.FromTypeSymbolC GHC.Types.Symbol
-- | Overloaded* language extensions as a source plugin.
module Overloaded
-- | Overloaded plugin.
--
-- To enable plugin put the following at top of the module:
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols #-}
--
--
-- At least one option is required, multiple can given either using
-- multiple -fplugin-opt options, or by separating options with
-- colon:
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols:Numerals #-}
--
--
-- Options also take optional desugaring names, for example
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Labels=Data.Generics.ProductFields.field #-}
--
--
-- to desugar OverloadedLabels directly into field from
-- generics-lens (no need to import orphan instance!)
--
--
-- {-# OPTIONS_GHC -ddump-simpl #-}
--
--
-- to avoid surprising segfaults.
--
--
-- class HasField {k} x r a | x r -> a where
-- hasField :: r -> (a -> r, a)
--
--
-- Suppose we have
--
--
-- data R y = MkR { foo :: [y] }
--
--
-- and foo in scope. We will solve constraints like
--
-- -- HasField "foo" (R Int) a ---- -- by emitting a new wanted constraint -- --
-- [Int] ~# a ---- -- and building a HasField dictionary out of selector -- foo appropriately cast. -- --
-- {-# LANGUAGE TemplateHaskellQuotes #-}
-- {-# OPTIONS_GHC -fplugin=Overloaded -fplugin-opt=Overloaded:IdiomBrackets #-}
--
-- data Tree a
-- = Leaf a
-- | Branch (Tree a) (Tree a)
-- deriving (Show)
--
-- instance Functor Tree where
-- fmap f (Leaf x) = Leaf (f x)
-- fmap f (Branch l r) = Branch (fmap f l) (fmap f r)
--
-- instance Traversable Tree where
-- traverse f (Leaf x) = [| Leaf (f x) |]
-- traverse f (Branch l r) = [| Branch (traverse f l) (traverse f r) |]
--
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:RebindableApplication #-}
--
-- let f = pure ((+) :: Int -> Int -> Int)
-- x = Just 1
-- y = Just 2
--
-- z = let ($) = (<*>) in f x y
-- in z
--
plugin :: Plugin
-- | Another way to desugar overloaded string literals using this class.
--
-- A string literal "example" is desugared to
--
-- -- fromSymbol @"example" ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Symbols #-}
--
class FromSymbol (s :: Symbol) a
fromSymbol :: FromSymbol s a => a
-- | Another way to desugar numerals.
--
-- A numeric literal 123 is desugared to
--
-- -- fromNumeral @123 ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:Numerals #-}
--
--
-- One can do type-level computations with this.
class FromNumeral (n :: Nat) a
fromNumeral :: FromNumeral n a => a
-- | Default implementation of fromNumeral.
--
-- Usage example:
--
-- -- instance (KnownNat n, ...) => FromNumeral n MyType where -- fromNumeral = defaultFromNumeral @n --defaultFromNumeral :: forall n a. (KnownNat n, Integral a) => a -- | Class for Natural-like datastructures -- -- A numeric literal 42 is desugared to -- --
-- fromNatural 42 --class FromNatural a fromNatural :: FromNatural a => Natural -> a -- | Class for Char-like datastructures -- -- A character literal x is desugared to -- --
-- fromChar 'x' --class FromChar a fromChar :: FromChar a => Char -> a -- | Class for nil, [] -- -- See test-suite for ways to define instances for Map. There are -- at-least two-ways. class Nil a nil :: Nil a => a -- | Class for Cons :. class Cons x ys zs | zs -> x ys cons :: Cons x ys zs => x -> ys -> zs infixr 5 `cons` -- | Class for Bool-like datastrucutres -- -- An if--expression if b then t else e is desugared to -- --
-- ifte (toBool b) t e ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:If #-}
--
class ToBool b
toBool :: ToBool b => b -> Bool
-- | ToBool overloaded if-expression.
ifte :: ToBool b => b -> a -> a -> a
-- | Class for auto-spliced labels
--
-- The labels #lbl is desugared into $$(codeFromlabel
-- @"lbl") splice.
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:CodeLabels #-}
--
--
-- This feature is not very usable, see
-- https://gitlab.haskell.org/ghc/ghc/-/issues/18211
class IsCodeLabel (sym :: Symbol) a
codeFromLabel :: IsCodeLabel sym a => SpliceQ a
-- | Class for auto-spliced string literals
--
-- The string literals "beer" is desugared into
-- $$(codeFromString @"beer") splice.
--
--
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:CodeLabels #-}
--
--
-- This feature is not very usable, see
-- https://gitlab.haskell.org/ghc/ghc/-/issues/18211
class IsCodeString a
codeFromString :: IsCodeString a => String -> SpliceQ a
-- | A way to overload type level Nats.
--
-- A number type-literal 42 is desugared to
--
-- -- FromNat 42 ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:TypeNats #-}
--
class FromNatC a where {
type family FromNat (n :: Nat) :: a;
}
-- | A way to overload type level Symbols.
--
-- A symbol type-literal "example" is desugared to
--
-- -- FromTypeSymbol "example" ---- -- Enabled with: -- --
-- {-# OPTIONS -fplugin=Overloaded -fplugin-opt=Overloaded:TypeSymbols #-}
--
class FromTypeSymbolC a where {
type family FromTypeSymbol (s :: Symbol) :: a;
}
data DoMethod
-- | return
Pure :: DoMethod
-- | >>
Then :: DoMethod
-- | >>=
Bind :: DoMethod
type Pure = 'Pure
type Then = 'Then
type Bind = 'Bind
class Monad' (method :: DoMethod) (ty :: Type)
monad :: Monad' method ty => ty
-- | A class for categories. Instances should satisfy the laws
--
--
class Category (cat :: k -> k -> Type)
-- | A non-clashing name for id.
identity :: Category cat => cat a a
-- | A non-clashing name for (.).
(%%) :: Category cat => cat b c -> cat a b -> cat a c
infixr 9 %%
-- | Cartesian category is a monoidal category where monoidal product is
-- the categorical product.
class Category cat => CartesianCategory (cat :: k -> k -> Type) where {
type family Product cat :: k -> k -> k;
}
proj1 :: CartesianCategory cat => cat (Product cat a b) a
proj2 :: CartesianCategory cat => cat (Product cat a b) b
-- | fanout f g is written as <math> in category
-- theory literature.
fanout :: CartesianCategory cat => cat a b -> cat a c -> cat a (Product cat b c)
-- | Cocartesian category is a monoidal category where monoidal product is
-- the categorical coproduct.
class Category cat => CocartesianCategory (cat :: k -> k -> Type) where {
type family Coproduct cat :: k -> k -> k;
}
inl :: CocartesianCategory cat => cat a (Coproduct cat a b)
inr :: CocartesianCategory cat => cat b (Coproduct cat a b)
-- | fanin f g is written as <math> in category
-- theory literature.
fanin :: CocartesianCategory cat => cat a c -> cat b c -> cat (Coproduct cat a b) c
-- | Bicartesian category is category which is both cartesian and
-- cocartesian.
--
-- We also require distributive morpism.
class (CartesianCategory cat, CocartesianCategory cat) => BicartesianCategory cat
distr :: BicartesianCategory cat => cat (Product cat (Coproduct cat a b) c) (Coproduct cat (Product cat a c) (Product cat b c))
-- | Closed cartesian category.
class CartesianCategory cat => CCC (cat :: k -> k -> Type) where {
-- | Exponential cat a b represents <math>. This is
-- due how (->) works.
type family Exponential cat :: k -> k -> k;
}
eval :: CCC cat => cat (Product cat (Exponential cat a b) a) b
transpose :: CCC cat => cat (Product cat a b) c -> cat a (Exponential cat b c)