New language : Ocaml

New language : Ocaml

por Yannick Le Bras -
Número de respuestas: 8

Hi,

I use for a while coderunner's questions with my own jobe server (jobeinabox), but only for python questions. It work well.

My students are now working with OCaml. How could I adapt the "new language" example for OCaml programs ? I already modified the Dockerfile to install ocaml and it seems to work (ocaml works when bash to the docker container)

Thanks for this beautiful work,


Yannick

En respuesta a Yannick Le Bras

Re: New language : Ocaml

por Lilian Besson -
Hi there,
I just joined the CodeRunner forum, because of your question.
I'm extra interested by this question!

To the CodeRunner developpers, it might be easier than it seems, because a OCaml repl (console) compiled to javascript is available, see <https://BetterOCaml.ml/editor/>; for a demo!
En respuesta a Lilian Besson

Re: New language : Ocaml

por Richard Lobb -

Hi Yannick and Lilian

Thanks for the interest in CodeRunner.

I've never used ocaml and I'm afraid I don't have time to explore it. There are lots of different programming languages, and lots of different question types you might want in each one, and it's just not feasible to support them all. You can think of CodeRunner as a question-type framework, where it's over to teachers to define their preferred question types in their own preferred languages.

Have you made any progress with modifying the new language template to run ocaml, Yannick? When I look at the documentation I see multiple ways of installing and running ocaml. You may be able to get away with running the top level ocaml (installed simply with sudo apt install ocaml-nox) but if I understand correctly, that just gives you a repl shell. You could try writing the student code to a file prog.ml and then running ocaml, feeding it the line #use ''prog.ml''. But that would break if the program tries to do input at some point, wouldn't it? It looks to me (after only a few minutes browsing the documentation) that you might be better off installing opam + dune + ocaml. Then the question type could init a new project, build it, and run it. That would seem like a reasonably straightforward modification to the standard new language template.



En respuesta a Richard Lobb

Re: New language : Ocaml

por Yannick Le Bras -
Hi, I modified the Jobeinabox dockerfile to install ocaml. Then I tried something like this for the template, and it seems to work on simple examples. It's sufficient for me for the moment, but I didn't test it a lot.

I don't know if the timeout argument is necessary ? Does the Jobe server have such a limit ?

Yannick

import subprocess

__student_answer__ = """{{ STUDENT_ANSWER | e('py') }}"""
with open("prog.ml","w") as src :
print(__student_answer__,file=src)

SEPARATOR = "#<ab@17943918#@>#"

{% for TEST in TESTCASES %}
code = "#use \"prog.ml\";;"+"\n\n"+"""{{ TEST.testcode }}"""

with open("progtest.ml","w") as src :
print(code[:-1], file=src)

if True:
try:
output = subprocess.check_output("ocaml progtest.ml",shell=True,timeout=90)
print(output.decode())
except subprocess.CalledProcessError as e:
if e.returncode > 0:
# Ignore non-zero positive return codes
if e.output:
print("erreur")
print(e.output)
else:
# But negative return codes are signals - abort
if e.output:
print(e.output, file=sys.stderr)
if e.returncode < 0:
print("Task failed with signal", -e.returncode, file=sys.stderr)
print("** Further testing aborted **", file=sys.stderr)



{% if not loop.last %}
print(SEPARATOR)
{% endif %}
{% endfor %}
En respuesta a Yannick Le Bras

Re: New language : Ocaml

por Lilian Besson -
I'm new to CodeRunner so I don't understand everything, although I recognize Jinja templates !

Another possibility could be to use OCaml toplevel compiled to javascript, as for instance offered by this <https://github.com/andrewray/iocamljs>; or <http://ocsigen.org/js_of_ocaml/3.7.0/manual/files/toplevel/index.html>;.
If CodeRunner already supports running javascript, then it should be possible to add support for OCaml without requiring the server running Moodle and CodeRunner to even have OCaml installed! Just use the javascript CodeRunner backend, but between CodeRunner for OCaml files and this backend, pipe input/output through the js_of_ocaml toplevel.
En respuesta a Lilian Besson

Re: New language : Ocaml

por Richard Lobb -

Hi Lilian

Thanks for the suggestion and it's interesting that OCaml is available as a JavaScript module. However, that doesn't really help in a CodeRunner context. It's true that you could have a CodeRunner question that ran OCaml in the browser but the architecture of CodeRunner requires that the actual program execution (i.e., the running of the question's template) takes place on the sandbox server (i.e. "Jobe").

Richard


En respuesta a Yannick Le Bras

Re: New language : Ocaml

por Richard Lobb -

Good to hear you've got it working Yannick. Except for the OCaml bit, which I don't understand because I don't know OCaml, it all looks fine. You could of course dispense with the "if True" and dedent its block. The timeout=90 probably doesn't achieve anything: Jobe has its own timeout and will kill the job before the subprocess timer anyway. If that happens, CodeRunner will re-run the job, one test case at a time, until a timeout occurs again. But you might want to check with a program that gets into an infinite loop on one of the test cases only.

En respuesta a Richard Lobb

Re: New language : Ocaml

por Yannick Le Bras -

Ok thanks for the precisions.

Of course I agree with the "if True" thing : it was just too late...

Thank you for all the Job, CodeRunner is really usefull.

Yannick