[Haskell-cafe] The Q Programming Language can do symbolic manipulation -- Haskell?

Lemmih lemmih at gmail.com
Wed Aug 16 00:12:46 EDT 2006


On 8/16/06, Casey Hawthorne <caseyh at istar.ca> wrote:
> The Q Programming Language can do symbolic manipulation -- Haskell?
>
> The Q Programming Language can do the following:
>
> sqr X = X*X
>
> ==>sqr 5
> 25
>
> ==>sqr (X+1)
> (X+1)*(X+1)
>
> Can Haskell do symbolic manipulation?
>
> Or are term-rewriting and the lambda calculus sufficiently far enough
> apart concepts?

You can do (if you don't mind cheating) :

Compiling Sym              ( Sym.hs, interpreted )
:Ok, modules loaded: Sym.
*Sym> :!cat Sym.hs
module Sym where

data Sym a = Const a
           | Var String
           | Sym a `Mult` Sym a
           | Sym a `Plus` Sym a
           | Sym a `Minus` Sym a
             deriving (Eq,Show)

instance Num a => Num (Sym a) where
    (+) = Plus
    (*) = Mult
    abs = id
    signum = id
    fromInteger = Const . fromInteger

sqr x = x*x

*Sym> sqr 5
25
*Sym> sqr (Var "X" + 1)
(Var "X" Plus Const 1) Mult (Var "X" Plus Const 1)

-- 
Cheers,
  Lemmih


More information about the Haskell-Cafe mailing list