Last active
March 18, 2024 17:13
-
-
Save sjoerdvisscher/6570858 to your computer and use it in GitHub Desktop.
Skew binary tree with the invariants enforced by the type system, using a nested data type.
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
{-# LANGUAGE GADTs #-} | |
type Id = Int | |
data Path a where | |
P :: P a -> Path a | |
Two :: b -> b -> P (Br b) -> (P (Br b) -> P a) -> Path a | |
data Br a = Br a Id a | |
data P a = Nil | Zero (P (Br a)) | One a (P (Br a)) | |
cons :: Id -> Path Id -> Path Id | |
cons x (P p) = consP x p | |
cons x (Two y z zs f) = case consP (Br y x z) zs of | |
P p -> P (f p) | |
Two y' z' zs' f' -> Two y' z' zs' (f . f') | |
consP :: a -> P a -> Path a | |
consP x Nil = P $ One x Nil | |
consP x (Zero xs) = P $ One x xs | |
consP x (One y xs) = Two x y xs Zero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment