This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE AllowAmbiguousTypes #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE FlexibleContexts #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE ScopedTypeVariables #-} | |
{-# LANGUAGE StandaloneDeriving #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
{-# LANGUAGE TypeApplications #-} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo a where | |
foo :: a -> Int | |
instance Foo Char where | |
foo _ = 2 | |
instance {-# INCOHERENT #-} Foo a where | |
foo _ = 3 | |
incoherent :: Char -> Bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE GADTs #-} | |
{-# LANGUAGE KindSignatures #-} | |
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE RankNTypes #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE TypeOperators #-} | |
{-# LANGUAGE PolyKinds #-} | |
module StringAndIntList where |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |