Skip to content

Instantly share code, notes, and snippets.

@kosmikus
kosmikus / Example.hs
Created April 2, 2022 12:55
CLI parser using generics-sop
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
@kosmikus
kosmikus / TinyServant.hs
Created November 1, 2015 20:30
Implementation of a small Servant-like DSL
{-# LANGUAGE DataKinds, PolyKinds, TypeOperators #-}
{-# LANGUAGE TypeFamilies, FlexibleInstances, ScopedTypeVariables #-}
{-# LANGUAGE InstanceSigs #-}
module TinyServant where
import Control.Applicative
import GHC.TypeLits
import Text.Read
import Data.Time
@kosmikus
kosmikus / Tutorial.hs
Last active May 1, 2020 23:59
Preliminary rewrite of Gabriel's lens tutorial to use `optics` instead of `lens`
{-| This @lens@ tutorial targets Haskell beginners and assumes only basic
familiarity with Haskell. By the end of this tutorial you should:
* understand what problems the @lens@ library solves,
* know when it is appropriate to use the @lens@ library,
* be proficient in the most common @lens@ idioms,
* understand the drawbacks of using lenses, and:
{-# LANGUAGE DataKinds, FlexibleContexts, TypeFamilies, TypeOperators #-}
module SOPTail where
import Data.SOP
import Data.SOP.Dict
test :: (All c ys, ys ~ (x : xs)) => Proxy x -> Proxy c -> Dict (All c) xs
test _ _ = Dict
{-# LANGUAGE DerivingVia, ScopedTypeVariables, StandaloneDeriving, TypeApplications, TypeFamilies #-}
module DataFamilyDerivingVia where
import Data.Coerce
data family Foo a
newtype ByBar a = ByBar a
class Baz a where
@kosmikus
kosmikus / Person.hs
Created April 2, 2018 11:11
Generic validation using generic-sop
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
module Person where
import Generics.SOP
import qualified GHC.Generics as GHC
data Person' f =
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
module CustomShowEnum where
@kosmikus
kosmikus / CurryNP4.hs
Created October 20, 2018 06:32
Curry / uncurry in generics-sop
{-# LANGUAGE DataKinds, PolyKinds, MultiParamTypeClasses, TypeFamilies, TypeOperators #-}
{-# LANGUAGE FlexibleInstances, FlexibleContexts, UndecidableInstances, ScopedTypeVariables #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE UndecidableSuperClasses, TypeApplications #-}
{-# LANGUAGE ConstraintKinds #-}
module CurryNP4 where
import Data.Kind
import Generics.SOP
import GHC.Exts
@kosmikus
kosmikus / TypeFamilies.hs
Created October 8, 2018 18:15
Bug in ghc-8.6.1
{-# LANGUAGE TypeFamilies #-}
module TypeFamilies where
type family A (x :: *) :: * where
A x = x
type family B (x :: *) :: * where
A x = x -- note A, not B !!
-- ghc-8.6.1 accepts this program, whereas ghc-8.4.3 reports an error
@kosmikus
kosmikus / SOPNF.hs
Created April 18, 2018 12:30
DerivingVia and SOP experiments
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}