Skip to content

All Posts

welcome to my blog :^)

164 Midterm Prep

As of 12:35AM October 28th 2025, I am in poor shape.

Week 0

compiler : source_program -> target_program

Compilers take source code written in one language and return source code written in another language.

interpreter : source_program -> value

Interpreters take source code written in some language and return a value.

164 compiler : OCaml -> x86-64 machine code

  • runtime is in C

entry function: the entry point to the code our compiler is producing


OCaml

  • is a functional programming language
    • for each input, it always produces the same output
  • has first-class functions
    • you can use functions as input to other functions, and produce functions as output
  • has type inference
    • compiler automatically figures out most data types
  • is statically-typed
    • detects type errors at compile time
  • is type-safe
    • limits which kinds of operations can be performed on which kinds of data
  • is primarily immutable
    • no side effects that update state when producing a return value

In the toplevel, running let x = 24;; returns val x : int = 42.

  • "x has type int and equals 42."

Week 1

S-Expressions

type s_exp =
    Sym of string | Num of int | Lst of s_exp list
  • Lst takes as its argument a list of other s-expressions.
    • fold_left

Unary Operations

Pipeline expressions: x |> f is the same as f x.

Dev environment

Works for Fall 2025

flake.nix
{
  description = "cs164";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
    systems.url = "github:nix-systems/default";
  };
  outputs = { self, nixpkgs, systems }:
    let
      pkgsFor = system: nixpkgs.legacyPackages.${system};
      forAllSystems = fn: nixpkgs.lib.genAttrs (import systems) (system: fn (pkgsFor system));
    in
    {
      packages = forAllSystems (pkgs: {
        default = pkgs.callPackage ./. { };
      });
      devShells = forAllSystems (pkgs: {
        default = pkgs.mkShell {
          inputsFrom = [ self.packages.${pkgs.system}.default ];
          #shellHook = "mkdocs serve";
        };
      });
      formatter = forAllSystems (pkgs: pkgs.nixpkgs-fmt);
    };
}
default.nix
{ pkgs }:

pkgs.stdenvNoCC.mkDerivation {
  name = "cs164";
  src = ./.;

  nativeBuildInputs = with pkgs; [
    ocaml
    dune_3
    ocamlPackages.findlib
    ocamlPackages.ounit2
    ocamlPackages.ppx_deriving
    ocamlPackages.menhir
    ocamlPackages.yojson
    ocamlPackages.janeStreet.shexp
    ocamlPackages.core_unix
    ocamlPackages.ppx_blob
    nasm

    # for examples in class notes
    libgccjit
  ];
}

In a directory with both of these files on a machine with Nix installed, run nix develop --extra-experimental-features nix-command --extra-experimental-features flakes. Remove flags if you have flakes or nix-command enabled systemwide.