* Re: [Caml-list] help
2004-04-25 1:53 ` mohammad siddiqui
@ 2004-04-25 12:13 ` Jon Harrop
2004-04-25 12:45 ` Henri DF
2004-04-25 16:44 ` [Caml-list] help Brian Hurt
2004-04-25 17:28 ` [Caml-list] help Matt Gushee
2 siblings, 1 reply; 11+ messages in thread
From: Jon Harrop @ 2004-04-25 12:13 UTC (permalink / raw)
To: caml-list
I sent you this as an e-mail on Friday:
The following types are a start for your conversion:
type fnum=int and fval=float;
type word = { wnum: fnum ref; weight: fval }
type doc = { docnum: int; queryid: int; costfactor: float; twonorm_sq: float;
words: word array }
type model = { sv_num: int; at_upper_bound: int; b: float; supvec: doc array;
alpha: float };
The following line creates an example "model" with its values filled in:
let test_model = { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 1
{ docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
{ wnum=ref 0; weight=0. } }; alpha=0. };
I deliberately made "wnum" a reference so that it can be set:
test_model.supvec.(0).words.(0).wnum := 1;
If you want to do a really direct conversion then you probably want to make
each of the fields in each of the records a reference (as I did with "wnum").
However, you may wish to exercise the functional programming side of ocaml
and write functions which replace data structures, instead of altering them
in an imperative style. Although this is unintuitive at first, because an
imperative language would do lots of copying and deleting of data structures,
the ocaml compiler is very adept at performing only the changes which are
made to a data structure...
HTH.
Cheers,
Jon.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] help
2004-04-25 12:13 ` Jon Harrop
@ 2004-04-25 12:45 ` Henri DF
2004-04-26 19:13 ` Jon Harrop
0 siblings, 1 reply; 11+ messages in thread
From: Henri DF @ 2004-04-25 12:45 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
> I sent you this as an e-mail on Friday:
>
> The following types are a start for your conversion:
>
> type fnum=int and fval=float;
>
> type word = { wnum: fnum ref; weight: fval }
> type doc = { docnum: int; queryid: int; costfactor: float; twonorm_sq: float;
> words: word array }
> type model = { sv_num: int; at_upper_bound: int; b: float; supvec: doc array;
> alpha: float };
>
> The following line creates an example "model" with its values filled in:
>
> let test_model = { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 1
> { docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
> { wnum=ref 0; weight=0. } }; alpha=0. };
>
> I deliberately made "wnum" a reference so that it can be set:
any reason not to make it a mutable ?
henri
> test_model.supvec.(0).words.(0).wnum := 1;
>
> If you want to do a really direct conversion then you probably want to make
> each of the fields in each of the records a reference (as I did with "wnum").
> However, you may wish to exercise the functional programming side of ocaml
> and write functions which replace data structures, instead of altering them
> in an imperative style. Although this is unintuitive at first, because an
> imperative language would do lots of copying and deleting of data structures,
> the ocaml compiler is very adept at performing only the changes which are
> made to a data structure...
>
> HTH.
>
> Cheers,
> Jon.
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
>
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] help
2004-04-25 12:45 ` Henri DF
@ 2004-04-26 19:13 ` Jon Harrop
2004-04-26 22:38 ` Andrew Lenharth
0 siblings, 1 reply; 11+ messages in thread
From: Jon Harrop @ 2004-04-26 19:13 UTC (permalink / raw)
To: caml-list
On Sunday 25 April 2004 1:45 pm, Henri DF wrote:
> > I deliberately made "wnum" a reference so that it can be set:
>
> any reason not to make it a mutable ?
No good reason. ;)
I've just been looking at the assembler output from ocamlopt on this. I think
that a mutable record field represents one fewer levels of indirection
compared to a reference record field. On a contrived example, using mutable
is twice as fast.
Comparing a mutable record field to a reference (not in a record), the latter
comes out as slightly quicker in my timings. But I think this is
insignificant as the assembler output seems to be identical.
Cheers,
Jon.
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] help
2004-04-26 19:13 ` Jon Harrop
@ 2004-04-26 22:38 ` Andrew Lenharth
0 siblings, 0 replies; 11+ messages in thread
From: Andrew Lenharth @ 2004-04-26 22:38 UTC (permalink / raw)
To: Jon Harrop; +Cc: caml-list
On Mon, Apr 26, 2004 at 08:13:42PM +0100, Jon Harrop wrote:
> On Sunday 25 April 2004 1:45 pm, Henri DF wrote:
> > > I deliberately made "wnum" a reference so that it can be set:
> >
> > any reason not to make it a mutable ?
>
> No good reason. ;)
Take a look at how it is implemented in Pervasives.
>From the manual:
type 'a ref = {
mutable contents : 'a;
}
So, yea, unless you like another layer of indirection, just using
mutable should save you a little bit.
Andrew
--
"It will work in practice, yes. But will it work in theory?"
--- a french diplomat's comment, recalled by Madeleine Albright
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Caml-list] Re: help
2004-04-25 1:53 ` mohammad siddiqui
2004-04-25 12:13 ` Jon Harrop
@ 2004-04-25 16:44 ` Brian Hurt
2004-04-25 17:28 ` [Caml-list] help Matt Gushee
2 siblings, 0 replies; 11+ messages in thread
From: Brian Hurt @ 2004-04-25 16:44 UTC (permalink / raw)
To: mohammad siddiqui; +Cc: zilles, skaller, rich, caml-list
On Sun, 25 Apr 2004, mohammad siddiqui wrote:
> Hello,
>
> I am having problem dealing with data structures in OCAML, I am translating
> code from C to OCAML. As shown below one structure contains array of other
> structures as its field. One of the guys here told me that we can\x12t \x13inline\x14
> structures into other structures in OCAML.
That was me.
Let's consider the C code:
typedef struct {
int a;
int b;
} foo_t;
typedef struct {
int c;
foo_t * arr;
} bar_t;
void f(bar_t * ptr) {
/* Change a foo_t */
ptr->arr[ptr->c].a += 1;
ptr->arr[ptr->c].b -= 1;
}
Fairly simply, and basically what you want to do, right? Now, Ocaml can't
"inline" (unbox is what I should have said) the foo_t's into the array.
So you can't do the above code. What you *can* implement code like (C
again):
typedef struct {
int c;
foo_t ** arr;
} bar_t;
void f(bar_t ptr) {
ptr->arr[ptr->c]->a += 1;
ptr->arr[ptr->c]->b -= 1;
}
Notice the extra level of indirection- arr is no longer a pointer to an
array of foo_t's, but instead a pointer to an array of pointers to
foo_t's.
Now, the precise translation of this, second version of the code, is this:
type foo_t = { mutable a: int; mutable b: int };;
type bar_t = { c: int; arr: foo_t array };;
let f ptr =
ptr.arr.(ptr.c).a <- ptr.arr.(ptr.c).a + 1;
ptr.arr.(ptr.c).b <- ptr.arr.(ptr.c).b - 1
;;
But we can do better than this, by converting the structures to tuples,
giving us:
type foo_t = int * int;;
type bar_t = int * (foo_t array);;
let f ((c, arr) : bar_t) =
arr.(c) <-
begin
match (arr.(c)) with
| (a, b) -> (a+1, b-1)
end
;;
Here we're taking advantage of the fact that the foo_t's are boxed (not
inlined), by simply throwing the old tuple out and replacing it with a new
tuple with the correct new values.
Does this help?
--
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford
Brian
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [Caml-list] help
2004-04-25 1:53 ` mohammad siddiqui
2004-04-25 12:13 ` Jon Harrop
2004-04-25 16:44 ` [Caml-list] help Brian Hurt
@ 2004-04-25 17:28 ` Matt Gushee
2 siblings, 0 replies; 11+ messages in thread
From: Matt Gushee @ 2004-04-25 17:28 UTC (permalink / raw)
To: caml-list
On Sun, Apr 25, 2004 at 01:53:48AM +0000, mohammad siddiqui wrote:
> Hello,
>
> I am having problem dealing with data structures in OCAML, I am translating
> code from C to OCAML. As shown below one structure contains array of other
> structures as its field. One of the guys here told me that we can?t
> ?inline? structures into other structures in OCAML.
> I don?t how to deal with this, I already spent like 2 days on it. Should I
> make use of objects and let some member of it array of other objects? I am
> not sure this will do the purpose. I really appreciate if anyone helps me
> out this.
>
> For example if try to change the value model.supvec. (1).words. (1).wnum,
> it changes the values of all elements of the array model.supvec. I tried
> using references, mutable fields, Array.set function for modifying the
> contents of individual element.
>
> The structures in OCAML are:
>
> type word = { mutable wnum:int; (* word number *)
> mutable weight:float };; (* word weight *)
>
> type doc = {
> mutable docnum:int; (* Document ID *)
> mutable queryid:
> mutable costfactor:float;
> mutable twonorm_sq:float;
> mutable words:word array
> };;
>
> type model = {
> mutable sv_num:int;
> mutable at_upper_bound:int;
> mutable b:float;
> mutable supvec:doc array;
> mutable alpha: int array;
>
> } ;;
>
> The Structures in C are
>
> typedef struct word {
> int wnum;
> float weight;
> } WORD;
>
> typedef struct doc {
> long docnum;
> long queryid;
> double costfactor;
> double twonorm_sq;
> WORD *words;
> } DOC;
>
>
>
> typedef struct model {
> long sv_num;
> long at_upper_bound;
> double b;
> DOC **supvec;
> Double *alpha;
> } MODEL;
>
> _________________________________________________________________
> FREE pop-up blocking with the new MSN Toolbar ? get it now!
> http://toolbar.msn.com/go/onm00200415ave/direct/01/
>
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives:
> http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ:
> http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
On Sun, Apr 25, 2004 at 04:30:56PM +0000, mohammad siddiqui wrote:
> I tried exactly what you have told. Its is fine when we just have one
> element in an array of words. If we have more than one, changing the value
> of one changes the values of the rest of the elements.
That's not exactly what's happening, though your example makes it look
that way.
> for example , if the test_model is initialiazed like this:
>
> let test_model =
> { sv_num=0; at_upper_bound=0; b=0.; supvec=Array.make 2
> { docnum=0; queryid=0; costfactor=0.; twonorm_sq=0.; words=Array.make 1
> { wnum=ref 0; weight=0. } }; alpha=0. };
Okay, we can see the cause of the problem here: you are initializing an
array with Array.make, where the initial value includes a ref. The
resulting array will have unique members, *except* that each 'wnum'
field points to a single, shared ref value. That's the problem.
> now if i change the values , it chnages all th elements of the array
> "supvec" in model
>
> test_model.supvec.(0).words.(0).wnum := 1;
As you probably understand by now, what this is really doing is changing
all instances of the shared value stored in the 'wnum' field. Evidently
that was the only field you tried to change, in which case it would
appear that all the elements of 'supvec' were being changed.
Here are three possible solutions:
1. Make wnum a mutable field instead of a ref.
2. Replace the array member instead of setting the ref:
test_model.supvec.(0).words <- newvalue
(but then I guess there's no point in 'wnum' being either a ref or
a mutable field)
3. Don't use Array.make to initialize the array. Instead, use a
recursive function that starts with an empty array, and adds new
elements with Array.append.
--
Matt Gushee When a nation follows the Way,
Englewood, Colorado, USA Horses bear manure through
mgushee@havenrock.com its fields;
http://www.havenrock.com/ When a nation ignores the Way,
Horses bear soldiers through
its streets.
--Lao Tzu (Peter Merel, trans.)
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 11+ messages in thread