Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So this turns out to be wrong. I'll leave it here in case anybody wants to try to fix it.</p> <p>This implementation is the way @pigworker suggested I think. It compiles, but I haven't tested it. (I took the <code>cojoin1</code> implementation from <a href="http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html" rel="nofollow">http://blog.sigfpe.com/2006/12/evaluating-cellular-automata-is.html</a>)</p> <pre><code>{-# LANGUAGE KindSignatures #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE TypeFamilies #-} data Nat = Z | S Nat data U (n :: Nat) x where Point :: x -&gt; U Z x Dimension :: [U n x] -&gt; U n x -&gt; [U n x] -&gt; U (S n) x unPoint :: U Z x -&gt; x unPoint (Point x) = x dmap :: (U n x -&gt; U m r) -&gt; U (S n) x -&gt; U (S m) r dmap f (Dimension ls mid rs) = Dimension (map f ls) (f mid) (map f rs) right, left :: U (S n) x -&gt; U (S n) x right (Dimension a b (c:cs)) = Dimension (b:a) c cs left (Dimension (a:as) b c) = Dimension as a (b:c) instance Functor (U n) where fmap f (Point x) = Point (f x) fmap f d@Dimension{} = dmap (fmap f) d class Functor w =&gt; Comonad w where (=&gt;&gt;) :: w a -&gt; (w a -&gt; b) -&gt; w b coreturn :: w a -&gt; a cojoin :: w a -&gt; w (w a) x =&gt;&gt; f = fmap f (cojoin x) cojoin xx = xx =&gt;&gt; id instance Comonad (U n) where coreturn (Point x) = x coreturn (Dimension _ mid _) = coreturn mid cojoin (Point x) = Point (Point x) cojoin d@Dimension{} = fmap unlayer . unlayer . fmap dist . cojoin1 . fmap cojoin . layer $ d dist :: U (S Z) (U n x) -&gt; U n (U (S Z) x) dist = layerUnder . unlayer layerUnder :: U (S n) x -&gt; U n (U (S Z) x) layerUnder d@(Dimension _ Point{} _) = Point d layerUnder d@(Dimension _ Dimension{} _) = dmap layerUnder d unlayer :: U (S Z) (U n x) -&gt; U (S n) x unlayer = dmap unPoint layer :: U (S n) x -&gt; U (S Z) (U n x) layer = dmap Point cojoin1 :: U (S Z) x -&gt; U (S Z) (U (S Z) x) cojoin1 a = layer $ Dimension (tail $ iterate left a) a (tail $ iterate right a) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload