-
Notifications
You must be signed in to change notification settings - Fork 1
/
ETerm.hs
60 lines (48 loc) · 1.35 KB
/
ETerm.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE DeriveDataTypeable #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
module ETerm where
import Data.Int
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import Data.Data
newtype AtomNo = AtomNo { unAtomNo :: Int32 } deriving (Show, Eq, Data, Typeable)
data ETerm
= EInteger !Integer
| ETuple [ETerm]
| EString B.ByteString
| ENil
| EList !ETerm !ETerm
| EBinary BL.ByteString
| EAtom !AtomNo
| EFun
{ funIp :: !Int
, funFree :: [ETerm]
, funMod :: B.ByteString
, etfunArity :: !Int32
, funName :: B.ByteString
}
| ENonValue
deriving (Show, Eq, Data, Typeable)
isEtermEq :: ETerm -> ETerm -> Bool
isEtermEq = (==)
isList :: ETerm -> Bool
isList (EList _ _) = True
isList ENil = True
isList (EString _) = True
isList _ = False
toErlangList :: [ETerm] -> ETerm
toErlangList [] = ENil
toErlangList (x:xs) = EList x (toErlangList xs)
fromErlangList :: ETerm -> [ETerm]
fromErlangList (EList hd tl) = hd : fromErlangList tl
fromErlangList ENil = []
fromErlangList (EString bs) = map (EInteger . fromIntegral ) (B.unpack bs)
isAtom :: ETerm -> Bool
isAtom (EAtom _) = True
isAtom _ = False
isNonValue :: ETerm -> Bool
isNonValue ENonValue = True
isNonValue _ = False
isFunction :: ETerm -> Bool
isFunction (EFun {}) = True
isFunction _ = False