Skip to content

Commit

Permalink
add more information on types
Browse files Browse the repository at this point in the history
  • Loading branch information
eerio committed Aug 2, 2023
1 parent 74e71a4 commit 0b17cb7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ This repository contains an interpreter for the [Latte](https://www.mimuw.edu.pl

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.

What do I personally like in this implementation? The types! Look at this:
https://github.com/eerio/latte-interpreter/blob/0f80c0cff471a4c5a941c2dda57667bc3a5f2eb1/app/Interpreter.hs#L56-L72
What do I personally like in this implementation? The types! Please take a look at this:
https://github.com/eerio/latte-interpreter/blob/0f80c0cff471a4c5a941c2dda57667bc3a5f2eb1/app/Interpreter.hs#L56-L80
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.


## Features
Expand Down
8 changes: 8 additions & 0 deletions app/Interpreter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,27 @@ instance Show Exc where

instance Exception Exc

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

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

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

0 comments on commit 0b17cb7

Please sign in to comment.