Skip to content

Commit 0b17cb7

Browse files
committed
add more information on types
1 parent 74e71a4 commit 0b17cb7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ This repository contains an interpreter for the [Latte](https://www.mimuw.edu.pl
1818

1919
The Latte Interpreter is designed to execute programs written in the Latte programming language. It takes Latte source code as input and runs it, producing the corresponding output. The interpreter performs lexical analysis, parsing, and static type checking, followed by the execution of the input program. It is built using Haskell, leveraging the power and expressiveness of the functional programming paradigm.
2020

21-
What do I personally like in this implementation? The types! Look at this:
22-
https://github.com/eerio/latte-interpreter/blob/0f80c0cff471a4c5a941c2dda57667bc3a5f2eb1/app/Interpreter.hs#L56-L72
21+
What do I personally like in this implementation? The types! Please take a look at this:
22+
https://github.com/eerio/latte-interpreter/blob/0f80c0cff471a4c5a941c2dda57667bc3a5f2eb1/app/Interpreter.hs#L56-L80
23+
Beautiful, isn't it? The nontrivial thing here is the type of the [monadic transformer](https://en.wikipedia.org/wiki/Monad_transformer) (`IM a`) - as we know, monadic transformers aren't commutative, so that the type itself had to be somehow engineered. A different type could also work, but the rest of the implementation wouldn't be as elegant as now.
2324

2425

2526
## Features

app/Interpreter.hs

+8
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,27 @@ instance Show Exc where
5353

5454
instance Exception Exc
5555

56+
-- Loc type is an abstraction for indexing the cells of our memory abstraction
5657
type Loc = Int
58+
-- Env is the mapping from a variable/function/etc. name to a memory cell with it's value
5759
type Env = Map Ident Loc
60+
-- all the types supported by my version of Latte
5861
data Val = ValVoid
5962
| ValInt !Integer
6063
| ValBool !Bool
6164
| ValStr !String
6265
| ValFun !Env ![ArgC] !BlockC
6366
deriving Show
67+
-- abstraction of the memory: maps a cell number to a true value it holds
6468
type Store = Map Loc Val
69+
-- type of stdout
6570
type Log = Data.Sequence.Seq String
6671

72+
-- the most important thing: the type of `execution` of the interpreter.
6773
type IM a = ExceptT Exc (RWST Env Log Store Identity) a
6874

75+
-- a simple `malloc` function for our abstraction of the memory :)
76+
-- yes, the numbers grow indefinitely - NOP for this task
6977
alloc :: Store -> Loc
7078
alloc store = case Map.maxViewWithKey store of
7179
Nothing -> 0

0 commit comments

Comments
 (0)