Skip to content

Instantly share code, notes, and snippets.

@andrewthad
Created April 7, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewthad/637326ffd95b29a08e9bd2dffa0d1cf6 to your computer and use it in GitHub Desktop.
Save andrewthad/637326ffd95b29a08e9bd2dffa0d1cf6 to your computer and use it in GitHub Desktop.
Finger trees without polymorphic recursion
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
module FingerTree where
import Data.Kind (Type)
data Nat = Z | S Nat
data Digit n a
= One (Tree n a)
| Two (Tree n a) (Tree n a)
| Three (Tree n a) (Tree n a) (Tree n a)
| Four (Tree n a) (Tree n a) (Tree n a) (Tree n a)
data Node n a
= Node2 (Tree n a) (Tree n a)
| Node3 (Tree n a) (Tree n a) (Tree n a)
-- Two-three tree with values at leaves.
data Tree :: Nat -> Type -> Type where
TreeZero :: a -> Tree 'Z a
TreeSucc :: Node n a -> Tree ('S n) a
data FingerTreeN :: Nat -> Type -> Type where
Empty :: FingerTreeN n a
Single :: Tree n a -> FingerTreeN n a
Deep :: Digit n a -> FingerTreeN ('S n) a -> Digit n a -> FingerTreeN n a
newtype FingerTree a = FingerTree (FingerTreeN 'Z a)
module FingerTree where
data Digit a
= One a
| Two a a
| Three a a a
| Four a a a a
data Node a = Node2 a a | Node3 a a a
data FingerTree a
= Empty
| Single a
| Deep (Digit a) (FingerTree (Node a)) (Digit a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment