Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize scenario datatypes #903

Merged
merged 1 commit into from
Dec 20, 2022
Merged

Parameterize scenario datatypes #903

merged 1 commit into from
Dec 20, 2022

Conversation

kostmo
Copy link
Member

@kostmo kostmo commented Dec 12, 2022

Towards #873

Motivation

The Entity datatype has a fair amount of state and extra fields that are not relevant to serializing a scenario (in particular, a world map) to a file.

In this PR, the Cell record is renamed to PCell (representing "Parameterized Cell"), replacing Entity with a type parameter. This permits the introduction of a trimmed-down Entity type for the purpose of serialization. This lightweight type can also be a reliable Map key.

A type alias Cell is created with the original Entity type as a parameter.

The WorldDescription and WorldPalette are likewise parameterized on the Cell type.

@kostmo kostmo marked this pull request as ready for review December 12, 2022 22:45
@kostmo kostmo requested a review from byorgey December 12, 2022 22:45
src/Swarm/Game/Scenario/WorldDescription.hs Outdated Show resolved Hide resolved
{ cellTerrain :: TerrainType
, cellEntity :: Maybe Entity
, cellEntity :: Maybe e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the idea is that instead of writing something like:

worldMapToText :: WorldPalette -> [[Cell]] -> Text

we would instead do it in two steps:

simplifyEntityForMe :: Entity -> SimplerEntity
simpleWorldMapToText :: WorldPalette (PCell SimplerEntity) -> Text

Is this because you want to have ToJSON (not ToJSONE) instance? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kostmo if this is about ToJSON we discussed with @byorgey, that you could simply use a custom tuple:

newtype Env e v = Env (e, v)
instance ToJSON (Env E Val) -- maybe we could codify this pattern as ToJSONE => ToJSON (Env, Val)

which is the same as

simplify :: E -> Val -> SimpleVal
instance ToJSON SimpleVal

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because you want to have ToJSON (not ToJSONE) instance?

I haven't quite wrapped my head around how FromJSONE works (or how ToJSONE would).

For the purpose of the World Editor, we just need to emit a fragment of the scenario file (either to a file or via the web API ).

I'm not sure how easy it is to read in its current state, but here's what I'm trying to do in the World Editor PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reading through your PR, I think it makes sense to keep it simple. 👍

The idea with FromJSONE is that you need the environment to choose between parsing alternatives.

But with ToJSON no tricks are necessary, so I was incorrect in thinking that we would ever need it.

@@ -144,6 +144,7 @@ import Swarm.Game.Recipe (
reqRecipeMap,
)
import Swarm.Game.Robot
import Swarm.Game.Scenario.WorldDescription
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this new import?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I would have expected a compiler warning about an unused import. Anyway, I've removed it and it still works.

@@ -52,7 +56,7 @@ instance FromJSONE (EntityMap, RobotMap) WorldDescription where
-- string into a nested list of 'Cell' values by looking up each
-- character in the palette, failing if any character in the raw map
-- is not contained in the palette.
paintMap :: MonadFail m => WorldPalette -> Text -> m [[Cell]]
paintMap :: MonadFail m => WorldPalette Cell -> Text -> m [[Cell]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this polymorphic, WorldPalette c -> Text -> m [[c]] or something like that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

newtype WorldPalette = WorldPalette
{unPalette :: KeyMap Cell}
newtype WorldPalette c = WorldPalette
{unPalette :: KeyMap c}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to have newtype WorldPalette e = WorldPalette { unPalette :: KeyMap (PCell e) }? I am honestly not sure. Or maybe we should just double down on the generality and call it Palette instead of WorldPalette --- it maps characters to anything.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@kostmo kostmo added the merge me Trigger the merge process of the Pull request. label Dec 20, 2022
@mergify mergify bot merged commit aa6b9f2 into main Dec 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge me Trigger the merge process of the Pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants