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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:52:56+00:00 2026-05-23T07:52:56+00:00

I am writing a small compiler in Ocaml. In ast.mli , i have defined

  • 0

I am writing a small compiler in Ocaml. In ast.mli, i have defined 2 kinds of expressions

type int_expr =
  | Integer_constant of int
  | Evar of string
  | Ebinop of binop * int_expr * int_expr
  | Ecell of int_expr * int_expr (* Sheet[ , ] *)

type bool_expr = 
  | Bool_constant of bool
  | Bcmp of cmp * int_expr * int_expr
  | Band of bool_expr * bool_expr
  | Bor of bool_expr * bool_expr
  | Bnot of bool_expr

In interp.ml, i want to define a function called eval_expression to evaluate any expression which could either be int_expr or bool_expr

let rec eval_expression env = function
  | Integer_constant n -> Integer n
  | Ebinop (op, n, m) -> ...
  | Evar x -> ...
  | Ecell (r, c) -> ... 
  | Bool_constant b -> ...
  | Bnot c -> ...
  | Bor (c1, c2) -> ...
  | Band (c1, c2) -> ...

But it returns an error while compiling:

ocamlc  -c interp.ml
File "interp.ml", line 63, characters 4-19:
Error: This pattern matches values of type Ast.bool_expr
       but a pattern was expected which matches values of type Ast.int_expr
make: *** [interp.cmo] Error 2

Could anyone tell me how I could change the structure of my expression types so that eval_expression works? Thank you very much!

  • 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-23T07:52:57+00:00Added an answer on May 23, 2026 at 7:52 am

    You will need to write another type for arbitrary-typed expressions:

    type expr = Iexpr of int_expr
              | Bexpr of bool_expr
    

    and a type for final values which could be either integer or boolean

    type value = Ivalue of int
               | Bvalue of bool
    

    and write a function evaluate : expr -> value.

    For what it’s worth, I’ve never felt that statically separating the allowable expressions in this manner into int-typed and bool-typed expressions was ever really worth it in an ML-like language. You end up with a lot of duplicated syntax once you add richer types to your object language. What you really want is to kick the object language types into the type strata of your implementation language, but that ends up being pretty ugly in ML. It’s slightly nicer in Haskell’s richer type and kind machinery, but it still feels like a very affected programming style.

    In the end, for little throwaway interpreters I usually just code up separate tp and expr types for the type and expressions of my object language and write the eval as expr -> tp -> value

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

Sidebar

Related Questions

I have an application where I am reading and writing small blocks of data
I'm writing a compiler for a small language, and my Parser class is currently
I am writing a compiler for a small imperative language. The target language is
I'm currently writing a compiler for a small language that compiles to JavaScript. In
I'm trying to practice my F# by writing small console scripts in F# in
Coming from a PHP background, I'm used to writing small functions that return a
I am writing a small wxPython utility. I would like to use some event
I am writing a small application to get the various diagnostic parameter of Apache
I've been writing a small notice board site in ASP.NET and nothing i do
I'm writing a small program to record reading progress, the data models are simple:

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.