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

  • Home
  • SEARCH
  • 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 511953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:16:12+00:00 2026-05-13T07:16:12+00:00

Lets say I’m building a parser for a domain-specific language in F#. I’ve defined

  • 0

Lets say I’m building a parser for a domain-specific language in F#.

I’ve defined a discriminated union to represent expressions:

    type Expression = 
        | Equality of Expression*Expression
        | NonEquality of Expression*Expression
        | Or of Expression*Expression
        | And of Expression*Expression
        | If of Expression*Expression
        | IfElse of Expression*Expression*Expression
        | Bool of bool
        | Variable of string
        | StringLiteral of string

Now, I’ve built up an AST of type Expression and want to generate code for it.
I have one function which does type inference and type checking on an expression.

It’s defined like

    let rec InferType expr = 
        match expr with
        | Equality(e1,e2) -> CheckTypes (InferType e1) (InferType e2)
        | Or(e1,e2) -> CheckTypes (InferType e1) (InferType e2)
        | And(e1,e2) -> CheckTypes (InferType e1) (InferType e2)
        ...

And I have another function to generate code which follows a similar pattern: Take an expression, write pattern-matching statements for each item in the union.

My question is: Is this the idiomatic way to do it in F#?

It seems to me that it would be cleaner if each member of the union defined its own InferType and GenerateCode locally with it.

If I were using C#, I would define some abstract base class called Expression with virtual methods for InferType and GenerateCode and then override them in each subclass.

Is there any other way to do this?

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

    It seems to me that it would be
    cleaner if each member of the union
    defined its own InferType and
    GenerateCode locally with it.

    I believe you mean “more familiar”, not “cleaner”.

    Really, is your ideal to have you code generator implementation spread out across 10 different classes?

    There is definitely a fundamental tension between whether you want to group things “by type” or “by operation”. The usual OO way is “by type” whereas the FP (functional programming) way is “by operation”.

    In the case of a compiler/interpreter (or most things that in OO rely heavily on the Visitor pattern), I think “by operation” is the more natural grouping. The code generator for If and And and Or may have a bit in common; the typecheckers of the various nodes will similarly have commonalities; if you make a pretty-printer, there will likely be formatting routines common to all the node-pretty-printing implementations. In contrast, printing, typechecking, and codegenning an IfElse really don’t have much to do with one another at all, so why would you want to group those in an IfElse class?

    (To answer your questions: yes, this is idiomatic. Is there another way – yes, you can do it just like you would in C#. I think you’ll find you’re much less happy with the C# way, and that the code will also be 2-3x larger that way, with no benefit.)

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

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 302k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer man i am NOT on form today, just realised you… May 13, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer You have to have your Greasemonkey script write the code… May 13, 2026 at 8:30 pm
  • Editorial Team
    Editorial Team added an answer I'm not aware of a library, but this function will… May 13, 2026 at 8:30 pm

Related Questions

Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have several web sites on my web server, all as applications
Lets say I've got two interchangeable pieces of code and I want to figure
Lets say I have an array like this: string [] Filelist = ... I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.