'Match' to record fields?

34 views
Skip to first unread message

ikarus

unread,
Jul 21, 2012, 5:55:37 AM7/21/12
to bits...@googlegroups.com
Hi!

Can't find the answer in the doc and in this list, but is it possible to bind values to record type?
Will be this useful?

Something like this (sorry for code formatting):

type record = {a:int; b32:int32; c:int64; d:bool, ... }
type variant = GoodRecord of record | EmptyRecord

let dosomething bits = let r = make_record() (*defined elsewhere*)
 in
bitmatch bits with
       | {r.a: 16;
          r.d:    1;
         (*  other init code ...*)
         }     -> GoodRecord r
       | { _ } -> EmptyRecord

Or even do it in some way without make_record - hide construction from user?
If it isn't possible in current version, would it be a useful feature in future implementations or is it some way to mimic such behaviour in current versions?

Now I'm just find myself creating a record in GoodRecord constructor in pattern matching branch, but for big structures (>10 fields) it seems to be a lot of code duplication (and code looks a little bit silly):
 -> GoodRecrod {a = a; b = b; ...(* many rows to come *) }

P.S.
Thanks going for developers for a nice project.


Edgar Friendly

unread,
Jul 21, 2012, 11:58:35 AM7/21/12
to bits...@googlegroups.com
On 07/21/2012 05:55 AM, ikarus wrote:
> type variant = GoodRecord of record | EmptyRecord

btw, the option type already encapsulates this pattern.

type 'a option = Some of 'a | None

> Now I'm just find myself creating a record in GoodRecord constructor in
> pattern matching branch, but for big structures (>10 fields) it seems to
> be a lot of code duplication (and code looks a little bit silly):
> -> GoodRecrod {a = a; b = b; ...(* many rows to come *) }

With OCaml 3.12, you can do {a; b; c; ...} to create a record from
values bound to identifiers that match the record field names.

Other than this, it might be possible to modify bitstring to support
bittype records, maybe something like:

bittype record = {a: 16; d:1; ...}

Where the actual record fields are typed as bitstring would type matches
of those sizes. Then you could use something like:

bitmatch bits with
| { r: record } -> Some r
| { _ } -> None

E.

Richard W.M. Jones

unread,
Jul 21, 2012, 12:14:24 PM7/21/12
to bits...@googlegroups.com
On Sat, Jul 21, 2012 at 02:55:37AM -0700, ikarus wrote:
> Something like this (sorry for code formatting):
>
> type record = {a:int; b32:int32; c:int64; d:bool, ... }
> type variant = GoodRecord of record | EmptyRecord
>
> let dosomething bits = let r = make_record() (*defined elsewhere*)
> in bitmatch bits with
> | {r.a: 16;
> r.d: 1;
> (* other init code ...*)

Basically no, not at the moment. The closest you can do is
what you suggested, ie.

{ a : 16; d : 1; b : 2; c : 2 } -> { a = a; b = b; c = c; d = d }

It's clumsy because you end up with code like this:

https://github.com/libguestfs/hivex/blob/7ab91822f954e62e2a9854923f2f9b40856a9b5e/lib/tools/visualizer.ml#L380

Rich.

--
Richard Jones
Red Hat

ikarus

unread,
Jul 23, 2012, 9:01:30 AM7/23/12
to bits...@googlegroups.com


btw, the option type already encapsulates this pattern.

   type 'a option = Some of 'a | None


Hm, I see... Thank you for remind, I'm a newbie in ocaml, so forgot about this pattern.
But it will be possible to add some match branches (and variants) in future to the target application, so I've decided to stay with 'variant' type from first post.
 
With OCaml 3.12, you can do {a; b; c; ...} to create a record from
values bound to identifiers that match the record field names.


Nice, it'll reduce the the problem by 2/3. :-)
 
Other than this, it might be possible to modify bitstring to support
bittype records, maybe something like:

   bittype record = {a: 16; d:1; ...}

Where the actual record fields are typed as bitstring would type matches
of those sizes.  Then you could use something like:

   bitmatch bits with
     | { r: record } -> Some r
     | { _ } -> None
 
Well, hope I'll gain someday enough knowledge in ocaml & its syntax extension (preprocessor?) to make such modification possible. ;)
Reply all
Reply to author
Forward
0 new messages