caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] how to convert strings to Ocaml values at run-time?
@ 2014-02-23 12:23 Matej Kosik
  2014-02-23 13:12 ` Tianyi Cui
  0 siblings, 1 reply; 6+ messages in thread
From: Matej Kosik @ 2014-02-23 12:23 UTC (permalink / raw)
  To: caml-list

Hi,

I would like to ask, what is the easiest way how to:
- take given string, provided by the user at run time
- run it through lexer --> parser --> check given value, in the current context, has a given type
- and bind the value to a variable (provided there were no lexing/parsing/typechecking problems).

(I am not yet 100% sure, that this is the right direction to solve my problem, but I am quite interested whether I could solve it this way.)

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

* Re: [Caml-list] how to convert strings to Ocaml values at run-time?
  2014-02-23 12:23 [Caml-list] how to convert strings to Ocaml values at run-time? Matej Kosik
@ 2014-02-23 13:12 ` Tianyi Cui
  2014-02-23 20:24   ` Matej Kosik
  0 siblings, 1 reply; 6+ messages in thread
From: Tianyi Cui @ 2014-02-23 13:12 UTC (permalink / raw)
  To: Matej Kosik; +Cc: caml-list

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

If you know the type of this string in advance, you can do so with
ocaml_plugin: https://github.com/janestreet/ocaml_plugin


On Sun, Feb 23, 2014 at 8:23 PM, Matej Kosik <
5764c029b688c1c0d24a2e97cd764f@gmail.com> wrote:

> Hi,
>
> I would like to ask, what is the easiest way how to:
> - take given string, provided by the user at run time
> - run it through lexer --> parser --> check given value, in the current
> context, has a given type
> - and bind the value to a variable (provided there were no
> lexing/parsing/typechecking problems).
>
> (I am not yet 100% sure, that this is the right direction to solve my
> problem, but I am quite interested whether I could solve it this way.)
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> 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: 1631 bytes --]

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

* Re: [Caml-list] how to convert strings to Ocaml values at run-time?
  2014-02-23 13:12 ` Tianyi Cui
@ 2014-02-23 20:24   ` Matej Kosik
  2014-02-24  1:30     ` Francois Berenger
                       ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Matej Kosik @ 2014-02-23 20:24 UTC (permalink / raw)
  To: caml-list

On 23/02/14 13:12, Tianyi Cui wrote:
> If you know the type of this string in advance, you can do so with ocaml_plugin: https://github.com/janestreet/ocaml_plugin

Hm, imagine that the expected type is:

	'a * 'a -> bool

Now, if user provides a string, say:

	"fun (a,b) -> a = b"

how can I convert that string to Ocaml value of the above type (like how toplevel does it)?

Is this something ocaml_plugin is able to do for me? I've failed to figure out the trick.

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

* Re: [Caml-list] how to convert strings to Ocaml values at run-time?
  2014-02-23 20:24   ` Matej Kosik
@ 2014-02-24  1:30     ` Francois Berenger
  2014-02-24  5:05     ` Tianyi Cui
  2014-02-24 13:48     ` Matthias Puech
  2 siblings, 0 replies; 6+ messages in thread
From: Francois Berenger @ 2014-02-24  1:30 UTC (permalink / raw)
  To: caml-list

On 02/24/2014 05:24 AM, Matej Kosik wrote:
> On 23/02/14 13:12, Tianyi Cui wrote:
>> If you know the type of this string in advance, you can do so with ocaml_plugin: https://github.com/janestreet/ocaml_plugin
>
> Hm, imagine that the expected type is:
>
> 	'a * 'a -> bool
>
> Now, if user provides a string, say:
>
> 	"fun (a,b) -> a = b"
>
> how can I convert that string to Ocaml value of the above type (like how toplevel does it)?
>
> Is this something ocaml_plugin is able to do for me? I've failed to figure out the trick.

I think MLdonkey could do that, but I have no idea how it was doing it.


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

