-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAST.hs
73 lines (58 loc) · 1.9 KB
/
AST.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
61
62
63
64
65
66
67
68
69
70
71
72
73
module AST where
import Data.String (IsString (..))
import Descent (Descent(..), DescentConfig (..), generateDescentInstances)
-- An example program type.
data Prog
= Var NameVar
| Lam NameDecl Prog
| App Prog Prog
| Let Decl Prog
| Match Prog [Alt]
| Const Constant
| New [Decl]
| Ctor NameCtor Int
| Mark String Prog
deriving stock (Eq, Ord, Show)
instance IsString Prog where fromString = Var . fromString
-- | I recommend separating types for usage and declaration of some entity.
--
-- This will greatly help when working with this entity.
--
-- So, typenames, for instance, should have their own TName/TNameUsed newtypes.
--
data Name = Name { unName :: String, index :: Int } deriving stock (Eq, Ord)
instance Show Name where
show (Name s (-1)) = s
show (Name s n) = "#" ++ show n
instance IsString Name where fromString = flip Name (-1)
newtype NameVar = NameVar { unNameVar :: Name } deriving newtype (Eq, Ord, Show, IsString)
newtype NameDecl = NameDecl { unNameDecl :: Name } deriving newtype (Eq, Ord, Show, IsString)
newtype NameCtor = NameCtor { unNameCtor :: Name } deriving newtype (Eq, Ord, Show, IsString)
newtype NameLet = NameLet { unNameLet :: Name } deriving newtype (Eq, Ord, Show, IsString)
data Alt
= Alt Pat Prog
deriving stock (Eq, Ord, Show)
data Decl
= Bind { name :: NameLet, body :: Prog }
deriving stock (Eq, Ord, Show)
data Pat
= IsCtor NameCtor [Pat]
| IsVar NameDecl
| IsConst Constant
| IsObj [IsDecl]
| IsView Prog Pat
| As Pat Pat
deriving stock (Eq, Ord, Show)
data IsDecl
= IsBind NameVar Pat
| IsCapture NameDecl
deriving stock (Eq, Ord, Show)
data Constant
= I Integer
| F Double
| S String
deriving stock (Eq, Ord, Show)
generateDescentInstances DescentConfig
{ recure = [''Prog, ''Alt, ''Decl, ''Pat, ''IsDecl]
, visit = [''NameVar, ''NameDecl, ''NameCtor, ''NameLet, ''Constant]
}