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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:00:36+00:00 2026-05-25T13:00:36+00:00

For practice, I’m trying to define a type corresponding to lambda-calculus expressions composed of

  • 0

For practice, I’m trying to define a type corresponding to lambda-calculus expressions composed of variables, abstractions, and applications. My current best attempt is:

type expr = Var of string | Abs of expr * expr | App of expr * expr

However, I would like to restrict the first part of an Abs to be a Var. For instance, I would like this to be an ill-formed expr:

Abs(Abs(Var "foo", Var "bar"), Var "bar")

but it is currently accepted as valid. I would also like to require that the first part of an App be an Abs. Is there any way to have the type system enforce these restrictions, or is this pushing it beyond what it was designed to do?

Edit: In case it wasn’t clear, Abs(Var "x", Var "x") should be considered valid.

  • 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-25T13:00:36+00:00Added an answer on May 25, 2026 at 1:00 pm

    AFAIU, you need GADTs to achieve that, which were recently merged into svn trunk.

    Here is amateur’s example (maybe not fully correct, but it satisfies your requirements) :

            OCaml version 3.13.0+dev6 (2011-07-29)
    
    # type _ t =
      | Var : string -> string t
      | Abs : (string t * 'a t) -> (string * 'a) t
      | App : ((string * 'a) t * 'b t) -> ('a -> 'b) t;;
    type 'a t =
        Var : string -> string t
      | Abs : (string t * 'b t) -> (string * 'b) t
      | App : ((string * 'c) t * 'd t) -> ('c -> 'd) t
    # Abs(Abs(Var "foo", Var "bar"), Var("bar"));;
    Error: This expression has type (string * 'a) t
           but an expression was expected of type string t
    # App(Var "bar", Abs(Var "foo", Var "bar"));;
    Error: This expression has type string t
           but an expression was expected of type (string * 'a) t
    # App(Abs(Var "foo", Var "bar"), Var("bar"));;
    - : (string -> string) t = App (Abs (Var "foo", Var "bar"), Var "bar")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a best practice for making tooltip type functionality accessible to people with
Best practice for REST resource versioning is putting version information into Accept/Content-Type headers of
Best practice is to use unique ivs, but what is unique? Is it unique
What's the best practice for making sure that certain ajax calls to certain pages
What is best practice when creating your exception classes in a .NET solution: To
Why is it bad practice to declare variables on one line? e.g. private String
What is the best practice of Unicode processing in C++?
for practice purposes, I'm trying to find a way to print rows of numbers
For practice, I'm trying to do some stuff in Python. I've decided to make
which is the best practice to identify if a registrationId is actually used by

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.