110 likes | 242 Views
CAS810: WEEK 7. LECTURE: DENOTIONAL SEMANTICS OF A SIMPLE LANGUAGE WITH ENVIRONMENTS : INTERPRETATION IN HASKELL PRACTICAL AND DURING THE WEEK: Do practical exercises to be given out. HASKELL IMPLEMENTATION OF LAST WEEKS LANGUAGE.
E N D
CAS810: WEEK 7 LECTURE: DENOTIONAL SEMANTICS OF A SIMPLE LANGUAGE WITH ENVIRONMENTS : INTERPRETATION IN HASKELL PRACTICAL AND DURING THE WEEK: Do practical exercises to be given out..
HASKELL IMPLEMENTATION OF LAST WEEKS LANGUAGE • NBimplementations always add extra “detail” that doesn’t occur in the abstract definition. Some syntax is necessarily different • I’ve cut a few corners to keep it simple – not all the error situations are specified
ABSTRACT SYNTAX data Exp = SN Int | SB Bool | SV Identifier | SC Identifier | Plus Exp Exp | Minus Exp Exp | GT Exp Exp data Cmd = Assigns Identifier Exp | Sequence Cmd Cmd | While Exp Cmd | Block Dec Cmd data Dec = Const Identifier Exp | Var Identifier Exp
ABSTRACT SYNTAX - EXAMPLE (Block (Var 'r' (SV 'y')) (Block (Var 'q' (SN 0)) (While (GT (SV 'r') (SV 'x')) (Sequence (Assigns 'r' (Minus (SV 'r') (SV 'x') )) (Assigns 'q' (Plus (SV 'q') (SN 1) )) ) ) ) ) Example “Concrete Syntax” .. {Var r = y; {Var q = 0; while r > x do {r = r-x; q = q+1} } }
SEMANTIC DOMAINS -- values and locations data V = VB Bool | VN Int | V_error data D = LC Loc | EB Bool | EN Int | D_error -- the MAPS environments and stores are -- represented as a sequence of pairs data U = UCons Identifier D U | UNull | U_error data S = SCons Loc V S | SNull | S_error
AUXILIARY FUNCTIONS contents :: Loc -> S -> V contents l s = applyS s l update :: Loc -> V -> S -> S update l v s = overwrite s l v new :: S -> Loc new s = (maxloc 0 s)+1 maxloc :: Int -> S -> Int maxloc i SNull = i maxloc i (SCons l v s) | l > i = maxloc l s | otherwise = maxloc i s
meaning functions- Emeans -- exp is a var emeans (SV x) u S_error = V_error emeans (SV x) u s = contents l s where l = get_loc (applyU u x) NB extra selector function because applyU returns a Denotable value! get_loc :: D -> Loc get_loc (LC l) = l
meaning functions- Dmeans dmeans :: Dec -> U -> S -> (U,S) dmeans (Const id exp) u s = (UCons id e UNull, s) where e = get_exp (emeans exp u s) -- -- (NB get_exp :: V -> D) dmeans (Var id exp) u s = (UCons id (LC l) UNull, update l e s) where e = (emeans exp u s) l = new s
meaning functions- Cmeans - assignment cmeans (Assigns id exp) u s = update l v s where l = get_loc (applyU u id) v = emeans exp u s -- Sequence of two or more commands cmeans (Sequence c1 c2) u s = cmeans c2 u (cmeans c1 u s)
meaning functions- Cmeans -- meaning of a block command cmeans (Block dec c) u s = cmeans c u2 s1 where u1 = fst (dmeans dec u s) s1 = snd (dmeans dec u s) u2 = env_overwrite u u1
meaning functions- Cmeans cmeans (While exp c) u s | val (emeans exp u s) == False= s | otherwise = cmeans (While exp c) u (cmeans c u s)