caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* camlp4 and class introspection
@ 2007-06-09 22:59 Joel Reymont
  2007-06-10  0:14 ` [Caml-list] " Jonathan Bryant
  0 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-06-09 22:59 UTC (permalink / raw)
  To: Caml List

Is it possible to add code to a class's initialization section using  
camlp4?

I would like to introspect the class definition to print instance  
variable names and types. Then I would like to introspect the methods.

If anyone could give an example of how this is done with camlp4 3.10  
I would really appreciate it!

To explain a bit, I would like to make sure that each defined class  
is registered with the Objective-C runtime, only once, not per object  
instantiation.

	Thanks, Joel

--
http://topdog.cc      - EasyLanguage to C# translator
http://wagerlabs.com  - Blog






^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-09 22:59 camlp4 and class introspection Joel Reymont
@ 2007-06-10  0:14 ` Jonathan Bryant
  2007-06-10 12:30   ` Joel Reymont
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Bryant @ 2007-06-10  0:14 UTC (permalink / raw)
  To: Joel Reymont; +Cc: caml-list


On Jun 9, 2007, at 6:59 PM, Joel Reymont wrote:

> Is it possible to add code to a class's initialization section  
> using camlp4?
>
> I would like to introspect the class definition to print instance  
> variable names and types. Then I would like to introspect the methods.

I don't think this would be possible since CamlP4 happens before type  
inference and therefore knows nothing about types.

>
> If anyone could give an example of how this is done with camlp4  
> 3.10 I would really appreciate it!
>
> To explain a bit, I would like to make sure that each defined class  
> is registered with the Objective-C runtime, only once, not per  
> object instantiation.
>
> 	Thanks, Joel
>
> --
> http://topdog.cc      - EasyLanguage to C# translator
> http://wagerlabs.com  - Blog
>
>
>
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-10  0:14 ` [Caml-list] " Jonathan Bryant
@ 2007-06-10 12:30   ` Joel Reymont
  2007-06-10 20:02     ` Yaron Minsky
  2007-06-11  0:58     ` Jacques GARRIGUE
  0 siblings, 2 replies; 11+ messages in thread
From: Joel Reymont @ 2007-06-10 12:30 UTC (permalink / raw)
  To: Jonathan Bryant; +Cc: caml-list


On Jun 10, 2007, at 1:14 AM, Jonathan Bryant wrote:

> I don't think this would be possible since CamlP4 happens before  
> type inference and therefore knows nothing about types.

That's really too bad. I was thinking of recognizing that type X was  
used for instance variables, for example, and generating code to  
register these appropriately with the Objective-C runtime.

I guess I could build a standalone tool to process OCaml class  
definitions and do this but I would rather have it as part of the  
regular compilation workflow.

So, in a word, there's no way to achieve what I'm looking for?  
Haskell people do this using Template Haskell and Camlp4 provides  
very similar capabilities.

	Thanks, Joel

--
http://topdog.cc      - EasyLanguage to C# translator
http://wagerlabs.com  - Blog






^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-10 12:30   ` Joel Reymont
@ 2007-06-10 20:02     ` Yaron Minsky
  2007-06-10 21:18       ` Joel Reymont
  2007-06-11  0:58     ` Jacques GARRIGUE
  1 sibling, 1 reply; 11+ messages in thread
From: Yaron Minsky @ 2007-06-10 20:02 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Jonathan Bryant, caml-list

[-- Attachment #1: Type: text/plain, Size: 1714 bytes --]

I actually think you can do this kind of thing with a combination of
type-directed programming and macros.  A similar kind of introspection-like
capabilities is achieved with the s-expression macros that we've released.
You would need to apply your macros to every type referenced by your
objects, not just to the object definitions themselves (otherwise, how is
the system to know that the value of type [foo] you refer to somewhere in
your object is really defined as [int * int] somewhere else?)

y

On 6/10/07, Joel Reymont <joelr1@gmail.com> wrote:
>
>
> On Jun 10, 2007, at 1:14 AM, Jonathan Bryant wrote:
>
> > I don't think this would be possible since CamlP4 happens before
> > type inference and therefore knows nothing about types.
>
> That's really too bad. I was thinking of recognizing that type X was
> used for instance variables, for example, and generating code to
> register these appropriately with the Objective-C runtime.
>
> I guess I could build a standalone tool to process OCaml class
> definitions and do this but I would rather have it as part of the
> regular compilation workflow.
>
> So, in a word, there's no way to achieve what I'm looking for?
> Haskell people do this using Template Haskell and Camlp4 provides
> very similar capabilities.
>
>         Thanks, Joel
>
> --
> http://topdog.cc      - EasyLanguage to C# translator
> http://wagerlabs.com  - Blog
>
>
>
>
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

[-- Attachment #2: Type: text/html, Size: 2444 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-10 20:02     ` Yaron Minsky
@ 2007-06-10 21:18       ` Joel Reymont
  2007-06-10 22:07         ` Yaron Minsky
  0 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-06-10 21:18 UTC (permalink / raw)
  To: Yaron Minsky; +Cc: Jonathan Bryant, caml-list

Yaron,

On Jun 10, 2007, at 9:02 PM, Yaron Minsky wrote:
> I actually think you can do this kind of thing with a combination of
> type-directed programming and macros.

What would be type-directed programming in this scenario?

Would this be done within camlp4?

	Thanks, Joel

--
http://topdog.cc      - EasyLanguage to C# translator
http://wagerlabs.com  - Blog



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-10 21:18       ` Joel Reymont
@ 2007-06-10 22:07         ` Yaron Minsky
  0 siblings, 0 replies; 11+ messages in thread
From: Yaron Minsky @ 2007-06-10 22:07 UTC (permalink / raw)
  To: Joel Reymont; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 1139 bytes --]

Others can probably give a better definition of type-directed programming
than I can, and it's possible I'm misusing the time.  But in any case, what
I mean by type-directed programming is roughly this:  for each type, you
define a value corresponding to that type that contains useful information
about the type.  In our s-expression macros, these values are simply
functions named sexp_of_<type> and <type>_of_sexp for each type.  The role
of these values is to essentially give you a run-time representation of the
type, in a rather static way.

Macros come in by giving you a way to automate the generation of these
type-related values, which otherwise can be rather tedious.

y

On 6/10/07, Joel Reymont <joelr1@gmail.com> wrote:
>
> Yaron,
>
> On Jun 10, 2007, at 9:02 PM, Yaron Minsky wrote:
> > I actually think you can do this kind of thing with a combination of
> > type-directed programming and macros.
>
> What would be type-directed programming in this scenario?
>
> Would this be done within camlp4?
>
>         Thanks, Joel
>
> --
> http://topdog.cc      - EasyLanguage to C# translator
> http://wagerlabs.com  - Blog
>
>
>

