Skip to content

Instantly share code, notes, and snippets.

@Lysxia
Lysxia / testeq.hs
Created May 29, 2021
Generic implementation of TestEquality
View testeq.hs
{-# 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 Aug 1, 2020
Minimal examples of Haskell language extensions.
View language-extension-examples.hs
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
View absurd.hs
{-# 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 Aug 10, 2021
Binary existentials
View Binary.hs
{-# 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 Jun 6, 2017
Fleshing out a new design for Generic1 that doesn't use Functor contexts for derived instances, but rather Coercible.
View NewGenerics.hs
{-# 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
View NewtypeDeriving.markdown

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 Mar 26, 2017
HasSuperClasses for Constraint product.
View HasSuperClassesProduct.hs
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