Created
July 12, 2022 05:00
-
-
Save emarzion/1feeb63d2f63cfb5437e57afdb0f46b0 to your computer and use it in GitHub Desktop.
walking a graph
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
Require Import List. | |
Import ListNotations. | |
CoInductive Graph (X : Type) : Type := | |
| node : X -> list (Graph X) -> Graph X. | |
Arguments node {_}. | |
Fixpoint walks {X} (n : nat) (g : Graph X) | |
{struct n} : list (list X) := | |
match g with | |
| node x gs => | |
match n with | |
| 0 => [[x]] | |
| S m => map (cons x) (concat (map (walks m) gs)) | |
end | |
end. | |
(* 1 <-> 2 <-> 3 | |
^ ^ | |
| / | |
v / | |
4< | |
*) | |
CoFixpoint example : Graph nat := | |
node 1 [two] | |
with | |
two := node 2 [example; three; four] | |
with | |
three := node 3 [two; four] | |
with | |
four := node 4 [two; three]. | |
Compute walks 4 example. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment