From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id p9I887hE004411 for ; Tue, 18 Oct 2011 10:08:07 +0200 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao0BAJ4ynU5KfVI0kGdsb2JhbABDoSYBhzAIIgEBAQEJCQ0HFAQhgW4BAQEDARICExkBGx0BAwELBgUEBzshAQERAQUBHAYTCBqHXphtCotRgmCFaD2IbgIFCogRBJN9ii+Cez2DcQ X-IronPort-AV: E=Sophos;i="4.69,364,1315173600"; d="scan'208";a="113282655" Received: from mail-ww0-f52.google.com ([74.125.82.52]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 18 Oct 2011 10:08:01 +0200 Received: by wwf25 with SMTP id 25so538748wwf.9 for ; Tue, 18 Oct 2011 01:08:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=Yco1ubU+2Oa720XgnEC1CueIwhehaYj9mXp3No7YeEo=; b=kJtmkud5gyRRxc2zBJEC2pYcoyK9dC3EwKc7zA57gjIOMlk0DZgp4omvsqmpaaUxoV vRmdBFAM8cnwg8p2dIghmlzRJBulw1x3XOL7ch0xizLE8grF7Lc36t+MsKvts+2fwWYq meAxsPJ8H9s5vBcpmiTnRvPShpUl7+33wkliU= Received: by 10.227.195.13 with SMTP id ea13mr474074wbb.36.1318925281267; Tue, 18 Oct 2011 01:08:01 -0700 (PDT) MIME-Version: 1.0 Received: by 10.227.155.67 with HTTP; Tue, 18 Oct 2011 01:07:41 -0700 (PDT) In-Reply-To: References: From: Gabriel Scherer Date: Tue, 18 Oct 2011 10:07:41 +0200 Message-ID: To: Diego Olivier Fernandez Pons Cc: caml-list Content-Type: multipart/alternative; boundary=20cf30025a58e0123e04af8e3852 Subject: Re: [Caml-list] Interpreting a language with (sparse) arrays explicitly indexed --20cf30025a58e0123e04af8e3852 Content-Type: text/plain; charset=ISO-8859-1 What about hash tables ? | Val_table of (value, value) Hashtbl.t If you have caml closures in your values (not in Val_closure but in Val_primitive), it is maybe not appropriate to use the default hash function that would choke on them. You should rather define your own hashing function --but defining a mutually recursive `value` type and Hashtable.make application will be delicate -- or change your representation of primitives to avoid ocaml functions (by reifying them into a concrete datatype for example). On Tue, Oct 18, 2011 at 1:12 AM, Diego Olivier Fernandez Pons < dofp.ocaml@gmail.com> wrote: > Caml-list, > > I have to write an interpreter for a language that has arrays explicitly > indexed by anything that can be sequential (list, range, set) > > {string} airports = {"ATL", "JFK"}; > range index = 1 .. 2; > tuple recordAirport { airport : string; id : int } > {recordAirport} otherAiports = { <"ATL", 12345>, <"JFK", 42>}; > > int myArray [airports][index] = [[1, 2], [3, 4]]; > string myArray2 [a in recordAirports][i in index] = (i < a.id) ? > "unknown" : a.airport ; > int mySliceOfArray [a in recordAirports] = sum (i in index) > myArray[a.airport][i]; > > Usually the trick in interpreter implementation is to transform everything > back to "one-dimensional" objects > - simple types > - inductive constructions for lists > - curried functions > > For instance in the book "Le langage Caml" the return type of the eval > function is > > type value = > | Val_number of int > | Val_boolean of bool > | Val_pair of value * value > | Val_nil > | Val_cons of value * value > | Val_closure of closure > | Val_primitive of value -> value > and closure = { definition: (pattern * expression) list; mutable > environnement: environnement } > and environnement == (string * value) list > > I don't see however how I am going to represent a type Val_Array given that > the indexes can be of arbitrary type and in arbitrary number. > I haven't found either how to transform the arrays into inductive types > like lists to avoid the issue. > > Any suggestions ? > > Diego Olivier > --20cf30025a58e0123e04af8e3852 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable What about hash tables ?

=A0 | Val_table of (value, value) Hashtbl.t=

If you have caml closures in your values (not in Val_closure but in= Val_primitive), it is maybe not appropriate to use the default hash functi= on that would choke on them. You should rather define your own hashing func= tion --but defining a mutually recursive `value` type and Hashtable.make ap= plication will be delicate -- or change your representation of primitives t= o avoid ocaml functions (by reifying them into a concrete datatype for exam= ple).

On Tue, Oct 18, 2011 at 1:12 AM, Diego Olivi= er Fernandez Pons <dofp.ocaml@gmail.com> wrote:
=A0 =A0 =A0Caml-list,

I have to write an interpreter for= a language that has arrays explicitly indexed by anything that can be sequ= ential (list, range, set)

=A0 =A0 {string} airport= s =3D {"ATL", "JFK"};
=A0 =A0 range index =3D 1 .. 2;
=A0 =A0 tuple recordAirport = { airport : string; id : int }
=A0 =A0 {recordAirport} otherAipor= ts =3D { <"ATL", 12345>, <"JFK", 42>};

=A0 =A0 int myArray [airports][index] =3D [[1, 2], [3, 4]];
= =A0 =A0 string myArray2 [a in recordAirports][i in index] =3D (i < a.id) ? "unknown" : a.ai= rport ;
=A0 =A0 int mySliceOfArray [a in recordAirports] =A0=3D sum (i in inde= x) myArray[a.airport][i];

Usually the trick in interpreter implementation is to t= ransform everything back to "one-dimensional" objects
-= simple types
- inductive constructions for lists
- curried functions

For instance in the book "Le langage Caml" th= e return type of the eval function is

type va= lue =3D
=A0 =A0| Val_number of int
=A0 =A0| Val_boolean= of bool
=A0 =A0| Val_pair of value * value
=A0 =A0| Val_nil
=A0 =A0| Val_cons of value * value
=A0 =A0| Val_closure of clos= ure
=A0 =A0| Val_primitive of value -> value
and clo= sure =3D=A0{ definition: (pattern * expression) list;=A0mutable environneme= nt: environnement }
and environnement =3D=3D (string * value) list

I don't see however how I am going to represent a type Val_Arra= y given that the indexes can be of arbitrary type and in arbitrary number.<= /div>
I haven't found either how to transform the arrays into inductive = types like lists to avoid the issue.

Any suggestio= ns ?

=A0 =A0 =A0 =A0 Diego= Olivier

--20cf30025a58e0123e04af8e3852--