| 1 | {-# LANGUAGE GADTs, EmptyDataDecls, RankNTypes #-} |
|---|
| 2 | -- {-# OPTIONS_GHC -F -pgmF she #-} |
|---|
| 3 | data Z |
|---|
| 4 | data S n |
|---|
| 5 | |
|---|
| 6 | -- only works on ghc6 but not on ghc7 |
|---|
| 7 | type Const a b = a |
|---|
| 8 | |
|---|
| 9 | {- |
|---|
| 10 | data List a n where |
|---|
| 11 | Nil :: List a Z |
|---|
| 12 | Cons :: a -> List a n -> List a (S n) |
|---|
| 13 | -} |
|---|
| 14 | |
|---|
| 15 | data L a l n where |
|---|
| 16 | N :: L a l Z |
|---|
| 17 | C :: a -> l n -> L a l (S n) |
|---|
| 18 | |
|---|
| 19 | newtype Fix f n = In { out :: f (Fix f) n } |
|---|
| 20 | |
|---|
| 21 | mcata :: (forall x a . (forall a' . x a' -> Const b a') -> f x a -> Const b a) |
|---|
| 22 | -> Fix f a -> Const b a |
|---|
| 23 | mcata f = f (mcata f) . out |
|---|
| 24 | |
|---|
| 25 | -- lengthList :: Fix (L e) n -> Int |
|---|
| 26 | lengthList = mcata phi |
|---|
| 27 | where |
|---|
| 28 | phi :: (forall n . x n -> Int) -> L e x n -> Int |
|---|
| 29 | phi len N = 0 |
|---|
| 30 | phi len (C x xs) = 1 + len xs |
|---|