Skip to content

Instantly share code, notes, and snippets.

import GHC.Prim
import GHC.TypeLits
import SuperRecord
type BuildRecA c lts =
( BuildRecA' c lts
, KnownNat (RecSize lts)
)
buildRecA
@carymrobbins
carymrobbins / BuildRec.hs
Created September 23, 2021 20:54
Generalized SuperRecord builders
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module NEL where
import Data.List.NonEmpty (NonEmpty((:|)))
import qualified Data.List.NonEmpty as NonEmpty
nel :: (NEL a r) => a -> r
nel a = nelgo (a :|)
{-# INLINE nel #-}
@carymrobbins
carymrobbins / SuperRecordRead.hs
Created September 20, 2021 18:06
Read instance for superrecord Rec type in Haskell - https://hackage.haskell.org/package/superrecord-0.5.1.0
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
@carymrobbins
carymrobbins / profile.sh
Last active August 23, 2021 19:20
Bash function for profiling a command
profile() {
local size=10
local scale=3
local unit
while true; do
case "$1" in
-n|--size) size=$2; shift; shift;;
-p|--scale) scale=$2; shift; shift;;
-u|--unit) unit=$2; shift; shift;;
--) shift; break;;
#!/usr/bin/env stack
{- stack
--resolver lts-15.6
--install-ghc runghc
-}
-- | Demonstrates the 'showName' function.
--
-- @TemplateHaskellQuotes@ is enabled so we can use the @'name@ syntax.
-- @DuplicateRecordFields@ is enabled to show that we handle name mangling.
#!/usr/bin/env python3
import enum
def match(v, allow_partial=False, allow_impossible=False):
"""Provides exhaustive pattern matching for Enum."""
if not isinstance(v, enum.Enum):
raise ValueError(f'Supplied value is not an Enum: {v}')
@carymrobbins
carymrobbins / shouldNotCompile.hs
Created March 1, 2021 22:38
Test infra to assert that code does not compile
-- | Test support for asserting that expressions should not compile.
--
-- Attempts to compile the supplied @moduleName@ in @testdata/${moduleName}.hs@ and
-- asserts that it both fails to compile and reports error messages that match
-- the contents of @testdata/${moduleName}.txt@.
--
-- You may be tempted to use @-fdefer-type-errors@ and/or the
-- @should-not-typecheck@ package; however, this doesn't work for custom type errors.
-- See: https://gitlab.haskell.org/ghc/ghc/-/issues/18310
@carymrobbins
carymrobbins / LazyError.hs
Created February 24, 2021 16:13
Using Monad to force errors
#!/usr/bin/env stack
{- stack
--resolver lts-16.8
--install-ghc runghc
-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE LambdaCase #-}
#!/usr/bin/env stack
{- stack
--resolver lts-16.8
--install-ghc runghc
-}
{-# OPTIONS_GHC -Wall #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}