Skip to content

Instantly share code, notes, and snippets.

Finally Free

{-# LANGUAGE RankNTypes, DeriveFunctor, TypeOperators, KindSignatures, GADTs, DataKinds, MultiParamTypeClasses, FlexibleContexts, FlexibleInstances #-}
import Control.Applicative
import Data.Semigroup(Semigroup(..))

If you've programmed in Haskell you may have come across free construcions: free monoids, free monads, free applicatives and so on. But what does it actually mean for an object to be "free"?

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXLTMU2kl3OjpNyFZaP0ozfxVVDcmWSbgHD+K/76GA7qyh9CWo7EIgUjUGCcxRSXmKYwEZMiWX3ESXAIn9mQic4IKfj5UOvauWx2xLFr3Ztae54mgUC4CDiOoWzW/gMHRdTkgSGJGztdBNqNc9Ykwmql8IwxnUoKR0QftGOZz9P8pq3C/xf9xpaZBCAfq9IPhk8JGfKTa4Ur8/9Kg+PGhk8cyTt+ni56zUblwmDpTTZggH5eF6gulmiNquYh7maYqbcC2YFQujRfNZOetVfemE6rIN5nCxBdSDoJGQDafogNN63i0zxPEONh6MlPDd67S3jHfb/t6PC8RrVSpu9rLd root@nixos
data Bar (xs :: [k]) where
N :: Bar '[]
C :: Bar (x : xs)
class Foo xs where
foo :: Bar xs
Some instances for base:
```haskell
-- MonoidFromApplicative
-- Example: undoing the Const applicative
-- Takes [] to naturals with (+)
newtype MFA f = MFA (f ())
-- or?
newtype MFA f = MFA (forall x . f x)
instance Applicative f => Monoid (MFA f)
- Connection: TCP
- Framing/Call-Resp: HTTP1.1/2
- Encryption: TLS
- Auth
- Server: TLS certificate
- Client: JWT
- Serialization: JSON?
- Method name: Header/JSON frame
- Errors: Header/JSON frame
@hanshoglund
hanshoglund / fmap.hs
Last active April 29, 2019 23:11
fmap
> let f = fmap > :t (f f f f) (f f) (f f) (f f f f)
> let f = flip > :t (f f f f) (f f) (f f) (f f f f)
> let f = (.) > :t (f f f f) (f f) (f f) (f f f f)
@hanshoglund
hanshoglund / ex.md
Last active September 3, 2018 14:21

See also https://en.wikibooks.org/wiki/Haskell

HS excercises

Basics

  • GHC CLI & Repl. Stack. Standard (strict) compiler flags to use.
    1. Create a reproducible shell environment containing the packages network and warp using Stack (LTS, using astack.yaml file) or Nix (HEAD, using ghcWithPackages).
    2. Start a REPL session (using ghci or stack repl) and import Warp in the REPL session.
    3. Write and compile a Hello World program using ghc Hello.hs in the above environment
  1. Import Warp and write a web server printing Hello, World! on localhost:SOMEPORT
{-
> :t PrimInt
PrimInt :: Schema Integer
> :t list PrimInt
list PrimInt
:: Schema
(Fix
(("Nil" ~~~ Const (Rec '[]))
{-# LANGUAGE
FunctionalDependencies,
RankNTypes,
NoMonomorphismRestriction,
TypeOperators,
TypeFamilies,
TypeFamilyDependencies,
TypeApplications,
InstanceSigs,
PolyKinds,
Prelude> let k = const
Prelude> let f = flip
Prelude> k f k k k k f k k k k f k k k $ 2
?
---
Prelude> :t k
k :: a -> b -> a
Prelude> :t k k