Dream Coder - Algorithm 2
Type Unification and the sample'
Function
The core of the sample'
function involves generating a program whose type unifies with a desired type
Understanding Type Unification:
Type unification is the process of making two types compatible by finding a substitution (if needed) such that both types become equal. Specifically:
Case 1: is a function type
- Here, we are expected to generate a lambda function of the form:
- The variable
is mapped to type . - The recursive step is then to generate a program body whose type unifies with
. - This is done by calling
sample'
recursively to generate a program whose type unifies with.
- This is done by calling
Case 2: is not a function type (i.e., a concrete type)
- In this case, the program must have a type
that unifies with . - The goal is to find a program that has type
such that unifies with .
Generating a Program Based on
The algorithm proceeds by generating a program from the library
-
Picking a Set of Primitives:
- We first pick a set of primitives (programs
) from the library and the environment . - These primitives must have a type
such that: - If
is a lambda function , then must unify with . - Otherwise,
must unify (be equal to) .
- If
- We first pick a set of primitives (programs
-
Handling Variables:
- Next, we need to consider variables. These are programs that exist in the environment
. - We draw a program
with probability proportional to the weight assigned to by if . - If
, we instead use , which is the -th component of , and we draw with probability proportional to .
- Next, we need to consider variables. These are programs that exist in the environment
-
Type Checking:
- Notice that the drawn program
has type . - We check if
unifies with .
- Notice that the drawn program
-
Handling Lambda Functions:
- If
is a lambda function, we retrieve its argument types using (this is done in line 24). - We then sample all the arguments required to apply
(steps 18 to 20). - Finally, we return
with all its arguments bundled into one tuple.
- If