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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:46:37+00:00 2026-05-29T09:46:37+00:00

Suppose I am writing a substitute function on an abstract syntax tree data type:

  • 0

Suppose I am writing a “substitute” function on an abstract syntax tree data type:

data Term = Id String
          | If Term Term Term
          | Let String Term Term
          ...

subst :: String -- name of identifier to replace
      -> Term   -- term to replace the identifier with
      -> Term   -- body in which to perform the replacements
      -> Term
subst identifier term = go
  where go (Id id') = if identifier == id' then term else Id id'
        go (If t1 t2 t3) = If (go t1) (go t2) (go t3)
        go (Let id' term' body) = Let id' (go term') (go body)
        ...

(Ignore shadowing issues). Notice how tedious it is to write the If branch. I have to pattern match, naming the 3 parts, and then reconstruct an If applying go to each of the 3 parts explicitly. For the Let, I have to pattern match, naming the 3 parts, and reconstruct a Let applying go to the relevant 2 parts explicitly. Is there an easier (pointfree?) way to write this without having to spell out every detail?

  • 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-29T09:46:39+00:00Added an answer on May 29, 2026 at 9:46 am

    The best approach here is to use datatype-generic programming, which was precisely designed for AST-walking tasks like this. Here’s an example, using the standard SYB library:

    {-# LANGUAGE DeriveDataTypeable #-}
    
    import Data.Generics
    
    data Term = Id String
              | If Term Term Term
              | Let String Term Term
              deriving (Eq, Show, Typeable, Data)
    
    subst :: String -- name of identifier to replace
          -> Term   -- term to replace the identifier with
          -> Term   -- body in which to perform the replacements
          -> Term
    subst identifier term = everywhere (mkT go)
      where go (Id id') = if identifier == id' then term else Id id'
            go x = x
    

    This directly expresses that the transformation (in this case, applying the function go to any child of type Term) should be applied to the whole tree in a bottom-up manner. Not only is this much more concise, but the same code continues to work no matter how many constructs are added to Term, so long as the basic recursion scheme stays the same.

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

Sidebar

Related Questions

Suppose I'm writing a function that takes a list of integers and returns only
Suppose we are writing a class (let's call it Class) in an iPhone program.
Suppose I am writing a template library consisting of a function template template<T> void
Suppose I got this function call: cook(rice, kitchen->pot); After writing that, I notice parameter
Suppose I'm writing a very long string to a file using File.WriteAllText, and another
I m having a Class suppose Function I m writing such code class FunctionsList
Suppose I have a class called Person: public class Person { private String name,
I'm writing a server which is accepting incoming TCP connections. Let's suppose the server
Suppose I am writing an application in C++ and C#. I want to write
Suppose I'm writing an SNMP v1/2 agent. Is it mandatory to append the instance

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.