Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting cojoin or cobind for n-dimensional grid type
    primarykey
    data
    text
    <p>Using the typical definition of type-level naturals, I've defined an n-dimensional grid.</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 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) instance Functor (U n) where fmap f (Point x) = Point (f x) fmap f d@Dimension{} = dmap (fmap f) d </code></pre> <p>Now I want to make it an instance of Comonad, but I can't quite wrap my brain around it.</p> <pre><code>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 :: U Z x -&gt; U Z (U Z x) cojoin (Point x) = Point (Point x) -- cojoin ::U (S n) x -&gt; U (S n) (U (S n) x) cojoin d@Dimension{} = undefined -- =&gt;&gt; :: U Z x -&gt; (U Z x -&gt; r) -&gt; U Z r p@Point{} =&gt;&gt; f = Point (f p) -- =&gt;&gt; :: U (S n) x -&gt; (U (S n) x -&gt; r) -&gt; U (S n) r d@Dimension{} =&gt;&gt; f = undefined </code></pre> <p>Using <code>cojoin</code> on an n-dimensional grid will produce an n-dimensional grid of n-dimensional grids. I'd like to provide an instance with the same idea as <a href="http://hpaste.org/76465" rel="noreferrer">this one</a>, which is that the <em>value</em> of the <em>cojoined</em> grid at (x,y,z) should be the <em>original</em> grid <em>focused</em> on (x,y,z). To adapt that code, it appears that we need to reify <code>n</code> in order to perform <code>n</code> "fmaps" and <code>n</code> "rolls". You don't have to do it that way but if that helps, then there you go.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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