Skip to content

Instantly share code, notes, and snippets.

@scarf005
Last active August 3, 2023 06:23
Show Gist options
  • Save scarf005/63ecd67ee4ed3fe18a42ded29e857a22 to your computer and use it in GitHub Desktop.
Save scarf005/63ecd67ee4ed3fe18a42ded29e857a22 to your computer and use it in GitHub Desktop.

Visualizing typeclasses with mermaid diagram

id : a -> a

flowchart LR

a((a)) --> b((a))

classDef default stroke:#333,stroke-width:2px
classDef white fill:white
class a,b,c white
Loading

Num a

add : a -> a -> a

flowchart LR

a((a)) --> b((a)) --> c((a))

classDef default stroke:#333,stroke-width:2px
classDef white fill:white
class a,b,c white
Loading

Functor f

map : (a -> b) -> f a -> f b

flowchart LR

subgraph A[" "]
  direction LR
  a((a)) --> b((b))
end
subgraph B[f]
  fa((a))
end
subgraph C[f]
  fb((b))
end

A --> B --> C

classDef default stroke:#333,stroke-width:2px
classDef white fill:white
classDef black fill:black,color:white
classDef orange fill:LightSalmon,stroke-width:2px,stroke:#333

class a,fa white
class b,fb black
class B,C orange
Loading

Applicative f

pure : a -> f a

flowchart LR

subgraph FA[f]
  fa((a))
end

a((a)) --> FA

classDef default stroke:#333,stroke-width:2px
classDef white fill:white
classDef orange fill:LightSalmon,stroke-width:2px,stroke:#333

class a,fa white
class FA orange
Loading

<*> : f (a -> b) -> f a -> f b

flowchart LR

subgraph A[f]
  direction LR
  a((a)) --> b((b))
end
subgraph B[f]
  fa((a))
end
subgraph C[f]
  fb((b))
end

A --> B --> C

classDef default stroke:#333,stroke-width:2px
classDef white fill:white
classDef black fill:black,color:white
classDef orange fill:LightSalmon,stroke-width:2px,stroke:#333

class a,fa white
class b,fb black
class A,B,C orange
Loading

Monad m

>>= : m a -> (a -> m b) -> m b

flowchart LR

subgraph A[m]
  ma((a))
end
subgraph B[" "]
  direction LR

  subgraph MB[m]
    direction LR
    mb1((b))
  end

  a((a)) --> MB
end
subgraph C[m]
  mb2((b))
end

A --> B --> C

classDef default stroke:#333,stroke-width:2px
classDef white fill:white
classDef black fill:black,color:white
classDef orange fill:LightSalmon,stroke-width:2px,stroke:#333

class a,ma white
class b,mb1,mb2 black
class A,MB,C orange
Loading

join : m (m a) -> (m a)

flowchart LR

subgraph A[m]
  subgraph MA[m]
    direction LR
    mma((a))
  end
end
subgraph B[m]
  ma((a))
end

A --> B

classDef default stroke:#333,stroke-width:2px
classDef white fill:white
classDef orange fill:LightSalmon,stroke-width:2px,stroke:#333

class mma,ma white
class A,MA,B orange
Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment