~jojo/Carth

ref: 65fedc5ad30893bac36f128dac201ff7081f8e1b Carth/src/FreeVars.hs -rw-r--r-- 726 bytes
65fedc5aJoJo Allow Box in pattern to dereference 1 year, 10 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
24
{-# LANGUAGE MultiParamTypeClasses, LambdaCase #-}

module FreeVars (FreeVars(..), fvApp, fvIf, fvFun, fvLet) where

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

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) => (Set t, f e) -> e -> Set t
fvLet (bVs, bBs) b = Set.difference
    (Set.union (freeVars b) (foldr (Set.union . freeVars) Set.empty bBs))
    (Set.fromList (toList bVs))