How to reduce code duplication when dealing with recursive sum types

前端 未结 2 965
我在风中等你
我在风中等你 2021-01-31 14:04

I am currently working on a simple interpreter for a programming language and I have a data type like this:

data Expr
  = Variable String
  | Number Int
  | Add          


        
2条回答
  •  伪装坚强ぢ
    2021-01-31 14:57

    Congratulations, you just rediscovered anamorphisms!

    Here's your code, rephrased so that it works with the recursion-schemes package. Alas, it's not shorter, since we need some boilerplate to make the machinery work. (There might be some automagic way to avoid the boilerplate, e.g. using generics. I simply do not know.)

    Below, your recurseAfter is replaced with the standard ana.

    We first define your recursive type, as well as the functor it is the fixed point of.

    {-# LANGUAGE DeriveFunctor, TypeFamilies, LambdaCase #-}
    {-# OPTIONS -Wall #-}
    module AnaExpr where
    
    import Data.Functor.Foldable
    
    data Expr
      = Variable String
      | Number Int
      | Add [Expr]
      | Sub Expr Expr
      deriving (Show)
    
    data ExprF a
      = VariableF String
      | NumberF Int
      | AddF [a]
      | SubF a a
      deriving (Functor)
    

    Then we connect the two with a few instances so that we can unfold Expr into the isomorphic ExprF Expr, and fold it back.

    type instance Base Expr = ExprF
    instance Recursive Expr where
       project (Variable s) = VariableF s
       project (Number i) = NumberF i
       project (Add es) = AddF es
       project (Sub e1 e2) = SubF e1 e2
    instance Corecursive Expr where
       embed (VariableF s) = Variable s
       embed (NumberF i) = Number i
       embed (AddF es) = Add es
       embed (SubF e1 e2) = Sub e1 e2
    

    Finally, we adapt your original code, and add a couple of tests.

    substituteName :: String -> Int -> Expr -> Expr
    substituteName name newValue = ana $ \case
        Variable x | x == name -> NumberF newValue
        other                  -> project other
    
    testSub :: Expr
    testSub = substituteName "x" 42 (Add [Add [Variable "x"], Number 0])
    
    replaceSubWithAdd :: Expr -> Expr
    replaceSubWithAdd = ana $ \case
        Sub x (Number y) -> AddF [x, Number (-y)]
        other            -> project other
    
    testReplace :: Expr
    testReplace = replaceSubWithAdd 
       (Add [Sub (Add [Variable "x", Sub (Variable "y") (Number 34)]) (Number 10), Number 4])
    

    An alternative could be to define ExprF a only, and then derive type Expr = Fix ExprF. This saves some of the boilerplate above (e.g. the two instances), at the cost of having to use Fix (VariableF ...) instead of Variable ..., as well as the analogous for the other constructors.

    One could further alleviate that using pattern synonyms (at the cost of a little more boilerplate, though).


    Update: I finally found the automagic tool, using template Haskell. This makes the whole code reasonably short. Note that the ExprF functor and the two instances above still exist under the hood, and we still have to use them. We only save the hassle of having to define them manually, but that alone saves a lot of effort.

    {-# LANGUAGE DeriveFunctor, DeriveTraversable, TypeFamilies, LambdaCase, TemplateHaskell #-}
    {-# OPTIONS -Wall #-}
    module AnaExpr where
    
    import Data.Functor.Foldable
    import Data.Functor.Foldable.TH
    
    data Expr
      = Variable String
      | Number Int
      | Add [Expr]
      | Sub Expr Expr
      deriving (Show)
    
    makeBaseFunctor ''Expr
    
    substituteName :: String -> Int -> Expr -> Expr
    substituteName name newValue = ana $ \case
        Variable x | x == name -> NumberF newValue
        other                  -> project other
    
    testSub :: Expr
    testSub = substituteName "x" 42 (Add [Add [Variable "x"], Number 0])
    
    replaceSubWithAdd :: Expr -> Expr
    replaceSubWithAdd = ana $ \case
        Sub x (Number y) -> AddF [x, Number (-y)]
        other            -> project other
    
    testReplace :: Expr
    testReplace = replaceSubWithAdd 
       (Add [Sub (Add [Variable "x", Sub (Variable "y") (Number 34)]) (Number 10), Number 4])
    

提交回复
热议问题