On 10/18/08 16:48, Larry Evans wrote:
[snip]
I'm trying to apply this to a simple boolean simplifier
shown in the attachment.
This attachment is the same as the previous except, instead
of a boolean algebra, an monoid is used.
[snip]
The output
of the last line of attachment is:
[snip]
mon_eval:1*1*v0=(:*) (Op0 (OpCon MonoidOne)) (Op0 (OpVar V0))
however, what I want is a complete reduction to:
(OpVar V0)
As in the previous line beginning with mon_eval:
mon_eval:1*v0=Op0 (OpVar V0)
How can this be done using catamorphisms?
Same question w.r.t. this attachment.
TIA.
-Larry
{-# LANGUAGE PatternSignatures #-}
{-
Purpose:
"Try out" the use of catamorphism to simplify an expression
as far as possible.
Reference:
Post:
http://www.nabble.com/Re%3A-Is-there-already-an-abstraction-for-this--p19641692.html
Headers:
From: wren ng thornton
Newsgroups: gmane.comp.lang.haskell.cafe
Subject: Re: Is there already an abstraction for this?
Date: Wed, 24 Sep 2008 00:10:29 -0400
-}
module Main where
import Array
data Arity0 con var --nullary operators
= OpCon con -- constant
| OpVar var -- variable
deriving(Show)
data ArityN arity0
= Op0 arity0
| (:*) (ArityN arity0) (ArityN arity0)
deriving(Show)
infixl 7 :*
instance Functor ArityN where
fmap f (Op0 e) = Op0 (f e)
fmap f ((:*) e0 e1) = (:*) (fmap f e0) (fmap f e1)
data ConMonoid --boolean constants
= MonoidOne
deriving(Enum,Show,Ord,Eq,Bounded,Ix)
data VarName --varable names
= V0
| V1
| V2
deriving(Enum,Show,Ord,Eq,Bounded,Ix)
mon_eval :: ArityN (Arity0 ConMonoid var) -> ArityN (Arity0 ConMonoid var)
mon_eval e = case e of
{ (Op0 (OpCon MonoidOne) :* e1 ) -> e1
; (e0 :* Op0 (OpCon MonoidOne) ) -> e0
; e -> e
}
main = do
let mon_1::ArityN (Arity0 ConMonoid VarName) = Op0 (OpCon MonoidOne)
let mon_expr_1_v0 = mon_1 :* Op0 (OpVar V0)
putStr "mon_expr:1*v0="
print mon_expr_1_v0
let mon_eval_1_v0 = mon_eval mon_expr_1_v0
putStr "mon_eval:1*v0="
print mon_eval_1_v0
let mon_expr_1_1_v0 = mon_1 :* mon_expr_1_v0
putStr "mon_expr:1*1*1*v0="
print mon_expr_1_1_v0
let mon_eval_1_1_v0 = mon_eval mon_expr_1_1_v0
putStr "mon_eval:1*1*v0="
print mon_eval_1_1_v0
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe