File: HOPat.hs

package info (click to toggle)
haskell-syb 0.7.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 360 kB
  • sloc: haskell: 2,264; makefile: 2
file content (68 lines) | stat: -rw-r--r-- 1,792 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExplicitForAll     #-}

module HOPat (tests) where

{-

This module is in reply to an email by C. Barry Jay
received on March 15, and handled within hours. CBJ
raises the very interesting issue of higher-order patterns.
It turns out that some form of it is readily covered in
our setting.

-}

import Test.Tasty.HUnit

import Data.Generics


-- Sample datatypes
data T1 = T1a Int | T1b Double
        deriving (Show, Eq, Typeable, Data)
data T2 = T2a T1 T2 | T2b
        deriving (Show, Eq, Typeable, Data)

-- Eliminate a constructor if feasible
elim' :: (Data y, Data x) => Constr -> y -> Maybe x
elim' c y = if toConstr y == c
                then unwrap y
                else Nothing


-- Unwrap a term; Return its single component
unwrap :: (Data y, Data x) => y -> Maybe x
unwrap y = case gmapQ (Nothing `mkQ` Just) y of
             [Just x] -> Just x
             _ -> Nothing


-- Eliminate a constructor if feasible; 2nd try
elim :: forall x y. (Data y, Data x) => (x -> y) -> y -> Maybe x
elim c y = elim' (toConstr (c (undefined::x))) y


-- Visit a data structure
visitor :: (Data x, Data y, Data z)
        => (x -> y) -> (x -> x) -> z -> z
visitor c f = everywhere (mkT g)
  where
    g y = case elim c y of
            Just x  -> c (f x)
            Nothing -> y


-- Main function for testing
tests = ( (  elim' (toConstr t1a) t1a) :: Maybe Int
        , ( (elim' (toConstr t1a) t1b) :: Maybe Int
        , ( (elim  T1a t1a)            :: Maybe Int
        , ( (elim  T1a t1b)            :: Maybe Int
        , ( (visitor T1a ((+) 46) t2)  :: T2
        ))))) @=? output
 where
   t1a = T1a 42
   t1b = T1b 3.14
   t2  = T2a t1a (T2a t1a T2b)

output = (Just 42,(Nothing,(Just 42,(Nothing,T2a (T1a 88) (T2a (T1a 88) T2b)))))