* Re: [Caml-list] how to convert strings to Ocaml values at run-time?
  2014-02-23 20:24   ` Matej Kosik
  2014-02-24  1:30     ` Francois Berenger
@ 2014-02-24  5:05     ` Tianyi Cui
  2014-02-24 13:48     ` Matthias Puech
  2 siblings, 0 replies; 6+ messages in thread
From: Tianyi Cui @ 2014-02-24  5:05 UTC (permalink / raw)
  To: Matej Kosik; +Cc: caml-list

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

You can try to compile and run the hello_world example of ocaml_plugin:
https://github.com/janestreet/ocaml_plugin/blob/master/hello_world

It has a Plugin_intf module to specify the type of module expected from
loaded file. For you, it should probably be:

module type S = sig
  val equal : ('a * 'a) -> bool
end

Then you can use Ocaml_compiler module to dynamically load modules with
this signature as demonstrated in run.ml


On Mon, Feb 24, 2014 at 4:24 AM, Matej Kosik <
5764c029b688c1c0d24a2e97cd764f@gmail.com> wrote:

> On 23/02/14 13:12, Tianyi Cui wrote:
> > If you know the type of this string in advance, you can do so with
> ocaml_plugin: https://github.com/janestreet/ocaml_plugin
>
> Hm, imagine that the expected type is:
>
>         'a * 'a -> bool
>
> Now, if user provides a string, say:
>
>         "fun (a,b) -> a = b"
>
> how can I convert that string to Ocaml value of the above type (like how
> toplevel does it)?
>
> Is this something ocaml_plugin is able to do for me? I've failed to figure
> out the trick.
>
> --
> Caml-list mailing list.  Subscription management and archives:
> https://sympa.inria.fr/sympa/arc/caml-list
> 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: 2191 bytes --]

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

* Re: [Caml-list] how to convert strings to Ocaml values at run-time?
  2014-02-23 20:24   ` Matej Kosik
  2014-02-24  1:30     ` Francois Berenger
  2014-02-24  5:05     ` Tianyi Cui
@ 2014-02-24 13:48     ` Matthias Puech
  2 siblings, 0 replies; 6+ messages in thread
From: Matthias Puech @ 2014-02-24 13:48 UTC (permalink / raw)
  To: caml-list

Hi,
I am not sure if I understand your question correctly, but I guess you 
could:
- lex/parse the string to an untyped type of AST [term] as usual, and then
- have the type checking function take a [term] and produce an AST 
annotated with types ['a typed_term]. This type could be either:
     - a GADT, in which case you would need an evaluation function [eval 
: type a. a typed_term -> a], by recursion on the typed term
     - or, à la Finally Tagless [1], an abstract type declared in a 
module together with values implementing the various primitives.

The thing to remember here I think is to "box" the type ['a] of the 
expression parsed into a type annotation, i.e., ['a typed_term]. 
However, I fear it is going to be difficult to extend this approach to 
polymorphism.

Hope it helps,
     -m

[1]: http://okmij.org/ftp/tagless-final/#tagless-final

On 02/23/2014 09:24 PM, Matej Kosik wrote:
> On 23/02/14 13:12, Tianyi Cui wrote:
>> If you know the type of this string in advance, you can do so with ocaml_plugin: https://github.com/janestreet/ocaml_plugin
> Hm, imagine that the expected type is:
>
> 	'a * 'a -> bool
>
> Now, if user provides a string, say:
>
> 	"fun (a,b) -> a = b"
>
> how can I convert that string to Ocaml value of the above type (like how toplevel does it)?
>
> Is this something ocaml_plugin is able to do for me? I've failed to figure out the trick.
>


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

end of thread, other threads:[~2014-02-24 13:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-23 12:23 [Caml-list] how to convert strings to Ocaml values at run-time? Matej Kosik
2014-02-23 13:12 ` Tianyi Cui
2014-02-23 20:24   ` Matej Kosik
2014-02-24  1:30     ` Francois Berenger
2014-02-24  5:05     ` Tianyi Cui
2014-02-24 13:48     ` Matthias Puech

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).