Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3975344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:40:26+00:00 2026-05-20T04:40:26+00:00

I would like to write an OCaml module having a compile function which will

  • 0

I would like to write an OCaml module having a compile function which will take a string containing an OCaml program and output the outcome of compilation as either Correct or Error, and in the Error case, information on the line and character of the first error. I don’t care about what the error was (at least for now).

A basic module type for this is:

module type Compile =
    sig
        type result = Correct | Error of (int*int)
        val compile : string -> result
    end

There at least two fundamentally different ways to implement it:

  1. the quick hack way — write the string with the program to a file, use Unix processes to invoke ocamlc on the command line and parse the stderr;
  2. the proper way — use OCaml tools to analyse the code.

Regarding (1), I’m having trouble getting hold of the contents of stderr. I started by using
Unix.open_process_in, which redirects stdout to an in_channel which I can then read.
Since the compilation errors output by ocamlc go to stderr (and not to stdout), I tried to use

let ic = Unix.open_process_in "ocamlc test.ml 2>&1" 

so that the content of stderr was redirected at the command line to stdout. This way, I’d expect, ic
would contain the compilation errors. Unfortunately, this was not the case and ic contained just End_of_file.

I then moved to the Unix.create_process command, as it allows me to choose new channels for out, in, err. But I’m having trouble choosing these channels. What should the out channel be so that I can then read it in my program?

Regarding (2), what do you think is a (reasonably) simple way to go?

Many thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:40:27+00:00Added an answer on May 20, 2026 at 4:40 am

    Regarding 1) I think you did something wrong. On my machine :

    > ocaml unix.cma
            Objective Caml version 3.12.0
    
    # let ic = Unix.open_process_in "ocamlc test.ml 2>&1";;
    val ic : in_channel = <abstr>
    # input_line ic;;
    - : string = "File \"test.ml\", line 1, characters 0-1:"
    # input_line ic;;
    - : string = "Error: I/O error: test.ml: No such file or directory"
    # input_line ic;;
    Exception: End_of_file.
    

    For Unix.create_process one way of doing is to pass Unix.stdin for new_stdout so that you can read the output of the process via the standard input of your program (this assumes you don’t want to use the stdin of your program for something else).
    But a simpler way me be to use Unix.open_process_full.

    Regarding 2) you can try to use toplevellib.cma (note however that this is completely undocumented and unsupported). Have a look at toplevel/toploop.mli in the distribution, in particular Toploop.execute_phrase.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write an OCaml function which takes a URL and returns
I would like to write a small program in C# which goes through my
I would like to write a program that will identify a machine( for licensing
I would like to write a javascript plug in which can string manipulations something
I would like to write some functions that take a single parameter which could
I would like to write a plug-in that will allow a custom written CRM
I would like to write a utility that will provide me with a relatively
I would like to write a macro for Notepad++ which should replace char1, char2,
I would like to write an application that will copy MP3 files to a
My SQL is very rusty. I would like write a query to take field1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.