On graph.hexdocs.pm pub opaque type Graph(direction, value, label) is public but opaque, meaning you can use it but not construct it directly, so that the package can ensure whenever you construct a Graph certain things are true.
Ok "certain things"? Well say you want to have
You could make two different types GraphDirected and GraphUndirected, but there might be functions you want both to share eg graph.size. You could add a direction field in the type itself, but then what if someone calls insert_undirected_edge when mygraph.direction is Directed? Kind of a meaningless/impossible situation, can only return error, so now the whole fn has to return Result(Graph, Err), and we handle errors at runtime, clearly bad. Two separate types might be ok, but of course because you're reading this post the graph type has a clever answer:
The graph also carries along its
direction(eitherDirectedorUndirected) in its type so that it’s impossible to mix upDirectedandUndirectedgraphs inadvertently.
That is, insert_directed_edge can only be called on graphs with type Directed:
pub fn insert_directed_edge(
graph: Graph(Directed, value, label),
labelled label: label,
from source: Int,
to destination: Int,
) -> Graph(Directed, value, label)
The type parameter direction is not actually used anywhere in the type itself, you can't print it or anything like you'd use a field. But as a type parameter, it prevents you from compiling code that mixes up directed/undirected.
So ok, here's some code, try doing gleam add graph and gleam run on this will work.
import graph.{Node}
pub fn main() {
let _mygraph1 =
graph.new()
|> graph.insert_node(Node(1, "A"))
|> graph.insert_node(Node(2, "B"))
|> graph.insert_node(Node(3, "C"))
|> graph.insert_directed_edge(labelled: "A -> B", from: 1, to: 2)
|> graph.insert_directed_edge(labelled: "B -> C", from: 2, to: 3)
let _mygraph2 =
graph.new()
|> graph.insert_node(Node(1, "A"))
|> graph.insert_node(Node(2, "B"))
|> graph.insert_node(Node(3, "C"))
|> graph.insert_undirected_edge(labelled: "A - B", between: 1, and: 2)
|> graph.insert_undirected_edge(labelled: "B - C", between: 2, and: 3)
let _mygraph3 =
graph.new()
|> graph.insert_node(Node(1, "A"))
|> graph.insert_node(Node(2, "B"))
|> graph.insert_node(Node(3, "C"))
|> graph.insert_undirected_edge(labelled: "A - B", between: 1, and: 2)
// |> graph.insert_directed_edge(labelled: "B -> C", from: 2, to: 3)
}
But if you uncomment the last line, the compiler will complain
Expected type:
graph.Graph(graph.Directed, a, String)
Found type:
graph.Graph(graph.Undirected, String, String)
which is cool - the compiler catches us adding undirected and directed edges to the same graph!
It turns out there's a name for "type parameter used just to restrict what fns you can call" - phantom types - for instance you have a Length(unit) type that has some fns that work on any unit, but also some fns that can only be called with Length(Inches).
Is this perhaps too neat? From grugbrain.dev:
generics especially dangerous here, grug try limit generics to container classes for most part where most value add
temptation generics very large is trick! spirit demon complex love this one trick! beware!
But perhaps Grug is just down on types...
danger abstraction too high, big brain type system code become astral projection of platonic generic turing model of computation into code base. grug confused and agree some level very elegant but also very hard do anything like record number of club inventory for Grug Inc. task at hand
Can't really prove types enforce correctness vs are useless/confusing; it's more an empirical question of whenever someone manages to actually ship something that works they score a point.