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 4083822
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:21:42+00:00 2026-05-20T18:21:42+00:00

(The following question concerns the OCaml language and has examples in OCaml, but the

  • 0

(The following question concerns the OCaml language and has examples in OCaml, but the question is very general and probably a correct answer for any other computer language will solve my problem too. So, just assume this question in your favourite language.)

I want to write a function which takes an arbitrary program in OCaml as a string and decides if the program is correct or incorrect and, in the latter case, whether or not I can make it into a correct one by concatenating appropriate characters at the end.

I’m assuming there is a compiler of the language somewhere and that I can apply it and get a reply saying either “Compiles” or “Doesn’t compile — error at line X, character Y” (as is the case with most languages anyway). In summary, I’d like to have a function which takes a program and returns:

  • Correct — if the string contains a correct program;
  • Erroneous — if the string contains an incorrect program which, no matter how you concatenate characters to it, will never become Correct;
  • Incomplete — if the string contains an incorrect program which is not Erroneous.

For example, the OCaml program let x = f is incorrect because f has not been defined by the time it is used. And it can’t be continued, because whatever you write after f will always be some identifier which hasn’t been defined before. The program let x = is also incorrect; but if we extend to let x = 5 then we have a perfectly valid program. So, my function should return Erroneous in the first case and Incomplete in the second.

Things may become tricky if we have the program

let ans = 5
let x = a

because my function would have to see that if I continue the program with ns then the program becomes correct.

My question is: do you think it is possible to write such function/algorithm? If so, what’s the general idea? If not, try to convince me that it’s not.

(I’ll be happy with any insights or partial answers, e.g. something which implies Incomplete. For example, I believe that if the language compiler says there is an error at line 3 and the program has 100 lines, then there is no possible continuation of the program.)

  • 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-20T18:21:43+00:00Added an answer on May 20, 2026 at 6:21 pm

    In your first example, let x = f, what if I add un y -> y ?

    I think what you want is possible, but no with the current tools. If you were only interested in syntactic correctness, the basic idea would be to run the parser/lexer, return “erroneous” if it raises an error, and “incomplete” if it hasn’t returned a complete AST, but no error (so it is still waiting for more input).

    Note : there is still a small mismatch as the lexer will return a token just before EOF, which could have been continued. You would need not to consider that token as a complete token and do some finer reasoning at that point. More generally, the extremum of your input will require specialized reasoning that I don’t cover here.

    The properties that makes it easy at the lexing/parsing stage is that the lexer is demand-driven by the parser — it only reads as far as necessary for the parser to reason about the token stream — and the parser is “strict”, or fails early, instead of asking for more information at a failure site.

    The latter major phase of a program correctness are the identifier resolution (what does this variable name refer to ?) and the type system — there are other criterions, such as checking the arity of constructors and type names, but they’re not very interesting wrt. you problem. Those are generally not written in a demand-driven style, or do not more generally attempt to reason about partial information. It should be possible to redesign them in this way, but this would probably require a lot of effort.

    A good direction to go would be “incremental parsers”. A lot of people have recognized the need for incrementality in program tools related to an editor (where programs are incrementally written). They tackle the more general problem of updating the abstract information after a concrete change in the source code; not only a change “added at the end”, but more general kind of changes. Their tools could probably solve your problem too.

    Edit: Ah, I finally found what I was looking for. You should have a look at Oleg’s differentiating parsers.

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

Sidebar

Related Questions

Based on the following question: Check if one string is a rotation of other
I am using Sqlite3 with Flask microframework, but this question concerns only the Sqlite
Not really a question but I'm looking for comments/suggestions from anyone who has experiences
This question concerns generics and types as well as datatables. The following snippet is
Perhaps this question has been asked before in a different way, but I haven’t
The following question answers how to get large memory pages on Windows : how
The following question was asked in one of my interview (few years back) What
This question is overflow from the following question: How do I programmatically convert mp3
This question builds on the following question (s) Indexing : How do I construct
I thought about the following question about computer's architecture. Suppose I do in Python

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.