Skip to content

Instantly share code, notes, and snippets.

@Lysxia
Lysxia / testeq.hs
Created May 29, 2021 05:49
Generic implementation of TestEquality
{-# LANGUAGE KindSignatures, DataKinds, TemplateHaskell, GADTs, TypeFamilies, TypeOperators, MultiParamTypeClasses, FlexibleInstances, FlexibleContexts, UndecidableInstances, QuantifiedConstraints, ConstraintKinds, PolyKinds, TypeApplications, ScopedTypeVariables #-}
import Data.Kind (Type)
import Data.Type.Equality
import Data.Maybe (isJust, isNothing)
import Generics.Kind hiding ((:~:))
import Generics.Kind.TH
-- * Example
@tfausak
tfausak / language-extension-examples.hs
Created August 1, 2020 23:36
Minimal examples of Haskell language extensions.
a = a :: Eq b => () -- AllowAmbiguousTypes
a = proc b -> id -< b -- Arrows
a = let !b = () in b -- BangPatterns
a = 0b0 -- BinaryLiterals
a = id do 0 -- BlockArguments
foreign import capi "" a :: ()
class A b where c :: Eq b => b -- ConstrainedClassMethods
type A = Eq -- ConstraintKinds
# -- CPP
import Data.Proxy; a = Proxy :: Proxy True -- DataKinds
{-# LANGUAGE DataKinds, UndecidableInstances, ConstraintKinds, GADTs, FlexibleInstances, EmptyCase, LambdaCase #-}
{-# OPTIONS_GHC -Wall #-}
{-# OPTIONS_GHC -fno-warn-unticked-promoted-constructors #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import GHC.TypeLits
data Dict c where
Dict :: c => Dict c
@ndmitchell
ndmitchell / Binary.hs
Last active August 10, 2021 21:42
Binary existentials
{-# LANGUAGE StaticPointers #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleInstances #-}
import Data.Binary
import System.IO.Unsafe
import GHC.StaticPtr
@RyanGlScott
RyanGlScott / NewGenerics.hs
Created June 6, 2017 16:30
Fleshing out a new design for Generic1 that doesn't use Functor contexts for derived instances, but rather Coercible.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- | Fleshing out a new design for Generic1 that doesn't use Functor contexts
-- for derived instances, but rather Coercible. Why would we want this?
-- Consider this derived Generic1 instance:
--
-- data T f a = T (f [a]) deriving Generic1

Monad gives Applicative, Applicative etc. gives Num, Floating, Fractional

Setting it up

WrapMonad tells us that a Monad implies Functor, Applicative

instance Monad m => Functor     (WrappedMonad m)
instance Monad m => Applicative (WrappedMonad m)
instance Monad m => Monad       (WrappedMonad m)
@sjoerdvisscher
sjoerdvisscher / HasSuperClassesProduct.hs
Created March 26, 2017 17:18
HasSuperClasses for Constraint product.
class (c x, d x) => (c :&: d) x
instance (c x, d x) => (c :&: d) x
instance (HasSuperClasses c, HasSuperClasses d, Distr (SuperClasses c)) => HasSuperClasses (c :&: d) where
type SuperClasses (c :&: d) = (c :&: d) ': SuperClasses c ++ SuperClasses d
superClasses = h superClasses superClasses
where
h :: forall x. c x :- FoldConstraints (SuperClasses c) x
-> d x :- FoldConstraints (SuperClasses d) x
-> (c :&: d) x :- FoldConstraints (SuperClasses (c :&: d)) x