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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:00:21+00:00 2026-05-22T12:00:21+00:00

For haskell practice I want to implement a game where students/pupils should learn some

  • 0

For haskell practice I want to implement a game where students/pupils should learn some algebra playfully.

As basic datatype I want to use a tree:

  • with nodes that have labels and algebraic operators stored.
  • with leaves that have labels and variables (type String) or numbers

Now I want to define something like

data Tree = Leaf {l :: Label, val :: Expression}
          | Node {l :: Label, f :: Fun, lBranch :: Tree, rBranch :: Tree}

data Fun = "one of [(+),(*),(-),(/),(^)]"

-- type Fun = Int -> Int 

would work

Next things I think about is to make a ‘equivalence’ of trees – as multiplication/addition is commutative and one can simplify additions to multiplication etc. the whole bunch of algebraic operations.
I also have to search through the tree – by label I think is best, is this a good approach.

Any ideas what tags/phrases to look for and how to solve the “data Fun”.

  • 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-22T12:00:21+00:00Added an answer on May 22, 2026 at 12:00 pm

    To expand a bit on Edward Z. Yang’s answer:

    The simplest way to define your operators here is probably as a data type, along with the types for atomic values in leaf nodes and the expression tree as a whole:

    data Fun = Add | Mul | Sub | Div | Exp deriving (Eq, Ord, Show)
    
    data Val a = Lit a | Var String deriving (Eq, Ord, Show)
    
    data ExprTree a = Node String Fun (ExprTree a) (ExprTree a)
                    | Leaf String (Val a)
        deriving (Eq, Ord, Show)
    

    You can then define ExprTree a as an instance of Num and whatnot:

    instance (Num a) => Num (ExprTree a) where
        (+) = Node "" Add
        (*) = Node "" Mul
        (-) = Node "" Sub
        negate = Node "" Sub 0
        fromInteger = Leaf "" . Lit
    

    …which allows creating unlabelled expressions in a very natural way:

    *Main> :t 2 + 2
    2 + 2 :: (Num t) => t
    *Main> 2 + 2 :: ExprTree Int
    Node "" Add (Leaf "" (Lit 2)) (Leaf "" (Lit 2))
    

    Also, note the deriving clauses above on the data definitions, particularly Ord; this tells the compiler to automatically create an ordering relation on values of that type. This lets you sort them consistently which means you can, for instance, define a canonical ordering on subexpressions so that when rearranging commutative operations you don’t get stuck in a loop. Given some canonical reductions and subexpressions in canonical order, in most cases you’ll then be able to use the automatic equality relation given by Eq to check for subexpression equivalence.

    Note that labels will affect the ordering and equality here. If that’s not desired, you’ll need to write your own definitions for Eq and Ord, much like the one I gave for Num.

    After that, you can write some traversal and reduction functions, to do things like apply operators, perform variable substitution, etc.

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

Sidebar

Related Questions

Reading Real world Haskell i found some intresting question about data types: This pattern
What is Haskell's syntax for importing modules in another directory? I'm getting started with
I know newtype is more often compared to data in Haskell, but I'm posing
Apologies in advance if this question is a bit vague. It's the result of
When all you have is a pair of bolt cutters and a bottle of
I have got interested in and looking for practical examples of SMT Z3 usage
I believe the Erlang community is not envious of Node.js as it does non-blocking
I am impatient, looking forward to understanding catamorphism related to this SO question :)
I cannot really get it. Why do we need it at all? I mean
Currently I am working on a automated theorem prover in Java. I would like

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.