The Environment Model Semantics
When pressing the button (or Ctrl e), the expression under the cursor is sent to the OCaml pane, and the answer from OCaml is displayed there. In case of an error, the relevant part of the code in the editor is highlighted. 2 The OCaml interpreter The command ocaml (without parameters) launches the Caml interpreter. You can type Caml instructions inside and immediately get their result it is highly recommended to use the ledit command with ocaml as argument to make interacting with this inter-preter bearable. For instance, you can type: # let x = 2;; val x: int = 2. Earlier, we dusted-off our language and stdlib cheat-sheets. Because we wanted teachers to be able to give those cheats to their students as quickly as possible, we missed a few typos (special thanks to @hannes, @spop, @Khady, @holmdunc and narimiran for their keen eyes). With more time, we managed to design an opam cheat sheet we are proud of. 🍻 awesome cheatsheet. Awesome Cheatsheet' and other potentially trademarked words, copyrighted images and copyrighted readme contents likely belong to.
Environment Model Semantics Rule with Lexical ScopingTechnique to Generalize Folding
Generalized fold and List folding functionsFunction Type Inferrence
Infer the type of functions from operations nested within the function. Start off by labeling all of the bindings and parameters with a random type Tn. And, then find out the type for each of them. Use patterns like the branches of an if and else statements are the same type and same goes for match statements.Points to note are that the failure ('blah') and Exception Not_found have type 'a (just something random), so they can be restricted to whatever the other type is in a match expression. Also, let rec f x= f x in f has type 'a -> 'b
Ocaml Cheat Sheet 2020
Documenting Abstractions
A specification is a contract between an implementer of an abstraction and a client of an abstraction. An implementation satisfies a specification if it provides the described behavior.Locality: abstraction can be understood without needing to examine implementation
Modifiability: abstraction can be reimplemented without changing implementation of other abstractions
Good Specs:
Sufficiently restrictive: rule out implementations that wouldn’t be useful to clients
Sufficiently general: do not rule out implementations that would be useful to clients
Sufficiently clear: easy for clients to understand behavior
Abstraction function (AF) captures designer’s intent in choosing a particular representation of a data abstraction. Not actually OCaml function but an abstract function. Maps concrete values to abstract values. Think about Set example, where implementer sees Set as 'a list [1;2] but user sees it as {1,2}.
Many-to-one: many values of concrete type can map to same value of abstract type. [1;2] & [2;1] both map to {1,2}
Partial: some values of concrete type do not map to any value of abstract type
[1;1;2] because no duplicates
opA(AF(c)) = AF(opC(c)). AF commutes with op!
You might write:
– Abstraction Function: comment – AF: comment
– comment
Representation invariant characterizes which concrete values are valid and which are invalid.
-Valid concrete values will be mapped by AF to abstract values
-Invalid concrete value will not be mapped by AF to abstract values
Substitution Model of Evaluation
Substitution Model Evaluation- Capture-avoiding substitutionExample Module & Functor example
Modules Signatures, Structures and Functors
Basically, signature is the interface that we must follow for a certain module. The Structure of a module is the implementation of the given signature of the module. Furthermore, the functors go ahead and parameterize modules: that is, they will take in a module or multiple modules as inputs and return a new module that is parameterized with the input module. So, suppose you have a given Set module and you want this module to applicable to all types not only ints. So, you will need the notion of equality in your module, but this notion of equality is different between Ints and Strings, so you can parameterize by having a functor that has a type sig of EQUAL as its input. With functors remember to do the sharing constraints.Matching Mechanics & Type Declarations
A type synonym is a new kind of declaration. The type and the name are interchangeable in every way.Matching: Given a pattern p and a value v, decide
– Does pattern match value?
– If so, what variable bindings are introduced?
If p is a variable x, the match succeeds and x is bound to v.
If p is _, the match succeeds and no bindings are introduced
If p is a constant c, the match succeeds if v is c. No bindings are introduced
If p is C p1, the match succeeds if v is C v1 (i.e., the same constructor) and p1 matches v1. The bindings are the bindings from the sub-match.
If p is (p1,..., pn) and v is (v1,..., vn), the match succeeds if p1 matches v1, and ..., and pn matches vn. The bindings are the union of all bindings from the sub-matches.
1. If Expressions are just pattern matches
2. Lists and options are just datatypes
3. Let expressions are also pattern matches.
4. A function argument can also be a pattern.
Type Checking Rules
Type Checking Rules part of SemanticsKey Points about Modules
Other key points with modules:1. Difference between include and open is that include just sort of extends a module/ signature when its called. In general, opening a module adds the contents of that module to the environment that the compiler looks at to find the definition of various identifiers.While opening a module affects the environment used to search for identifiers, including a module is a way of actually adding new identifiers to a module proper. The difference between include and open is that we've done more than change how identifiers are searched for: we've changed what's in the module. Opening modules is usuallly not a good thing in top level as you are getting rid of the advantage of a new namespace and if you want to do it, do it locally.
2. Don't expose the type of module especially in the signature, it is smart to hid from your user as they may abuse your invariant and don't have any idea on the implementation. So, you can also change the implementation without them knowing.
3. We can also use sharing constraints in the context of a functor. The most common use case is where you want to expose that some of the types of the module being generated by the functor are related to the types in the module fed to the functor
Data Types VS Record VS Tuple
DeclareBuild/ConstructAccess/ DestructDataTypetypeConstructor namePattern matching with matchRecordtypeRecord expression with {...}Pattern matching with let OR field selection with dot operator .TupleN/ATuple expression with (...)Pattern matching with let OR fst or sndRecords are used to store this AND that. Datatypes represent this OR that. Also, a tuple is just a record with its fields referred to by position, where as with records it is by name.Algebraic Dataypes of form <Datatype: Name Student> of String
Dynamic VS Lexical Scoping
Rule of dynamic scope: The body of a function is evaluated in the current dynamic environment at the time the function is called, not the old dynamic environment that existed at the time the function was defined.Rule of lexical scope: The body of a function is evaluated in the old dynamic environment that existed at the time the function was defined, not the current environment when the function is called.
Functions as First Class Citizens
Functions are valuesCan use them anywhere we use values
First-class citizens of language, afforded all the “rights” of any other values
– Functions can take functions as arguments – Functions can return functions as results
Ocaml Cheat Sheet
...functions can be higher-orderMap: let rec map f xs = match xs with
[] -> []
| x::xs’ -> (f x)::(map f xs’)
map: ('a->'b)->'alist->'blist
Filter, Map, folds are iterators basically. They can iterate through structures just like normal loops can.