module Opn where import Control.Monad(when) -- ------------------------------------------------------------------------- -- -- Elementary Operations -- -- ------------------------------------------------------------------------- -- Values: integral numbers that can be converted to/from bool class (Ord a, Integral a)=> Value a where b2v :: Bool-> a v2b :: a-> Bool -- Unary operations data UnOp = UMinus | Not deriving (Eq, Read, Show) -- ... and their interpretation unOp :: Value a=> UnOp-> a-> a unOp o v = case o of UMinus -> - v Not -> b2v (not (v2b v)) -- Binary Operations data BinOp = Plus | Times | Div | Equals | Less | And | Or deriving (Eq, Read, Show) -- ... and their interpretation binOp :: Value a=> BinOp -> a-> a-> a binOp o = case o of Plus -> (+) Times -> (*) Div -> div Equals -> \v1 v2-> b2v (v1 == v2) Less -> \v1 v2-> b2v (v1 < v2) And -> \v1 v2-> b2v $ v2b v1 && v2b v2 Or -> \v1 v2-> b2v $ v2b v1 || v2b v2 -- Values: Int, Integer. instance Value Int where b2v False = 0 b2v True = 1 v2b = (/= 0) instance Value Integer where b2v False = 0 b2v True = 1 v2b = (/= 0) -- A fixpoint combinator fix :: (Value a, Monad m)=> m a-> m ()-> m () fix c b = do ev <- c when (v2b ev) $ do b; fix c b