Notes for 4-10
Database
PL/SQL
Anonymous and named PL/SQL block
- Anonymous PL/SQL block
- Need to compile every time
- Not stored
- Named PL/SQL block
- Can be stored in DB
Stored Procedure
Grammar:
Procedure ::= CREATE [OR REPLACE] PROCEDURE ProcedureName '(' Parameters ')' (AS|IS) RETURN ReturnType DECLARE Declaration BEGIN Body END ';'
Parameters ::= Nothing | Parameter {, Parameter}
Parameter ::= ParameterName (IN|OUT|IN OUT) Type ':=' DefaultValue
- Note:
- In the parameter list,
varchar
cannot be specified with a length. - MySQL has no
AS|IS
in the procedure, andIN/OUT
is put before the parameter name. - In MySQL ,one has to change the delimiter before defining a procedure or function.
- In the parameter list,
Loop statement
- for x in [Reverse] 1…20 loop … end loop;
- while … loop … end loop; To escape from a loop:
- exit when …;
Conditional statement
- if … then … elseif … end if;
Cursor
- Used to process a set of records. PL/SQL record variable can only store one record at a time.
- Technically, it acts like an iterator, which means one cannot process multiple records at the same time(though we can store them as a whole).
- In declaration, a cursor is declared with a SELECT statement, and we can use this cursor to access the data returned by that SELECT statement
- A cursor is bind to one specific SELECT clause, that is, one cannot bind it to another SELECT statement
- The SELECT statement is and only is executed when the cursor is OPENed(see below).
- CLOSE the cursor to free the memory space. Synopsis:
- Declaration
cursor cursor_name is select statement;
- Open cursor
OPEN cursor_name;
- Get the data
FETCH cursor_name into variable;
- The cursor will move forward, just like python’s
next
function do.
- Check if there is no more entry
IF NOT cursor_name%FOUND THEN …;
- Close the cursor
CLOSE cursor_name;
- For loop Unique to Oracle. No need to open, close, or fetch from a cursor.
FOR variable in cursor_name LOOP – do something to the variable END LOOP;
Artificial Intelligence
Knowledge base && Inferrence
* Truth table
* Brute force search
* Forward chaining
* reliable
* completeness
* -> only have these two properties for the knowledge bases able to be expressed with Horn clauses
* Horn clause: a disjunction of literals with at most one unnegated literal
* E.g. {% katex %}
\neg x_1 \vee \neg x_2 \vee \cdots \vee x_n \vee u
{% endkatex %}
* Backward chaining
* To prove q, with p->q now prove p
* Avoid repeating by check if a clause is already proved or disproved
Misc
Panning derives from panorama.
4/11 Record
Programming Languages
Polymorphic Functions
- Static: cost grows exponentially with parameters
- Dynamic:
- supply a tag when calling the function
Example: Overloading Equality
- Equality was overloaded as an operator
- Make type of equality fully polymorphic
- Make equality polymorphic in a limited way
(==) :: ''t -> ''t -> Bool
,''t
is a typet
with equality- will be a static error if
t
is not Eq type
Type Classes
- Merchanism in Haskell
- Dictionary-passing style implementation [ESOP1988]
- Type class declaration – dictionary
- Name of a type class method – label in the dictionary
- Parametric overloading – passing the dictionary to the function
class Show a where |
type 'a show = {show: 'a -> string} |
Smalltalk: subtyping
-
Object -> super class -> method table
-
If interface A contains all of interface B, then A objects can also be used as B objects
- need to look up all instances(members) at runtime
Javascript has the same problem
- v8 engine: hidden class
function Person(first_name, last_name) { |
Computer Architecture
Optimizing cache
Merging write cache
DRAM and SRAM
Misc
最低だ 最低だ 最低だ