[-- Attachment #2: Type: text/html, Size: 1640 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-10 12:30   ` Joel Reymont
  2007-06-10 20:02     ` Yaron Minsky
@ 2007-06-11  0:58     ` Jacques GARRIGUE
  2007-06-11  8:05       ` Joel Reymont
  1 sibling, 1 reply; 11+ messages in thread
From: Jacques GARRIGUE @ 2007-06-11  0:58 UTC (permalink / raw)
  To: joelr1; +Cc: jtbryant, caml-list

From: Joel Reymont <joelr1@gmail.com>
> On Jun 10, 2007, at 1:14 AM, Jonathan Bryant wrote:
> 
> > I don't think this would be possible since CamlP4 happens before  
> > type inference and therefore knows nothing about types.
> 
> That's really too bad. I was thinking of recognizing that type X was  
> used for instance variables, for example, and generating code to  
> register these appropriately with the Objective-C runtime.

I don't know which classes you want to register with the Objective-C
runtime, but an option is to require users to write .mli files (not
too hard since they can be generated with ocamlc -i).
Then you can use camlp4 to parse the .mli and generate the
registration code to be included in the .ml file. You just have to be
careful about "open" statements.

Jacques Garrigue


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-11  0:58     ` Jacques GARRIGUE
@ 2007-06-11  8:05       ` Joel Reymont
  2007-06-11 10:11         ` Martin Jambon
  0 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-06-11  8:05 UTC (permalink / raw)
  To: Jacques GARRIGUE; +Cc: jtbryant, caml-list


On Jun 11, 2007, at 1:58 AM, Jacques GARRIGUE wrote:

> Then you can use camlp4 to parse the .mli and generate the
> registration code to be included in the .ml file.

Right. I want to parse the class definition to extract instance  
variable types and method signatures. Then I can use this information  
to create calls to the Objective-C runtime.

If there's an instance variable of type "float outlet", for example,  
then I'll know to strip outlet from OCaml code and to tell ObjC that  
it's an outlet. The whole purpose is to be able to define ObjC  
classes in OCaml while automatically creating the glue and bridging  
code.

	Thanks, Joel

--
http://topdog.cc      - EasyLanguage to C# translator
http://wagerlabs.com  - Blog






^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-11  8:05       ` Joel Reymont
@ 2007-06-11 10:11         ` Martin Jambon
  2007-06-11 10:16           ` Joel Reymont
  0 siblings, 1 reply; 11+ messages in thread
From: Martin Jambon @ 2007-06-11 10:11 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Jacques GARRIGUE, jtbryant, caml-list

On Mon, 11 Jun 2007, Joel Reymont wrote:

>
> On Jun 11, 2007, at 1:58 AM, Jacques GARRIGUE wrote:
>
>> Then you can use camlp4 to parse the .mli and generate the
>> registration code to be included in the .ml file.
>
> Right. I want to parse the class definition to extract instance variable 
> types and method signatures. Then I can use this information to create calls 
> to the Objective-C runtime.
>
> If there's an instance variable of type "float outlet", for example, then 
> I'll know to strip outlet from OCaml code and to tell ObjC that it's an 
> outlet. The whole purpose is to be able to define ObjC classes in OCaml while 
> automatically creating the glue and bridging code.

I see 2 main options:
- parse type definitions in standard OCaml syntax (what Jacques 
suggested): you can use an external 
preprocessor, the program can be compiled independently from camlp4, and 
you would discard type definitions that can't be converted to Objective-C. 
Another advantage is that you could use other similar preprocessors to 
generate code for other purposes.
- introduce a special kind of type definitions (like Yaron suggested), so 
that only these would generate Objective-C code. The big advantage is that 
you could add options. For example, if an OCaml type has 2 equivalents in 
Objective-C, you could add an annotation to choose which one to use.
It makes OCaml-like type definitions, which usually do not cover all 
possible type definitions and are not compatible with standard OCaml.



Martin


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-11 10:11         ` Martin Jambon
@ 2007-06-11 10:16           ` Joel Reymont
  2007-06-11 11:36             ` Martin Jambon
  0 siblings, 1 reply; 11+ messages in thread
From: Joel Reymont @ 2007-06-11 10:16 UTC (permalink / raw)
  To: Martin Jambon; +Cc: Jacques GARRIGUE, jtbryant, caml-list


On Jun 11, 2007, at 11:11 AM, Martin Jambon wrote:

> - introduce a special kind of type definitions (like Yaron  
> suggested), so that only these would generate Objective-C code. The  
> big advantage is that you could add options.

This is the approach that I like but I can't visualize what  special  
kind of type definition is.

> For example, if an OCaml type has 2 equivalents in Objective-C, you  
> could add an annotation to choose which one to use.

What would this look like? Can you give me an example?

> It makes OCaml-like type definitions, which usually do not cover  
> all possible type definitions and are not compatible with standard  
> OCaml.

Right, to be handled by Camlp4 only. I don't want the syntax to stray  
too far from OCaml, though.

There's much to learn for me here, how to extend the class definition  
in Camlp4 to add bridging code, for example. I'm glad that it seems  
to be doable, though.

	Thanks, Joel

--
http://topdog.cc      - EasyLanguage to C# translator
http://wagerlabs.com  - Blog






^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [Caml-list] camlp4 and class introspection
  2007-06-11 10:16           ` Joel Reymont
@ 2007-06-11 11:36             ` Martin Jambon
  0 siblings, 0 replies; 11+ messages in thread
From: Martin Jambon @ 2007-06-11 11:36 UTC (permalink / raw)
  To: Joel Reymont; +Cc: Jacques GARRIGUE, jtbryant, caml-list

On Mon, 11 Jun 2007, Joel Reymont wrote:

>
> On Jun 11, 2007, at 11:11 AM, Martin Jambon wrote:
>
>> - introduce a special kind of type definitions (like Yaron suggested), so 
>> that only these would generate Objective-C code. The big advantage is that 
>> you could add options.
>
> This is the approach that I like but I can't visualize what  special kind of 
> type definition is.
>
>> For example, if an OCaml type has 2 equivalents in Objective-C, you could 
>> add an annotation to choose which one to use.
>
> What would this look like? Can you give me an example?

json-static (there are others...)
To export an (key, value) list from OCaml, you can choose a JSON object or 
a JSON array of arrays of 2 elements.

>> It makes OCaml-like type definitions, which usually do not cover all 
>> possible type definitions and are not compatible with standard OCaml.
>
> Right, to be handled by Camlp4 only. I don't want the syntax to stray too far 
> from OCaml, though.
>
> There's much to learn for me here, how to extend the class definition in 
> Camlp4 to add bridging code, for example. I'm glad that it seems to be 
> doable, though.
>
> 	Thanks, Joel
>
> --
> http: //topdog.cc      - EasyLanguage to C# translator
> http: //wagerlabs.com  - Blog
>
>
>
>
>
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2007-06-11 11:36 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-09 22:59 camlp4 and class introspection Joel Reymont
2007-06-10  0:14 ` [Caml-list] " Jonathan Bryant
2007-06-10 12:30   ` Joel Reymont
2007-06-10 20:02     ` Yaron Minsky
2007-06-10 21:18       ` Joel Reymont
2007-06-10 22:07         ` Yaron Minsky
2007-06-11  0:58     ` Jacques GARRIGUE
2007-06-11  8:05       ` Joel Reymont
2007-06-11 10:11         ` Martin Jambon
2007-06-11 10:16           ` Joel Reymont
2007-06-11 11:36             ` Martin Jambon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).