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”.
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:
You can then define
ExprTree aas an instance ofNumand whatnot:…which allows creating unlabelled expressions in a very natural way:
Also, note the
derivingclauses above on the data definitions, particularlyOrd; 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 byEqto 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
EqandOrd, much like the one I gave forNum.After that, you can write some traversal and reduction functions, to do things like apply operators, perform variable substitution, etc.