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 #-}
{-# 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 / 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:
@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 #-}
@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 =
@kosmikus
kosmikus / Buildable.hs
Created March 27, 2018 21:46
sop-based generic editor
{-# LANGUAGE FlexibleContexts, MultiParamTypeClasses, GADTs, DefaultSignatures, DeriveAnyClass, StandaloneDeriving, ScopedTypeVariables, TypeApplications #-}
module Buildable where
import Generics.SOP
import Generics.SOP.Metadata
class Buildable a where
build :: IO a
default build :: (Generic a, HasDatatypeInfo a, All2 Buildable (Code a)) => IO a
build = gbuild
@kosmikus
kosmikus / EnumRead.hs
Created March 19, 2018 10:49
Generic read for enum types (based on lowercase constructor names) using generics-sop
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MonoLocalBinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module EnumRead where
import Data.Char
import qualified Data.Map
import Generics.SOP