Last active
August 29, 2015 14:07
-
-
Save mrb/1b064e600d0328a72004 to your computer and use it in GitHub Desktop.
This file contains 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 Graph | |
%default total | |
Node : Type | |
Node = Nat | |
Adj : Type -> Type | |
Adj a = List (a, Node) | |
Context : Type -> Type -> Type | |
Context a b = (Adj b, Node, a, Adj b) | |
data Graph a b = Empty | | |
(&) (Context a b) (Graph a b) | |
infixr 10 & -- So we can do `context & graph` to construct graphs | |
isEmpty : Graph a b -> Bool | |
isEmpty Empty = True | |
isEmpty _ = False | |
gmap : (Context a b -> Context a b) -> Graph a b -> Graph a b | |
gmap fn Empty = Empty | |
gmap fn (co & gr) = (fn co) & (gmap fn gr) | |
grev : Graph a b -> Graph a b | |
grev = gmap swap where swap (p, v, l, s) = (s, v, l, p) | |
n1 : Node | |
n1 = 1 | |
n2 : Node | |
n2 = 2 | |
n3 : Node | |
n3 = 3 | |
left : Adj String | |
left = [("left", 2)] | |
up : Adj String | |
up = [("up", 3)] | |
right : Adj String | |
right = [("right", 2)] | |
down : Adj String | |
down = [("down", 3)] | |
g : Graph String String | |
g = (left ++ up, n1, "a", right) & | |
([], n2, "b", down) & | |
([], n3, "c", []) & Empty | |
Author
mrb
commented
Oct 14, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment