nhamblet / golf

Conway's Game of Life (Functional-y)
0 stars 0 forks source link

Can you have a comonad for a non-wrapping grid? #2

Open nhamblet opened 4 years ago

nhamblet commented 4 years ago

With PointedGrid[T](pt: (Int, Int), grid: Vector[Vector[Int]]), we have an extract method which uses modular arithmetic to wrap the grid, so that you never access values outside the grid. How do you set up a comonad for a non-wrapping grid? How do you have to change PointedGrid?

One could imagine a world (e.g., if the T type were a monoid) where if the grid index were outside the bounds of the grid, you returned some default value (e.g., the Monoid[T].empty). This makes the PointedGrid actually only a Functor of the sub-category of types which are Monoids, which breaks the rules.

It seems like you might want to try to get past that by not using (Int, Int) as the pointer type in PointedGrid. I guess with dependent types you could encode the appropriate int ranges for the indices. Actually, I'm not really sure what other options there are.

nhamblet commented 4 years ago

Ah, I guess for the problem at hand (Game of Life) you would change the definition of neighbors to do that checking for you. Then you let extract assume that all points are valid, and don't wrap.

nhamblet commented 4 years ago

The version where neighbors does the wrapping or not wrapping for you is here