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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:31:35+00:00 2026-06-17T14:31:35+00:00

When implementing call-by-value lambda-calculus in Haskell, should I force the evaluation of the arguments

  • 0

When implementing call-by-value lambda-calculus in Haskell, should I force the evaluation of the arguments to a function in the object language (i.e., the call-by-value lambda-calculus) to get around the call-by-need evaluation order of the meta-language (i.e., Haskell)?

Concretely, for the following implementation using higher-order abstract syntax:

data Exp
  = Abs (Exp -> Exp)
  | App Exp Exp

eval :: Exp -> Exp
eval exp = case exp of
  Abs _       -> exp
  App opr opd -> case eval opr of
    Abs fun -> eval (fun $ eval opd)  -- argument evaluation

on the line with comment, should I force the evaluation of eval opd by using fun $! eval opd instead?

I am aware that the evaluation order dependence between the object and meta- level can be avoid by CPS transformation. But I do not wanna bother it for the moment. I just wanna
make sure that call-by-value is implemented faithfully in Haskell. I raised this question because many example implementations I have seen seem not to take this into account. I mean
those implementations do not force eval opd. I wonder whether it is that they neglect it or that I consider too much. Thanks.

  • 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-17T14:31:36+00:00Added an answer on June 17, 2026 at 2:31 pm

    Using $! does work here, as long as evaluating eval e to weak head normal form in the meta-language corresponds to evaluating e to a value in the object language.

    Let me demonstrate that by adding a new constructor to the Exp data type to represent a diverging computation, and a value that we can actually print:

    data Exp
      = Abs (Exp -> Exp)
      | App Exp Exp
      | Bot
      | Value
    
    printExp :: Exp -> String
    printExp Bot = "bottom"
    printExp Value = "base value"
    printExp (Abs _) = "function"
    printExp (App _ _) = "thunk"
    

    For simplicity, let’s just transform it to a call to error in the evaluation function.

    eval :: Exp -> Exp
    eval exp = case exp of
      Bot         -> error "Evaluation would not terminate "
      Value       -> Value
      Abs _       -> exp
      App opr opd -> case eval opr of
        Abs fun -> eval (fun $ eval opd)
    

    Now we can see if this is call-by-value. The following code should diverge, as we are passing bottom to a function. Alas, it does not:

    *Main> let e = App (Abs (\_ -> Value)) Bot
    *Main> printExp e
    "thunk"
    *Main> printExp (eval e)
    "base value"
    

    Does it help to change $ to $!? Yes, it does:

    *Main> let e = App (Abs (const Value)) Bot
    *Main> printExp (eval e)
    "*** Exception: Evaluation would not terminate 
    

    But how reliable is that? Any change to the Exp datatype has to be carefully checked to see if forcing the outermost constructor is what you want, e.g. introduction of pairs.

    It might be a bit more reliable to use deepseq, i.e. $!!. But what I would really suggest is to add a isValue :: Exp -> Bool predicate that explicitly checks if the argument is a base value of your object language, and check isValue (eval opd) in eval before calling fun.

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

Sidebar

Related Questions

I'm implementing a general purpose function to extract a value from an arbitrary provided
I'm implementing 'check' constraints that simply call a CLR function for each constrained column.
I am implementing an API call to the MailChimp API in my web application.
While implementing XML file reading/writing in my application I saw that when I call
I'm implementing a Web service that returns a JSON-encoded payload. If the service call
I am implementing a tree browser in HTML. On clicking a node, I call
Implementing a custom Dependency Property on a Framework Element object causes my Visual Studio
Implementing the ScriptControlClass was extremely easy, unfortunately the side effects with the language implementation
Should the view have nothing event specific in its interface and call the presenter
In IronPython I am trying to call a PythonFunction with different numbers of arguments

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.