[Haskell-cafe] mapping a concept to a type

Corentin Dupont corentin.dupont at gmail.com
Mon Jun 4 12:06:47 CEST 2012


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.docx

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
<corentin.dupont at gmail.com> wrote:
> 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
> <benjamin.peter.doyle at gmail.com> 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



More information about the Haskell-Cafe mailing list