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 8531587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:31:11+00:00 2026-06-11T09:31:11+00:00

I’m toying around with zippers, where the current element can have a different type

  • 0

I’m toying around with zippers, where the current element can have a different type than the left and right lists:

data Z a b = Z ([a], b, [a])

One can navigate the zipper with moveLeft and moveRight:

moveLeft  :: (b -> a -> a) -> (a -> b -> b) -> Z a b -> Z a b
moveLeft listF currentF (Z (x:xs, c, ys)) = Z (xs, g x c, ((f c x):ys))

moveRight :: (b -> a -> a) -> (a -> b -> b) -> Z a b -> Z a b
moveRight listF currentF (Z (xs, c, y:ys)) = Z (((f c y):xs), g y c, ys)

Here, listF transforms a list element and the current one into a list element to the left or right. currentF transforms the current element and a list element into the current element.

If both the current and the list type are equal, moving is simple:

moveLeftSameType :: Z a a -> Z a a
moveLeftSameType = moveLeft const const

and everything works as expected, nice!

What I’d like to do now is to generalize the above idea, such that by only implementing the listF and currentF functions for a given Z a b (say a :: Char, b :: Int), moveLeft and moveRight do the right thing automagically. What would be the correct way to do this?

Remark

I have tried to implement a class like this:

class CPos a where
    listF :: c -> d -> d
    currentF :: d -> c -> c
    moveLeft :: a -> a
    moveRight :: a -> a

where moveLeft/Right are implemented in terms of listF and currentF, but this fails with

The class method `listF' mentions none of the type variables of the class CPos a

Remark 2

Something I don’t like about the above idea in general is the fact that be allowing arbitrary functions listF and currentF it’s impossible to guarantee

moveLeft . moveRight = id

(if the zipper is within the list, on the borders this doesn’t hold anyways). Any hints to enforce this?

  • 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-11T09:31:13+00:00Added an answer on June 11, 2026 at 9:31 am

    Here is a solution to that.

    class CPos a b where
        listF :: b -> a -> a
        currentF :: a -> b -> b
        moveLeft :: Z a b -> Z a b
        moveLeft (Z (x:xs, c, ys)) = Z (xs, currentF x c, ((listF c x):ys))
        moveRight :: Z a b -> Z a b
        moveRight (Z (xs, c, y:ys)) = Z (((listF c y):xs), currentF y c, ys)
    

    I don’t think you can enforce moveLeft . moveRight = id. I mean it is undecidable to guarantee two functions are equal. The best you can do is to write quickcheck testcases to guarantee the same.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.