Samuel Bayliss wrote: > Dear All. > > Newbie question. > > I'm a bit bewildered as to style when building some data structures in > my first eclipse program... > > I have a list of 'command' structures defined with the > > :- export struct( > command(type,time,bank) > ). > > Early in the program, the type and bank variables for structures > contained in my command list are grounded and no backtracking occurs > over these. The time variable remains unbound (this is a scheduling > program). > > Some of the command variables in my program have additional information > attached to them - i.e. read commands are commands, but have associated > information (which row and column to start reading from and how long a > burst to read for), activate and precharge commands have row > information, but no burst length information. > > Essentially these command variables have types, I'm trying to associate > extra information with each variable according to it's type. It's akin a > problem I'd solve with inheritance in an object oriented imperative > language. I'm guessing this is a pretty common pattern people face. > > How do people go about implementing this kind of pattern in > Prolog/Eclipse - I suspect that the inherited structures syntax in > eclipse nearly does what I want, but will create a lot of unbound > variables I don't care about and the attributed variable implementation > sounds similarish, but seems overkill. > > Thanks in advance. > > Sam I would use your 'type' field to not only hold the name of the command type, but hold a structure that contains all the type-specific information (the type would then be implicit in the functor of this sub-structure). So, you'd use your command-declaration from above, and declare e.g. :- export struct(command(type,time,bank)), struct(read(row,col,len)), struct(activate(row)). and then use it like ..., Command1 = command{type:read{row:3,col:7}, time:T1}, ... ..., Command2 = command{type:activate{row:1}, time:T2}, ... But alternatively, you could turn things inside-out, and have the specific type-structures contain the generic command-struct, so :- export struct(command(time,bank)), struct(read(cmd:command,row,col,len)), struct(activate(cmd:command,row)). That would allow you to maintain the illusion of flatness: ..., Command1 = read{row:3, col:7, time:T1}, ... ..., Command2 = activate{row:1, time:T2}, ... which is simply a shorthand for ..., Command1 = read{row:3, col:7, cmd:command{time:T1}}, ... ..., Command2 = activate{row:1, cmd:command{time:T2}}, ... -- JoachimReceived on Wed Feb 25 2009 - 04:58:25 CET
This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:20 CEST