Skip to content

Instantly share code, notes, and snippets.

View monadplus's full-sized avatar

Arnau Abella monadplus

View GitHub Profile
@monadplus
monadplus / evaluate.hs
Last active February 3, 2020 22:20
Control.Exception.evaluate
{-# LANGUAGE ScopedTypeVariables #-}
-- pure/return vs evaluate on try
data UserError = UserError String
deriving Show
instance Exception UserError
>>> (try . pure . throw $ UserError "die") >>= either (\(e :: SomeException) -> putStrLn "exception captured") (\a -> seq a (putStrLn "no exception"))
*** Exception: foo
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
@monadplus
monadplus / Foldl.hs
Created January 27, 2020 22:17
foldl: Composable, streaming, and efficient left folds (https://hackage.haskell.org/package/foldl)
module Foldl where
import qualified Control.Foldl as L
import Control.Applicative
import Data.Functor
import Data.Monoid
import Control.Monad.State
import Data.Functor.Identity
-- More on https://hackage.haskell.org/package/foldl
@monadplus
monadplus / incoherent.hs
Created January 28, 2020 13:42
Incoherent: use it with care
class Foo a where
foo :: a -> Int
instance Foo Char where
foo _ = 2
instance {-# INCOHERENT #-} Foo a where
foo _ = 3
incoherent :: Char -> Bool
@monadplus
monadplus / expr.hs
Last active January 29, 2020 21:39
Expression Language
{-# LANGUAGE GADTs #-}
module Expr where
data Expr' a where
Equals' :: Expr' Int -> Expr' Int -> Expr' Bool
Add' :: Expr' Int -> Expr' Int -> Expr' Int
If' :: Expr' Bool -> Expr' a -> Expr' a -> Expr' a
IntValue' :: Int -> Expr' Int
BoolValue' :: Bool -> Expr' Bool
Con' :: a -> Expr' a
-- | a. Fix that for me - write a list that allows me to hold any functions as
-- long as the input of one lines up with the output of the next.
data TypeAlignedList a b where
TNil :: TypeAlignedList a a
TCons :: (a -> r) -> TypeAlignedList r b -> TypeAlignedList a b
-- >>> f = TCons (+2) (TCons show TNil); f :: TypeAlignedList Int String
-- | b. Which types are existential?
-- r
-- | a. Fix that for me - write a list that allows me to hold any functions as
-- long as the input of one lines up with the output of the next.
data TypeAlignedList a b where
TNil :: TypeAlignedList a a
TCons :: (a -> r) -> TypeAlignedList r b -> TypeAlignedList a b
-- >>> f = TCons (+2) (TCons show TNil); f :: TypeAlignedList Int String
-- | b. Which types are existential?
-- r
@monadplus
monadplus / StringAndIntList.hs
Created January 30, 2020 18:55
StringAndIntList
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE PolyKinds #-}
module StringAndIntList where
@monadplus
monadplus / safe-index.hs
Created January 30, 2020 21:58
TODO improve this solution
data Vector (n :: Nat) (a :: Type) where
VNil :: Vector 'Z a
VCons :: a -> Vector n a -> Vector ('S n) a
data SmallerThan (limit :: Nat) where
SmallerThanZ :: SmallerThan ('S 'Z)
SmallerThanN :: SmallerThan n -> SmallerThan ('S n)
(!!!) :: Vector n a -> SmallerThan n -> a
(!!!) (VCons a _) SmallerThanZ = a
@monadplus
monadplus / html_render.hs
Created January 31, 2020 08:53
Simple HTML render
class Renderable component where
render :: component -> String
{-# MINIMAL render #-}
data Text = Text String
instance Renderable Text where
render (Text s) = "<p>" <> s <> "</p>"
data RenderableX where