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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:53:49+00:00 2026-06-11T17:53:49+00:00

I am trying to write a typed abstract syntax tree datatype that can represent

  • 0

I am trying to write a typed abstract syntax tree datatype that can represent
function application.

So far I have

type Expr<'a> =
    | Constant    of 'a
    | Application of Expr<'b -> 'a> * Expr<'b> // error: The type parameter 'b' is not defined

I don’t think there is a way in F# to write something like ‘for all b’ on that last line – am I approaching this problem wrongly?

  • 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-06-11T17:53:50+00:00Added an answer on June 11, 2026 at 5:53 pm

    In general, the F# type system is not expressive enough to (directly) define a typed abstract syntax tree as the one in your example. This can be done using generalized algebraic data types (GADTs) which are not supported in F# (although they are available in Haskell and OCaml). It would be nice to have this in F#, but I think it makes the language a bit more complex.

    Technically speaking, the compiler is complaining because the type variable 'b is not defined. But of course, if you define it, then you get type Expr<'a, 'b> which has a different meaning.

    If you wanted to express this in F#, you’d have to use a workaround based on interfaces (an interface can have generic method, which give you a way to express constraint like exists 'b which you need here). This will probably get very ugly very soon, so I do not think it is a good approach, but it would look something like this:

    // Represents an application that returns 'a but consists
    // of an argument 'b and a function 'b -> 'a
    type IApplication<'a> =
      abstract Appl<'b> : Expr<'b -> 'a> * Expr<'b> -> unit
    
    and Expr<'a> = 
      // Constant just stores a value...
      | Constant    of 'a 
      // An application is something that we can call with an 
      // implementation (handler). The function then calls the
      // 'Appl' method of the handler we provide. As this method
      // is generic, it will be called with an appropriate type
      // argument 'b that represents the type of the argument.
      | Application of (IApplication<'a> -> unit) 
    

    To represent an expression tree of (fun (n:int) -> string n) 42, you could write something like:

    let expr = 
      Application(fun appl -> 
        appl.Appl(Constant(fun (n:int) -> string n), 
                  Constant(42)))
    

    A function to evaluate the expression can be written like this:

    let rec eval<'T> : Expr<'T> -> 'T = function
      | Constant(v) -> v   // Just return the constant
      | Application(f) ->
          // We use a bit of dirty mutable state (to keep types simpler for now)
          let res = ref None
          // Call the function with a 'handler' that evaluates function application
          f { new IApplication<'T> with
                member x.Appl<'A>(efunc : Expr<'A -> 'T>, earg : Expr<'A>) = 
                  // Here we get function 'efunc' and argument 'earg'
                  // The type 'A is the type of the argument (which can be
                  // anything, depending on the created AST)
                  let f = eval<'A -> 'T> efunc
                  let a = eval<'A> earg
                  res := Some <| (f a) }
          res.Value.Value
    

    As I said, this is a bit really extreme workaround, so I do not think it is a good idea to actually use it. I suppose the F# way of doing this would be to use untyped Expr type. Can you write a bit more about the overall goal of your project (perhaps there is another good approach)?

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

Sidebar

Related Questions

i am trying to write a function that will make DataRow[column] return nullable typed
I'm trying to write a function that accepts a certain type or any of
I'm trying to write a C# function and make it accept any type parameter.
I'm trying to write a program that takes a large file (of any type)
I'm trying to write dynamic method that calls Parallel.ForEach. I have checked a IL
I wish to write a function that operates on any value that can be
I am having trouble trying to write a regular expression in JavaScript that can
I have an abstract syntax tree which I need to iterate. The AST is
I'm trying write a code that can the set property value through the lambda
I am trying to write a generic type-bound procedure that takes different callback functions

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.