Skip to content

Instantly share code, notes, and snippets.

@cartazio
Forked from goldfirere/Shape-7.6.hs
Created October 9, 2013 19:47
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 cartazio/6907168 to your computer and use it in GitHub Desktop.
Save cartazio/6907168 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DataKinds, PolyKinds, GADTs, TypeFamilies, TypeOperators,
ConstraintKinds, ScopedTypeVariables, RankNTypes #-}
module Shape where
import GHC.Exts
import Data.Proxy
import Data.Type.Equality
data Nat = Zero | Succ Nat
type family n1 + n2 where
Zero + n2 = n2
(Succ n1') + n2 = Succ (n1' + n2)
-- singleton for Nat
data SNat :: Nat -> * where
SZero :: SNat Zero
SSucc :: SNat n -> SNat (Succ n)
gcoerce :: (a :=: b) -> ((a ~ b) => r) -> r
gcoerce Refl x = x
-- inductive proof of right-identity of +
plus_id_r :: SNat n -> ((n + Zero) :=: n)
plus_id_r SZero = Refl
plus_id_r (SSucc n) = gcoerce (plus_id_r n) Refl
-- inductive proof of simplification on the rhs of +
plus_succ_r :: SNat n1 -> Proxy n2 -> ((n1 + (Succ n2)) :=: (Succ (n1 + n2)))
plus_succ_r SZero _ = Refl
plus_succ_r (SSucc n1) proxy_n2 = gcoerce (plus_succ_r n1 proxy_n2) Refl
data Shape :: Nat -> * where
Nil :: Shape Zero
(:*) :: Int -> Shape n -> Shape (Succ n)
reverseShape :: Shape n -> Shape n
reverseShape Nil = Nil
reverseShape list = go SZero Nil list
where
go :: SNat n1 -> Shape n1 -> Shape n2 -> Shape (n1 + n2)
go snat acc Nil = gcoerce (plus_id_r snat) acc
go snat acc (h :* (t :: Shape n3)) =
gcoerce (plus_succ_r snat (Proxy :: Proxy n3))
(go (SSucc snat) (h :* acc) t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment