~jojo/Carth

ref: d09e75d2aee08fb372c5e5eba38fc16f308c2ec7 Carth/src/FreeVars.hs -rw-r--r-- 652 bytes
d09e75d2JoJo Add macro rules and ellipsis (...) macro operator 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{-# LANGUAGE MultiParamTypeClasses, LambdaCase #-}

module FreeVars where

import qualified Data.Set as Set
import Data.Set (Set)

class Ord b => FreeVars a b where
    freeVars :: a -> Set b

fvApp :: FreeVars e t => e -> e -> Set t
fvApp f a = Set.unions (map freeVars [f, a])

fvIf :: FreeVars e t => e -> e -> e -> Set t
fvIf p c a = Set.unions (map freeVars [p, c, a])

fvFun :: FreeVars e t => t -> e -> Set t
fvFun p b = Set.delete p (freeVars b)

fvLet :: (FreeVars e t, Foldable f) => ([t], f e) -> e -> Set t
fvLet (bVs, bBs) e = Set.difference
    (Set.union (freeVars e) (foldr (Set.union . freeVars) Set.empty bBs))
    (Set.fromList bVs)