On Thu, Apr 26, 2012 at 10:21:36AM +0200, José Pedro Magalhães wrote:
> Hi Romildo,
> 
> If I understand correctly, you now want to add annotations to
> mutually-recursive datatypes. The annotations package supports that.
> Section 8 of our paper [1] gives an example of how to do that, and also
> Chapter 6 of Martijn's MSc thesis [2].
> 
> Let me know if these references do not answer your question.

I am reading Martijn's MSc thesis and trying some code he presents. In
secton 5.1 he presents catamorphisms over fixed points.

The code I am trying is attached.

When evaluating the expression

    cata exprEval (runExpr (1+2*3))

I am getting the following error:

    No instance for (Functor ExprF)
      arising from a use of `cata'
    Possible fix: add an instance declaration for (Functor ExprF)
    In the expression: cata exprEval (runExpr (1 + 2 * 3))
    In an equation for `it': it = cata exprEval (runExpr (1 + 2 * 3))

How should an instance of (Functor ExprF) be defined? It is not shown in
the thesis.

Romildo
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}

module Annot where

newtype Fix f = In { out :: f (Fix f) }

deriving instance Show (f (Fix f)) => Show (Fix f)

data Ann x f a = Ann x (f a)
                 deriving (Show)

instance Functor f => Functor (Ann x f) where
  fmap f (Ann x t) = Ann x (fmap f t)

type Algebra f a = f a -> a

cata :: Functor f => Algebra f a -> Fix f -> a
cata f = f . fmap (cata f) . out
module Expr where

import Annot

type Id = String

data Op = Add | Sub | Mul | Div
          deriving (Show)

data ExprF r = Num Double
             | Var Id
             | Bin Op r r
               deriving (Show)


newtype Expr = Expr { runExpr :: Fix ExprF }
               deriving (Show)


instance Num Expr where
  Expr x + Expr y = Expr (In (Bin Add x y)) -- (+) = ((Expr . In) .) . (. runExpr) . Bin Add . runExpr
  Expr x - Expr y = Expr (In (Bin Sub x y)) -- (-) = (Expr . In) .: (Bin Sub `on` runExpr), where infixr 9 .: ; (.:) = (.) . (.)
  Expr x * Expr y = Expr (In (Bin Mul x y))
  abs             = undefined
  signum          = undefined
  fromInteger     = Expr . In . Num . fromInteger

instance Fractional Expr where
  Expr x / Expr y = Expr (In (Bin Div x y))
  fromRational    = Expr . In . Num . fromRational


data Bounds = Bounds Int Int
              deriving (Show)

newtype PosExpr = PosExpr { runPosExpr :: Fix (Ann Bounds ExprF) }
                  deriving (Show)





exprEval :: Algebra ExprF Double
exprEval expr = case expr of
                  Num n -> n
                  Var v -> 0
                  Bin op x y -> let g = case op of Add -> (+)
                                                   Sub -> (-)
                                                   Mul -> (*)
                                                   Div -> (/)
                                in g x y
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to