mapping a concept to a type

Hi everybody, I'm still working on implementing a nomic game in Haskell. Although the game is pretty advanced, I'm still confused by one fundamental question: A nomic game is composed of rules. A Rule is a sort of little program submitted by the player during the game. They come in two fashions: - a Normal rule, when executed, can change the state of the Game. - a Meta rule, when executed on another rule, can assess the legality of that rule and change the state of the Game. In those definitions, the Game can be seen as a data structure containing all the current state of the game. Sorry if it's a bit abstract, but as it's fairly complex, I'd prefer to keep it like this for the moment. Thus, I found that those definitions can be translated to two possible type definitions: type NormalRule = State Game () type MetaRule = Rule -> State Game Bool data Rule = MR MetaRule | NR NormalRule *** or *** newtype Rule = Rule {rule :: Maybe Rule -> (State Game Bool)} All rules submitted by the user must be of the type Rule. It is thus important that it is very clean. Which type do you prefer? I find both heavy and redundant. The first forces me to specify if I want an argument of not (with the constructors MR and NR), the second forces me to pass the Nothing argument in the case of a Normal rule, and to return a dummy value (for ex. return True). Do you know of a construction/abstraction that allows having or not an argument (a variable number of arguments, here zero or one)? Thanks! Corentin

I find both heavy and redundant. The first forces me to specify if I want an argument of not (with the constructors MR and NR)
I'm sorry, I don't understand what you mean here.
Do you know of a construction/abstraction that allows having or not an argument (a variable number of arguments, here zero or one)?
Either way you do it (Maybe, or building the variant yourself), you're going to need a constructor. This is a feature, not a bug. Edward

I wonder if you want a typeclass here, rather than a type? A Normal Rule is pretty much a State Transformer, while a Meta Rule seems like a higher-order function on Normal Rules[*]. These are different kinds of things --- and I say "kind" advisedly --- so perhaps better to define the specific commonalities you need than to try to shoehorn them both into one type. [*]: Possibly related question: Can a Meta Rule depend upon an implementation detail of a Normal rule? In other words, does rule1 g == rule2 g imply myMetaRule rule1 == myMetaRule rule2 ?

Yes I totally agree, they have different kind. A Normal Rule is *
whereas a Meta Rule is * -> *.
But I have no experience with typeclasses. That could be what I'm looking for!
What they have in common? Well, Id' say that a rule (whatever sort it is) can:
- change the state of the game when executed - hence the State Transformer
- perhaps assess the legality of *something* (including the legality
of another rule)
So this gives:
class (Monad r) => (Rule r) where
changeState :: StateT Game r ()
legal :: l -> r Bool
legal _ = return True
Does that sound good? I will continue experiment with it tomorrow.
My other comment inline:
On Sat, May 19, 2012 at 12:02 AM, Ben Doyle
I wonder if you want a typeclass here, rather than a type? A Normal Rule is pretty much a State Transformer, while a Meta Rule seems like a higher-order function on Normal Rules[*]. These are different kinds of things --- and I say "kind" advisedly --- so perhaps better to define the specific commonalities you need than to try to shoehorn them both into one type.
[*]: Possibly related question: Can a Meta Rule depend upon an implementation detail of a Normal rule?
That's a very good question. I think the answer is yes. In other words, does
rule1 g == rule2 g imply myMetaRule rule1 == myMetaRule rule2
?
So this does not hold. Here myMetaRule is analysing the legality of rule1, possibly analysing the code of rule1 (quite simply for now). (To make thinks clear, the type of "myMetaRule rule1" is a monadic boolean). Thanks! Corentin

Hi,
I'm still thinking on how I can express neatly two fundamentally
aspects of a rule in Nomic.
I'm not a lawyer ;) but in my opinion there is two sorts of rules in
the legal system of any countries:
- The rules that say *what* is legal or illegal.
- The rules that say *how* to enforce the first (from what date, by
whom, is it retro-active, what is the penalty if you infringe it
etc.). These rules can be an application decree, for example.
In computing terms, the first rules can be called "functional rules"
whereas the second are "procedural rules".
Among the functional rules, there are the "meta rules" as said before,
rules that are able to judge the legality of another rule.
I'm trying to find an adequate type to embrace all this family.
Up to now, I came up with:
//functional and procedural rule types
newtype FuncRule legalizable = FuncRule {unRule :: legalizable →
State Game Bool}
type ProcRule = State Game ()
data RuleFunc2 legalizable = FR (FuncRule legalizable) | PR ProcRule
//a meta rule is a functional rule
type MetaRule = FuncRule Rule
What do you think?
I've made a design document of the complete game if you need more context:
https://github.com/cdupont/Nomic/blob/master/Nomic/doc/Haskell%20Nomic%20EN....
At the beginning of the development, the rules where only functional
and stateless. That was nice because the engine was able to execute
them whenever. Now they are becoming more and more stateful (for
example a rule can create a "bank account", or be executed on event).
This make handling them much more complex!! I'm wondering what middle
way I can choose.
Corentin
On Sat, May 19, 2012 at 1:06 AM, Corentin Dupont
Yes I totally agree, they have different kind. A Normal Rule is * whereas a Meta Rule is * -> *. But I have no experience with typeclasses. That could be what I'm looking for! What they have in common? Well, Id' say that a rule (whatever sort it is) can: - change the state of the game when executed - hence the State Transformer - perhaps assess the legality of *something* (including the legality of another rule)
So this gives: class (Monad r) => (Rule r) where changeState :: StateT Game r () legal :: l -> r Bool legal _ = return True
Does that sound good? I will continue experiment with it tomorrow. My other comment inline:
On Sat, May 19, 2012 at 12:02 AM, Ben Doyle
wrote: I wonder if you want a typeclass here, rather than a type? A Normal Rule is pretty much a State Transformer, while a Meta Rule seems like a higher-order function on Normal Rules[*]. These are different kinds of things --- and I say "kind" advisedly --- so perhaps better to define the specific commonalities you need than to try to shoehorn them both into one type.
[*]: Possibly related question: Can a Meta Rule depend upon an implementation detail of a Normal rule?
That's a very good question. I think the answer is yes.
In other words, does
rule1 g == rule2 g imply myMetaRule rule1 == myMetaRule rule2
?
So this does not hold. Here myMetaRule is analysing the legality of rule1, possibly analysing the code of rule1 (quite simply for now). (To make thinks clear, the type of "myMetaRule rule1" is a monadic boolean).
Thanks! Corentin

Hi,
I'm still thinking on how I can express neatly two fundamentally
aspects of a rule in Nomic.
I'm not a lawyer but in my opinion there is two sorts of rules in the
legal system of any countries:
- The rules that say *what* is legal or illegal.
- The rules that say *how* to enforce the first (from what date, by
whom, is it retro-active, what is the penalty if you infringe it
etc.). These rules can be an application decree, for example.
In computing terms, the first rules can be called "functional rules"
whereas the second are "procedural rules".
Among the functional rules, there are the "meta rules" as said before,
rules that are able to judge the legality of another rule.
I'm trying to find an adequate type to embrace all this family.
Up to now, I came up with:
//functional and procedural rule types
newtype FuncRule a = FuncRule {unRule :: a → State Game Bool}
type ProcRule = State Game ()
data RuleFunc2 a = FR (FuncRule a) | PR ProcRule
//a meta rule is a functional rule
type MetaRule = FuncRule Rule
What do you think?
I've made a design document of the game if you need more context:
https://github.com/cdupont/Nomic/blob/master/Nomic/doc/Haskell%20Nomic%20EN....
At the beginning of the development, the rules where only functional
and stateless. That was nice because the engine was able to execute
them whenever. Now they are becoming more and more stateful (for
example a rule can create a bank account, or create a signal handler
for future execution). This make handling them much more complex!! I'm
wondering what middle way I can choose.
Corentin
On Sat, May 19, 2012 at 1:06 AM, Corentin Dupont
Yes I totally agree, they have different kind. A Normal Rule is * whereas a Meta Rule is * -> *. But I have no experience with typeclasses. That could be what I'm looking for! What they have in common? Well, Id' say that a rule (whatever sort it is) can: - change the state of the game when executed - hence the State Transformer - perhaps assess the legality of *something* (including the legality of another rule)
So this gives: class (Monad r) => (Rule r) where changeState :: StateT Game r () legal :: l -> r Bool legal _ = return True
Does that sound good? I will continue experiment with it tomorrow. My other comment inline:
On Sat, May 19, 2012 at 12:02 AM, Ben Doyle
wrote: I wonder if you want a typeclass here, rather than a type? A Normal Rule is pretty much a State Transformer, while a Meta Rule seems like a higher-order function on Normal Rules[*]. These are different kinds of things --- and I say "kind" advisedly --- so perhaps better to define the specific commonalities you need than to try to shoehorn them both into one type.
[*]: Possibly related question: Can a Meta Rule depend upon an implementation detail of a Normal rule?
That's a very good question. I think the answer is yes.
In other words, does
rule1 g == rule2 g imply myMetaRule rule1 == myMetaRule rule2
?
So this does not hold. Here myMetaRule is analysing the legality of rule1, possibly analysing the code of rule1 (quite simply for now). (To make thinks clear, the type of "myMetaRule rule1" is a monadic boolean).
Thanks! Corentin
participants (3)
-
Ben Doyle
-
Corentin Dupont
-
Edward Z. Yang