Without a deeper understanding of your domain it's hard for me to offer much advice. One thing you definitely should check out (and probably use) is discriminated unions, they sound like they may come in handy. See e.g.

[link:lorgonblog.spaces.live.com]

and the discussion of the 'HandValuation' type here:

[link:lorgonblog.spaces.live.com]

I'm thinking e.g. about

1
2
3
type ExoticWager =
  | Double of int * int * money
  | Treble of int * int * int * money

You may also check out

[link:lorgonblog.spaces.live.com]

for general thoughts about OOP versus FP.

By on 12/4/2008 12:28 AM ()

Discriminated Unions look pretty good. I guess my only reservation is that I have a kind of mental block about wager amount and combination being stored in the same tuple.

One reason for this is that sometimes I will wish to sort lists of wagers by lexicographic ascending runner combination, e.g.:

2/7/12 - $100.00

6/1/1 - $5.00

14/14/9 - $99.00

and sometimes by ascending wager amount

6/1/1 - $5.00

14/14/9 - $99.00

2/7/12 - $100.00

Presumably less confusing and done more elegantly if combinations and amounts are two separate items in some kind of structure or object?

OTOH I do like very much the fact that DUs get one lexicographic sort order for free.

Clearly I need to do more reading and some experimentation over the coming weekend, but thanks very much for your tips!

By on 12/4/2008 9:45 AM ()

When reading the description of your problem I was reminded of the "Composing Contracts" paper by Peyton-Jones/Eber/Sewar [1]. This paper describes a DSL for describing derivative contracts, which are not so different from the bets you describe. Creating an abstract description of a problem, often using union types to form, at an AST like structure, then interpreting them or transforming them is a common technique in FP and I believe it would work well in this case.

Having said that I don't believe you need to throw away you OO techniques entirely, one the nice things about F# is the way you can add members to its native record and union types to get an OO like feel where appropriate.

Cheers,

Rob

[1] [link:research.microsoft.com]

By on 12/4/2008 3:57 AM ()

Thanks for reminding me of this paper! I'd squirreled away the book chapter version of it in my 'Learn Haskell One Rainy Day' Folder and promptly forgotten about it. One reason I'd grabbed it originally is that I've had the idea for some time that we could certainly benefit from a high level approach to various automated position taking approaches in some of the online markets we trade in (serious 'gambling' has a lot of similarities to the the 'real' world of quants). But that's a problem for another day when I've got my head around FP a lot more.

I'm not sure (speaking from the depths of ignorance) that combinators are quite the thing for designing the simple compound types I outlined above.... BUT - there was one issue I did not mention which gives rise to headaches when done in C# & co... - which is that it would be nice at design time to be able to declaratively spell out some other constraints on permissible data. e.g. it's clear that a Treble runner combination is int * int * int - but I would also like (in Market A) to be able to limit each tuple position value to be in range 1 through 14, and in (Market B) to limit each tuple position value to be in range 1 through 20. The more generally and succinctly I can define such run-time validation checking at design time, the easier it will be to quickly implement new wager types without excessive code duplication.

I can imagine that I ought to be able to specify a wager type as a combination with associated constraint checker function, and an amount with associated constraint checker function.

Another way of saying this is that Trebles are structurally the same everywhere, but races in (say) Hong Kong have a different max number of runners in a field cf. (say) UK races. Similarly wager amounts in HK have different minimum unit amount and different permissible discrete increments than wager amounts in the UK. So it seems to me like I ought to be able to compose (hopefully succinctly and elegantly) specific market wager definitions from a structure component and a constraint component. I wonder if this makes sense?

By on 12/4/2008 10:09 AM ()

Hi,

Just some general remarks whie reading these posts (I haven't really thought deeply on how I would model your particular problem domain in F#, just commenting on some pointers and ideas given)

0) You seem to be distantiating yourself from OO. I don't think this is necessary or warranted. It's one of the paradigms in your toolbelt, and it has many good uses. F# is not a functional language - it's a functional OO language.

1) Imo, Python (or any dynamic language) is hard to beat as far as fast prototyping goes. F# is certainly better than C# and friends here though. Besides, looks like you have the experimenation phase behind you. I may be biased, but I think F# looks like an ideal language to use in your situation.

2) Robert suggested a 'combinator library' like approach. I agree that libraries written using this approach are the most elegant and flexible I have seen. However, getting there is hard: it takes a deep insight in your problem domain, and a lot of experimentation to get the 'right' decomposition. Much harder than 'divide-and-conquer' functional decomposition. I remember reading about the functional contracts paper that it took them a lot of iterations to get to the decomposition they eventually arrived at (given their background, I found that somewhat disheartening to read...).

3) Since your domain seems to be dealing with money and statistics, you might find the recently added units of measure feature handy (you won't find that in the books you mention - too recent...). Basically, F# can statically check that you're not adding dollars to euros by mistake.

my 2c,

Kurt

By on 12/6/2008 3:54 AM ()

Re your points:

0) I'll keep that in mind. I've just come from some dabbling in Scheme and may need to regurgitate some Cool Aid and accept that objects are not all bad :).

1) I'm planning to use F# because I simply cannot abide C# after doing a lot of coding in Python and a fair bit of enjoyable dabbling in PLT Scheme. I have to produce dot Net stuff for work, at the moment, hence the obvious attraction of F#. Whilst I can sometimes get away with Python for even quite large projects, I've gradually come to the dim realisation that proper type discipline can help obviate the creeping worry that 'things are getting out of hand' (which seems to manifest itself in pathological unit testitis in my case) as Python projects grow. Also, although 95% of the time, Python is 'Fast Enough', the other 5% is what people remember when you work in an MS shop where the instinctive reaction to anything open source and different is decidedly lower brain stem in nature. Not expecting a Silver Bullet, but am hopeful that (a) I can learn some useful stuff and (b) gain new insights into my problem domain whilst getting the job done.

2) Agree that teasing out a combinator library is way out of my league for the immediate future as no headhunters from Galois have seen fit to beat a path to my door yet. It will certainly take longer than the completion time for this project for me to reach (if ever) that level of understanding - have to finish tackling the easy squad first.

3) Thanks for the tip - there's a lot to be said for being fastidious about currency handling. I wonder is it possible to define a currency type such that:

(a) the minimum currency amount is (say) 0.50

(b) valid currency amounts are multiples of (say) 0.50

It would be very useful if I could enforce this type of constraint in the type system.

Many thanks for your reply. I hope to ask more specific questions about design approaches as I test more concrete ideas in code.

By on 12/7/2008 8:28 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper