Skip to content

Instantly share code, notes, and snippets.

@rampion
Created May 18, 2017 02:25
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 rampion/e05fad464a0c5cd4916fc0c027bc8467 to your computer and use it in GitHub Desktop.
Save rampion/e05fad464a0c5cd4916fc0c027bc8467 to your computer and use it in GitHub Desktop.
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
module Fin where
data Nat = Z | S Nat
newtype Fin (n :: Nat) = Fin { unFin :: Int }
pattern FZ :: Fin ('S n)
pattern FZ = Fin 0
pattern FS :: Fin n -> Fin ('S n)
pattern FS f <- (finPred -> Just f)
where FS = finSucc
finPred :: Fin ('S n) -> Maybe (Fin n)
finPred (Fin 0) = Nothing
finPred (Fin i) = Just (Fin (pred i))
finSucc :: Fin n -> Fin ('S n)
finSucc (Fin i) = Fin (succ i)
x :: Int
x = case FZ of { FS (Fin i) -> i ; _ -> 1 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment