I’m putting together a Traveller implementation, and have begun by defining my data structures. I ran into a problem when trying to define a Ship.
I began with some simple data definitions.
data Ship = Ship Cargo Hull Weapons Engines
data Cargo = WholeMagilla
| MostOfIt
| HalfOfIt
| SomeOfIt
data Hull = Heavy
| AboveAverage
| Average
| Meh
data Weapons = WarMonger
| BadMofo
| CautiousCarl
| Pacifist
data Engines = WarpSuperFast
| WarpFairlyFast
| WarpFast
| Turtle
Now here’s my problem. I want to restrict what values a type can be, based on what the values of the other types are. Example : A possible Ship could be
Ship WholeMagilla Heavy Pacifist Turtle
Ship WholeMagilla Meh WarMonger Turtle
Ship WholeMagilla Meh Pacifist WarpSuperFast
So, if a Player has enough Credits they can have two types of maximum value, at best, at the cost of minimizing the rest. Then, there are all the possibilities in between. I started visualizing a graph whose paths are determined by what nodes are already in that path. This helps me think about the problem, but not in a way that is going to help me write a function that will get the result I want. Could someone point me in the right direction?
You could associate “credits” to your objects and use a smart constructor. Then, wrap everything in a module and export only the smart constructor, not the
Shipconstructor, so users will not mistakenly use it.Here is the code (I’ve stripped down your constructors, for simplicity):