-
Notifications
You must be signed in to change notification settings - Fork 0
/
V.DenotationalSemantics.2.hs
125 lines (105 loc) · 3.27 KB
/
V.DenotationalSemantics.2.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
{----------------------------------------LEARNING OBJECTIVE
V. Denotation Semantics and Domain Theory
(1. What elements are in a given semantic domain constructed using lifting, sums, and products?)**
2. What is a good choice of semantic domain for a given language?
3. Implement a simple denotational semantics in Haskell.
----------------------------------------------------------}
{--------------------------------------------------EXAMPLE2
Exercise: Denotational Semantics - IntBool Language
<Semantic Domain>
-- Think about all the possible values
data Value
= I Int
| B Bool
| Error
deriving (Eq,Show)
----------------------------------------------------------}
-- | A simple expression language with two types.
module IntBool where
import Prelude hiding (not,and,or)
-- 1. Define the abstract syntax as a Haskell data type.
data Exp
= LitI Int
| LitB Bool
| Neg Exp
| Add Exp Exp
| Mul Exp Exp
| Equ Exp Exp
| If Exp Exp Exp
deriving (Eq,Show)
-- Some example expressions:
-- | 2 * (3 + 4) ==> 14
ex1 :: Exp
ex1 = Mul (LitI 2) (Add (LitI 3) (LitI 4))
-- | 2 * (3 + 4) == 10 ==> false
ex2 :: Exp
ex2 = Equ ex1 (LitI 10)
-- | 2 * (3 + 4) ? 5 : 6 ==> type error!
ex3 :: Exp
ex3 = If ex1 (LitI 5) (LitI 6)
-- | 2 * (3 + 4) == 10 ? 5 : 6 ==> 6
ex4 :: Exp
ex4 = If ex2 (LitI 5) (LitI 6)
-- STEP II. Identify/ the semantic domain for this language
--
-- * what types of values can we have?
-- * Int
-- * Bool
-- * Error
--
-- * how can we express this in Haskell?
data Value
= I Int
| B Bool
| Error
deriving (Eq,Show)
-- Alternative semantics domain using Maybe and Either:
--
-- data Maybe a = Nothing | Just a
-- data Either a b = Left a | Right b
--
-- type Value = Maybe (Either Int Bool)
--
-- Example semantic values in both representations:
--
-- I 6 <=> Just (Left 6)
-- B True <=> Just (Right True)
-- Error <=> Nothing
--
-- Which of the following are isomorphic to Value?
--
-- type Value1 = Either (Maybe Int) Bool -- yes
-- I 6 <=> Left (Just 6)
-- B True <=> Right True
-- Error <=> Left Nothing
-- type Value2 = Either Int (Maybe Bool) -- yes
-- I 6 <=> Left 6
-- B True <=> Right (Just True)
-- Error <=> Right Nothing
-- type Value3 = Either (Maybe Int) (Maybe Bool) -- no!
-- I 6 <=> Left (Just 6)
-- B True <=> Right (Just True)
-- Error <=> Left Nothing --or-- Right Nothing ??
--
-- STEP III. Define the valuation function
-- LINK SYNTAX & SEMANTICS -- GIVE MEANING ON SYNTAX
sem :: Exp -> Value
sem (LitI i) = I i
sem (LitB b) = B b
sem (Neg e) = case sem e of
I i -> I (negate i)
_ -> Error
sem (Add l r) = case (sem l, sem r) of
(I i, I j) -> I (i + j)
_ -> Error
sem (Mul l r) = case (sem l, sem r) of
(I i, I j) -> I (i * j)
_ -> Error
sem (Equ l r) = case (sem l, sem r) of
(I i, I j) -> B (i == j)
(B a, B b) -> B (a == b)
_ -> Error
sem (If c t e) = case sem c of
B True -> sem t
B False -> sem e
_ -> Error