2024-11-26T04:19:27+03:00[Europe/Moscow] en true <p>Programs _______ their inputs</p>, <p>Define pure functions</p>, <p>Is this function pure?</p>, <p>Is this function pure?</p>, <p>Total functional program definition</p>, <p>"Well typed programs cannot _________"</p>, <p>Type system purpose/function</p>, <p>Sound type system meaning</p>, <p>Type systems are usually _______ but _________</p>, <p>Incomplete type system meaning</p>, <p>Define compositional reasoning</p>, <p>Define continuation</p>, <p>Functional core, imperative shell definition</p>, <p>Atomic data examples</p>, <p>6 steps in design recipe</p>, <p>Template definition</p>, <p>Templates have _______ (the “...” parts), and an ________ (pieces of the inputs that are likely to be useful)</p>, <p>Sum types vs product types</p>, <p>Enumeration definition/example</p>, <p>Interval definition/example</p>, <p>Itemization definition/example</p>, <p>Structure definition/example</p>, <p>Itemization of structure: definition/example</p>, <p>Self-referential data: definition/explanation</p>, <p>Natural recursion definition</p>, <p>Lists, self referential data definition+template in racket</p>, <p>Generative recursion definition/function</p>, <p>Natural recursion is also called ________ recursion</p>, <p>What is the time complexity of the following function:</p><p></p><p>;; reverse : ListOf&lt;X&gt; -&gt; ListOf&lt;X&gt; </p><p>;; Answers items from `input`, in reverse order. </p><p>(define (reverse xs) </p><p> (cond </p><p> [(null? xs) '()] </p><p> [(cons? xs) (append (reverse (rest xs)) (cons (first xs) '()))]))</p>, <p>The following method uses an __________ to obtain O(n) time complexity, as using append() instead would result in O(n^2)</p><p></p><p>;; reverse : ListOf&lt;X&gt; -&gt; ListOf&lt;X&gt; </p><p>;; Answers items from `input`, in reverse order. </p><p>(define (reverse xs) </p><p> (local [</p><p> ;; helper : ListOf&lt;X&gt; ListOf&lt;X&gt; -&gt; ListOf&lt;X&gt; </p><p> ;; Prepends `remaining` in reverse order onto `reversed`. 👀 </p><p> ;; INVARIANT: (append (reverse remaining) reversed) === (reverse xs)</p><p> (define (helper remaining reversed) </p><p> (cond </p><p> [(null? remaining) reversed] </p><p> [(cons? remaining) (helper (rest remaining) </p><p> (cons (first remaining) reversed))]))] </p><p>(helper xs '()))) </p>, <p>Differences between natural/structural and generative recursion</p>, <p>Examples function values</p>, <p>Do function values have applicable templates?</p>, <p>What is the '&lt;X&gt;' called in a function value such as Predicate&lt;X&gt; or List&lt;X&gt;?</p>, <p>Higher-Order Utilities: definition</p>, <p>What is the return type of : </p><p></p><p>(define (compose g f)</p><p> (lambda (a) (g (f a))))</p>, <p>Fill in the blank:</p><p></p><p>;; filter : Predicate&lt;X&gt; ListOf&lt;X&gt; -&gt; ListOf&lt;X&gt; \</p><p>(check-expect (filter (lambda (n) (&lt; n 5)) (list 1 7 4 9 8 2 6 5 3))</p><p> ???????)</p>, <p>Fill in the blank:</p><p></p><p>(check-expect (map number-&gt;string (list 1 2 3 4 5)) </p><p> ????????)</p>, <p>Fill in the blank:</p><p></p><p>(check-expect (foldr + 0 (list 1 2 3 4 5)) </p><p> ????????)</p>, <p>foldr general signature</p>, <p>foldr starts at the _____ of the input list, while foldl starts at the _____</p>, <p>In Java, a simple “structure” data definition turns into a standalone _____ _____</p>, <p>Is interacting with an external file a side-effect?</p>, <p>Total function definition</p>, <p>Key advantage of functional programming</p>, <p>Interpreter general signature</p>, <p>Define context</p>, <p>Dynamic vs static/lexical scope</p>, <p>Define soundness</p>, <p>Define completeness</p>, <p>Readable is usually covariant, writeable is usually contravariant, readable and writeable is usually invariant</p>, <p>Define closure</p>, <p>In Java, each “itemization” turns into a ______ ________, with one implementing _______ type per branch.</p>, <p>Itemization in Java: example</p>, <p>Notation for the Temperature data definition example, in Java</p>, <p>Methods instead of functions: features</p>, <p>In Java, often dynamic dispatch is used instead of ______ _______</p>, <p>What should you do any time a function involves an <em>externally observable</em> side effect?</p>, <p>Java's “lambda expressions” are automatically converted to <em>_________ _______ _________ </em>by the compiler</p>, <p>Java “lambda expressions” examples</p>, <p>Java: a lambda expression can take on any interface or class type with <em>a _______ _________ _________</em></p>, <p>Syntax, semantics and pragmatics definition</p>, <p>Abstract syntax definition</p>, <p>Concrete syntax definition</p>, <p>Two (sub)programs, <em>P</em> and <em>Q</em>, are equivalent iff</p>, <p>Syntactic equivalence vs semantic equivalence</p>, <p>Concrete syntax is _______ into abstract syntax, which is then _______ into values</p>, <p>Notes how to design interpreters</p>, <p>Tests for partial functions must trigger the documented ____________</p>, <p>Notes dynamic scope</p>, <p>SMoL and SImPl</p>, <p>(foldl (lambda (x n) (+ 1 n)) 0 (1, 2, 3)):</p><p>0 is the ____________ //// </p><p>x is the ____________ ////</p><p>n is the ____________ ////</p>, <p>For <em>correct</em> functions, the interpreter saves the ________ on _________ creation</p>, <p>Notes primitives</p>, <p>Does java use: static dispatch? or dynamic dispatch?</p>, <p>Dynamic dispatch definition</p>, <p>Delegation definition</p>, <p>Define orthogonal features</p>, <p>Define implementing language</p>, <p>Define type system</p>, <p>Type safety definition</p>, <p>Translate ⊢ To english</p>, <p>Translate ':' to english (type systems)</p>, <p>Theorem for type soundness</p>, <p>Soundness vs completeness</p>, <p>Non-theorem: completeness</p>, <p>Run-time checks fill in ________ _______ not covered or coverable by the type system.</p>, <p>RTTI definition</p>, <p>Are run-time type checks sound? Complete?</p>, <p>A judgement is notation for ...</p>, <p>Translate Γ to english</p>, <p>Translate to english: Γ ⊢ E ok</p>, <p>Top and bottom part of inference rules: names </p>, <p>A rule with 0 antecedents is called an _______</p>, <p>Translate to english: Γ ⊢ x : Γ(x)</p> flashcards
PPL

PPL

  • Programs _______ their inputs

    interpret

  • Define pure functions

    - You know exactly what the programming is doing from the code (no hidden state)

    - Deterministic (same inputs always give same output)

    - Output doesn't vary based on local static variables, non-local variables, etc.

    - No side effects (no mutation of local static variables, non-local variables, mutable reference arguments or input/output streams)

  • Is this function pure?

    Is this function pure?

    No

  • Is this function pure?

    Is this function pure?

    Yes

  • Total functional program definition

    Everything terminates!

    Total functions always halt

  • "Well typed programs cannot _________"

    Go wrong

  • Type system purpose/function

    Type systems rule out certain kinds of error that might happen at runtime

    They do their job before runtime even starts

  • Sound type system meaning

    if the type system accepts the program, the program will not “go wrong”...

  • Type systems are usually _______ but _________

    sound, incomplete

  • Incomplete type system meaning

    sometimes “good” programs are rejected by the type system

  • Define compositional reasoning

    the meaning or behavior of a composite structure (the "whole") can be entirely determined by the meanings or behaviors of its parts and how they are combined. meaning(whole)=f(meaning(parts)) (with the f being a composite function)

  • Define continuation

    A continuation is a higher-order function that takes the result of the current computation as input and describes the "next step."

  • Functional core, imperative shell definition

    The functional bit:

    Easy unit testing, easy reasoning about correctness

    Fearless concurrency

    Build up programs from small pieces

    The imperative bit:

    Easy integration testing

    Modular connections to the outside world

  • Atomic data examples

    Numbers, strings, images (!), symbols, booleans, ...

  • 6 steps in design recipe

    Problem analysis + data definitions

    Signature + purpose statement + header

    Worked functional examples

    Choose a function template

    Function definition

    Testing

  • Template definition

    An abstraction describing a pattern of code used to analyze a value conforming to a particular data definition

  • Templates have _______ (the “...” parts), and an ________ (pieces of the inputs that are likely to be useful)

    holes, inventory

  • Sum types vs product types

    Sum types: A or B or C

    Called a "sum" type because or behaves algebraically like addition.

    HtDP Enumerations and intervals are sum types

    Product types: A and B and C

    Eg tuples, records, structures

    Called a "product" type because and behaves algebraically like multiplication.

    HtDP Structures are product types

  • Enumeration definition/example

    A finite number of specific values to choose from.

    Template uses cond or match.

    ; A TrafficLight is one of the following Strings:

    ; – "red" ; – "green" ; – "yellow"

    ; interpretation the three strings represent the three

    ; possible states that a traffic light may assume

    (define (F traffic-light)

    (cond [(string=? traffic-light "red") ...]

    [(string=? traffic-light "green") ...]

    [(string=? traffic-light "yellow") ...]))

  • Interval definition/example

    Groups of elements that satisfy some specific property.

    Template uses cond with predicates.

    ;; A Temperature can be split into bands corresponding to comfort levels: ;; - less than 0 °C ;; - at or more than 0 °C, but less than 10 °C ;; - at or more than 10 °C, but less than 22 °C ;; - at or more than 22 °C ;; F : Temperature -> ? (define (F temp) (cond [(< temp 0) ... temp ...] [(and (>= temp 0) (< temp 10)) ... temp ...] [(and (>= temp 10) (< temp 22)) ... temp ...] [(>= temp 22) ... temp ...]))
  • Itemization definition/example

    Itemizations generalize Intervals and Enumerations.

    Template uses cond or match.

    ;; A MissionTime is one of ;; - 'preflight, denoting the time up to launch ;; - a NonNegativeNumber, denoting seconds since launch (define (F mission-time) (cond [(equal? mission-time 'preflight) ...] [(number? mission-time) ... mission-time ...])) (define (F mission-time) (match mission-time ['preflight ...] [seconds ... seconds ...]))
  • Structure definition/example

    A named collection of (sub-)values called fields.

    Template uses field accessor functions e.g. movie-title, ...

    Structure predicate also available e.g. movie?

    ;; A Movie is a (make-movie String String Number), representing

    ;; an entry in a movie database, with fields

    ;; - title, a String, the movie's title; ;; - director, a String, the name of its director; ;; - year, a Number, the year of its release.

    (define-struct movie [title director year])

    ;; Example: (make-movie "The Fellowship of the Ring" "Peter Jackson" 2001)

  • Itemization of structure: definition/example

    In algebra-speak, sums of products; an “or” of “ands”.

    Many different alternatives, each of which is (in general) a structure.

    ;; A DisplayShape represents a shape to draw on the screen, and is one of

    ;; - a (make-rectangle Size Size), a rectangle of given width and height;

    ;; - a (make-circle Size), a circle of given radius; or

    ;; - a (make-line Size Angle), a line of the given length and angle.

    (define-struct rectangle (width height))

    (define-struct circle (radius))

    (define-struct line (size angle))

    ;; A Size is a Number representing a pixel count.

    ;; An Angle is a Number representing an angle in degrees.

    ;; F : DisplayShape -> ?

    (define (F ds)

    (cond [(rectangle? ds) ... (rectangle-width ds) ... (rectangle-height ds) ...]

    [(circle? ds) ... (circle-radius ds) ...]

    [(line? ds) ... (line-size ds) ... (line-angle ds) ...]))

  • Self-referential data: definition/explanation

    Everywhere that a data definition has self-reference,

    the template (and your function) has recursion,

    and you prove correctness by induction.

  • Natural recursion definition

    Recursion exactly where self-reference occurs

  • Lists, self referential data definition+template in racket

    ;; A ListOf<X> is one of

    ;; - '(), the empty list

    ;; - (cons X ListOf<X>)

    ;; TEMPLATE (define (F xs)

    (cond

    [(null? xs) ...]

    [(cons? xs) ... (first xs) ... (F (rest xs)) ...]))

    ;; where: ;; first : (cons X ListOf<X>) -> X

    ;; rest : (cons X ListOf<X>) -> ListOf<X>

    Racket offers an abbreviation for uses of cons and '():

    (list a b ... z) =    (cons a (cons b ... (cons z '())...))

  • Generative recursion definition/function

    Fully general recursion that doesn't have to fit the very specific recursion patterns of structural/natural recursion.

    Generate subproblems that are still smaller but are not an immediate structural subproblem

    Requirement: termination argument

  • Natural recursion is also called ________ recursion

    Structural

  • What is the time complexity of the following function:

    ;; reverse : ListOf<X> -> ListOf<X>

    ;; Answers items from `input`, in reverse order.

    (define (reverse xs)

    (cond

    [(null? xs) '()]

    [(cons? xs) (append (reverse (rest xs)) (cons (first xs) '()))]))

    O(n^2), because append() is O(n)

  • The following method uses an __________ to obtain O(n) time complexity, as using append() instead would result in O(n^2)

    ;; reverse : ListOf<X> -> ListOf<X>

    ;; Answers items from `input`, in reverse order.

    (define (reverse xs)

    (local [

    ;; helper : ListOf<X> ListOf<X> -> ListOf<X>

    ;; Prepends `remaining` in reverse order onto `reversed`. đź‘€

    ;; INVARIANT: (append (reverse remaining) reversed) === (reverse xs)

    (define (helper remaining reversed)

    (cond

    [(null? remaining) reversed]

    [(cons? remaining) (helper (rest remaining)

    (cons (first remaining) reversed))]))]

    (helper xs '())))

    Accumulator

  • Differences between natural/structural and generative recursion

    In structural recursion, the subproblem is directly tied to a smaller structural piece of the input. Eg. data definition has a self reference

    In generative recursion, the subproblem is created through computation and may not align with the input's structure.

    Eg. quicksort: The key is that the two sub lists are generated by the algorithm

    rather than being directly derived from the structure of the list (e.g., first and

    rest). The subproblems aren't pre-determined by the data's construction but

    are dynamically computed.

  • Examples function values

    ;; A Predicate<X> is a (X -> Boolean).

    ;; A BinaryOperator is a (Number Number -> Number).

  • Do function values have applicable templates?

    No

  • What is the '<X>' called in a function value such as Predicate<X> or List<X>?

    A generic. It means the function works with generic/non-specified type(s).

  • Higher-Order Utilities: definition

    Functions that take other functions as input or return functions as output

  • What is the return type of :

    (define (compose g f)

    (lambda (a) (g (f a))))

    Function (that takes 'a' as an input parameter)

  • Fill in the blank:

    ;; filter : Predicate<X> ListOf<X> -> ListOf<X> \

    (check-expect (filter (lambda (n) (< n 5)) (list 1 7 4 9 8 2 6 5 3))

    ???????)

    (list 1 4 2 3)

  • Fill in the blank:

    (check-expect (map number->string (list 1 2 3 4 5))

    ????????)

    (list "1" "2" "3" "4" "5")

  • Fill in the blank:

    (check-expect (foldr + 0 (list 1 2 3 4 5))

    ????????)

    15

  • foldr general signature

    ;; foldr : (X Y -> Y) Y ListOf<X> -> Y

    (X Y -> Y) is a combiner function: It takes an element of the list (X) and an accumulated result (Y) and returns a new result (Y).

    Y is the initial value for the accumulation (also called the base case).

    ListOf<X> is the input list.

    Returns a final value of type Y.

  • foldr starts at the _____ of the input list, while foldl starts at the _____

    end, start

  • In Java, a simple “structure” data definition turns into a standalone _____ _____

    record class

  • Is interacting with an external file a side-effect?

    Yes, it breaks the purity of a function

  • Total function definition

    Function that is defined/terminates for all possible inputs mentioned in its signature

  • Key advantage of functional programming

    Less unpredictability

  • Interpreter general signature

    ;; interp : Expression -> Value

  • Define context

    Refers to the environment or scope in which an expression or function is evaluated.

    Includes bindings of variables to their values or functions.

  • Dynamic vs static/lexical scope

    Static: the binding of a variable (i.e., what the variable refers to) is determined by the program's structure, specifically by where the variable is declared in the source code. This decision is made at compile time.

    Dynamic: the binding of a variable is determined by the program's execution context (runtime stack) rather than its syntactic structure.

  • Define soundness

    Every program that passes the system's checks is guaranteed to be free of certain kinds of errors (e.g., type errors, logical contradictions).

    'No false negatives': the system never incorrectly claims that something is error-free when it isn't.

  • Define completeness

    Every program or statement that is actually correct passes the system's checks.

    'No false positives'

  • Readable is usually covariant, writeable is usually contravariant, readable and writeable is usually invariant

    FILL IN

  • Define closure

    A pairing of a function and the environment needed to execute/complete it

  • In Java, each “itemization” turns into a ______ ________, with one implementing _______ type per branch.

    sealed interface, record

  • Itemization in Java: example

    A Maybe<X> represents an optional X. It is one of a None(), representing absence, or a Some(X value), representing presence.

    /** A Maybe<X> represents ... */ public sealed interface Maybe<X> permits None, Some {} /** A None<X> represents an absent X. See Maybe<X> */ public record None<X>() implements Maybe<X> {} /** A Some<X> represents a present X. See Maybe<X> */ public record Some<X>(X value) implements Maybe<X> {}
  • Notation for the Temperature data definition example, in Java

    // A Temperature is a `double` representing temperature in degrees Celsius (°C). ...

    /** Get the active Temperature. */

    public static double /*Temperature*/ activeTemperature();

  • Methods instead of functions: features

    Use dynamic dispatch in place of the outermost layer of pattern-matching (instead of a switch case to determine type, use polymorphism)

    Signature can make use of keyword this and the implicit self type

    Purpose statement goes on the declaration in the interface

    One implementation method in each of the classes in the interface's permits clause

    Eg.

    public sealed interface Maybe<X> permits None, Some {

    X valueOrDefault(X d);

    }

    public record None<X>() implements Maybe<X> {

    public X valueOrDefault(X d) { return d; }

    }

    public record Some<X>(X value) implements Maybe<X> {

    public X valueOrDefault(X d) { return this.value; }

    }

  • In Java, often dynamic dispatch is used instead of ______ _______

    pattern matching

  • What should you do any time a function involves an externally observable side effect?

    Add a comment about it in its signature.

    Eg:

    /** Append items in `this` to `xs`. SIDE EFFECT: mutates `xs`. */

    void appendTo(java.util.List<X> xs);

  • Java's “lambda expressions” are automatically converted to _________ _______ _________ by the compiler

    Anonymous inner classes

  • Java “lambda expressions” examples

    @Test void test_simple_functions() {

    java.util.function.Function<String, String> f = (String s) -> "hello, " + s;

    java.util.function.Function<String, String> g = (s) -> "hello, " + s;

    java.util.function.Function<String, Integer> h = (s) -> s.length();

    assertEquals(f.apply("world"), "hello, world");

    assertEquals(g.apply("world"), "hello, world");

    assertEquals(h.apply("world").intValue(), 5);

    }

    public class Main { public static void main(String[] args) { ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(5); numbers.add(9); numbers.add(8); numbers.add(1); Consumer<Integer> method = (n) -> { System.out.println(n); }; numbers.forEach( method ); } }
  • Java: a lambda expression can take on any interface or class type with a _______ _________ _________

    single abstract method

    Eg:

    public interface MyFunction { int stringToInt(String s); }public class MainClass { public static void main(String[] args) { MyFunction f = (s) -> s.length(); String s = "test"; System.out.println(f.stringToInt(s)); } }
  • Syntax, semantics and pragmatics definition

    Syntax: how to write a valid program

    Semantics: what a program means

    Pragmatics: the human context in which a language and its programs exists

    Tooling: IDEs, foreign-function interfaces, debuggers, syntax highlighting, linters, package management tools...

    Context of Application: Who uses it? What for? which domain (medicine, engineering, graphic design, accountancy...)? Is it usable? Is it ergonomic? Is it error-prone? Is it safe?

  • Abstract syntax definition

    a data definition describing "valid" programs

  • Concrete syntax definition

    a grammar mapping text to abstract syntax

  • Two (sub)programs, P and Q, are equivalent iff

    For all contexts C[·] (a context is a program with a “hole”),

    eval(C[P]) = eval(C[Q])

    This says: in every context I could place P in, I will get exactly the same result and exactly the same side effects as if I put Q in that context.

  • Syntactic equivalence vs semantic equivalence

    Two statements are syntactically equivalent if we can reach either one from the other using only mathematical rules and not interpreting the statements at any point. Parallelly, if two statements are semantically equivalent, we can reach either one from the other by interpreting their meanings.

  • Concrete syntax is _______ into abstract syntax, which is then _______ into values

    parsed, interpreted

  • Notes how to design interpreters

    ;; A Program is an Expression.

    ;; An Expression is one of:

    ;; - an (app Expression Expression), a function call

    ;; - a (var Symbol), a variable reference

    ;; - a (fun Symbol Expression), a function definition

    ;; interp : Expression -> Value

  • Tests for partial functions must trigger the documented ____________

    exceptions

  • Notes dynamic scope

    The decision to let control flow determine binding is called dynamic scope. It is the one unambiguously wrong design decision in programming languages

    We can't be sure what a variable name means!

    Neither can our IDE

    Neither can our compiler - noncompositional

    All bets are off

    Tremendously error prone

  • SMoL and SImPl

    Standard Model of Languages

    Standard Implementation Plan (interpreters)

  • (foldl (lambda (x n) (+ 1 n)) 0 (1, 2, 3)):

    0 is the ____________ ////

    x is the ____________ ////

    n is the ____________ ////

    initial value of the accumulator

    item from the list input into the function on each iteration

    value of the accumulator

  • For correct functions, the interpreter saves the ________ on _________ creation

    environment, function

  • Notes primitives

    Programs start with an environment that contains the primitives, instead of an empty one

  • Does java use: static dispatch? or dynamic dispatch?

    dynamic dispatch (not scope)

  • Dynamic dispatch definition

    selecting which implementation of a polymorphic operation (method or function) to call at run time (as opposed to compile time, which is called static dispatch)

  • Delegation definition

    an object forwards a method call or behavior request to another object, called its delegate, typically used to reuse behavior or share functionality dynamically.

    Example: In prototype-based programming, an object inherits methods from its prototype through delegation.

  • Define orthogonal features

    Features that don't overlap with each other

  • Define implementing language

    The programming language used to build or implement another language

  • Define type system

    a tractable syntactic method for proving the absence of certain program behaviors

  • Type safety definition

    A type safe language is one that statically protects its own abstractions.

    No untrapped errors. Prevents illegal operations.

  • Translate ⊢ To english

    Under the given context

  • Translate ':' to english (type systems)

    Is of type

  • Theorem for type soundness

    If  âŠ˘e:Ď„ then either eval(e)=v and  âŠ˘v:Ď„, or eval(e) yields an exception, or eval(e) does not halt.

    If the type system checks expression

    e, and shows that it has type

    Ď„, then evaluation of

    e either yields a value

    v that is also of type

    Ď„, or yields an error/exception, or never answers a value.

  • Soundness vs completeness

    Sound: If the system claims property P, then P is really true.Complete: If P really is true, then system claims P.

    Sound: proof implies truth; Complete: truth implies proof

  • Non-theorem: completeness

    If eval(e)=v and  âŠ˘v:Ď„, then  âŠ˘e:Ď„.

  • Run-time checks fill in ________ _______ not covered or coverable by the type system.

    Safety gaps

  • RTTI definition

    RTTI = Run-time “type” information: not a “type” in the type-system sense — it's metadata about a value: a label, available at runtime, that describes some aspects of the value

  • Are run-time type checks sound? Complete?

    Both

  • A judgement is notation for ...

    An n-ary relation.

    Unary example: ⊢ E ok (E is ok)

  • Translate Γ to english

    Context

  • Translate to english: Γ ⊢ E ok

    the context Γ proves that E is 'ok'

  • Top and bottom part of inference rules: names

    Antecedent, consequent

  • A rule with 0 antecedents is called an _______

    Axiom

  • Translate to english: Γ ⊢ x : Γ(x)

    X is of the type that the type environment maps it to