-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComb.hs
40 lines (35 loc) · 891 Bytes
/
Comb.hs
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
-- |
-- Module : Comb
-- Copyright : (c) Adrián Enríquez Ballester, 2021
--
-- This module defines a data type for combinators
-- and some utilities for managing them.
module Comb
( Comb (..),
CombBase (..),
size,
apply,
)
where
data Comb a
= Base a
| App (Comb a) (Comb a)
deriving (Eq, Ord)
instance Show a => Show (Comb a) where
show (Base x) = show x
show (App x y) = parens x <> " " <> parens y
where
parens s = "(" <> show s <> ")"
class CombBase a where
rule :: Comb a -> Maybe (Comb a)
size :: Comb a -> Int
size (Base _) = 1
size (App x y) = size x + size y
apply :: CombBase a => Comb a -> Maybe (Comb a)
apply b@(Base _) = rule b
apply c@(App x y) =
case (rule c, apply x, apply y) of
(Just c', _, _) -> Just c'
(_, Just x', _) -> Just $ App x' y
(_, _, Just y') -> Just $ App x y'
_ -> Nothing