Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8713543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:22:03+00:00 2026-06-13T05:22:03+00:00

First, I started with some typical type-level natural number stuff. {-# LANGUAGE KindSignatures #-}

  • 0

First, I started with some typical type-level natural number stuff.

{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}

data Nat = Z | S Nat

type family Plus (n :: Nat) (m :: Nat) :: Nat
type instance Plus Z m = m
type instance Plus (S n) m = S (Plus n m)

So I wanted to create a data type representing an n-dimensional grid. (A generalization of what is found at Evaluating cellular automata is comonadic.)

data U (n :: Nat) x where
  Point     :: x                           -> U Z     x
  Dimension :: [U n x] -> U n x -> [U n x] -> U (S n) x

The idea is that the type U num x is the type of a num-dimensional grid of xs, which is “focused” on a particular point in the grid.

So I wanted to make this a comonad, and I noticed that there’s this potentially useful function I can make:

ufold :: (x -> U m r) -> U n x -> U (Plus n m) r
ufold f (Point x) = f x
ufold f (Dimension ls mid rs) =
  Dimension (map (ufold f) ls) (ufold f mid) (map (ufold f) rs)

We can now implement a “dimension join” that turns an n-dimensional grid of m-dimensional grids into an (n+m)-dimensional grid, in terms of this combinator. This will come in handy when dealing with the result of cojoin which will produce grids of grids.

dimJoin :: U n (U m x) -> U (Plus n m) x
dimJoin = ufold id

So far so good. I also noticed that the Functor instance can be written in terms of ufold.

instance Functor (U n) where
  fmap f = ufold (\x -> Point (f x))

However, this results in a type error.

Couldn't match type `n' with `Plus n 'Z'

But if we whip up some copy pasta, then the type error goes away.

instance Functor (U n) where
  fmap f (Point x) = Point (f x)
  fmap f (Dimension ls mid rs) =
    Dimension (map (fmap f) ls) (fmap f mid) (map (fmap f) rs)

Well I hate the taste of copy pasta, so my question is this. How can I tell the type system that Plus n Z is equal to n? And the catch is this: you can’t make a change to the type family instances that would cause dimJoin to produce a similar type error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:22:04+00:00Added an answer on June 13, 2026 at 5:22 am

    What you need is a nice propositional equality type:

    {-# LANGUAGE KindSignatures #-}
    {-# LANGUAGE DataKinds #-}
    {-# LANGUAGE PolyKinds #-}
    {-# LANGUAGE GADTs #-}
    {-# LANGUAGE TypeFamilies #-}
    {-# LANGUAGE TypeOperators #-}
    
    data Nat = Z | S Nat
    
    type family Plus (n :: Nat) (m :: Nat) :: Nat
    type instance Plus Z m = m
    type instance Plus (S n) m = S (Plus n m)
    
    data (:=) :: k -> k -> * where
      Refl :: a := a
    
    data Natural (n :: Nat) where
      Zero :: Natural Z
      Suc  :: Natural n -> Natural (S n)
    
    plusZero :: Natural n -> n := (n `Plus` Z)
    plusZero Zero = Refl
    plusZero (Suc n) | Refl <- plusZero n = Refl
    

    This allows you to prove arbitrary things about your types and bring that knowledge into scope locally by pattern matching on the Refl.

    One annoying thing is that my plusZero proof requires induction over the natural in question, which you won’t be able to do by default (since it doesn’t exist at runtime). A typeclass for generating Natural witnesses would be easy, though.

    Another option for your particular case might be just to invert the arguments to plus in your type definition so that you get the Z on the left and it reduces automagically. It’s often a good first step to make sure your types are as simple as you can make them, but then you’ll often need propositional equality for more complicated things, regardless.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started my first MVC 2.0 .net application. And I set up some default
I first started C++ with Microsoft VC++ in VS2010. I recently found some work,
When i first started using monotouch i found a page with some code samples
Hi noticed some code in our application when I first started Java programming. I
When i first started learning Rails, erb seemed very natural for me, because i've
When I first started programming I always followed the convention of defining global variables
When I first started with Javascript, I usually just put whatever I needed into
When I first started looking at Scala, I liked the look of for-comprehensions. They
When I first started reading about and learning ruby, I read something about the
When I first started to program in C# last year, I immediately looked for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.