Title: | Dynamic Programming Domain-Specific Language |
---|---|
Description: | A domain-specific language for specifying translating recursions into dynamic-programming algorithms. See <https://en.wikipedia.org/wiki/Dynamic_programming> for a description of dynamic programming. |
Authors: | Thomas Mailund [aut, cre] |
Maintainer: | Thomas Mailund <[email protected]> |
License: | GPL-3 |
Version: | 0.1.1 |
Built: | 2024-11-03 03:34:39 UTC |
Source: | https://github.com/mailund/dynprog |
This function parses a dynamic programming recursion expression and evaluates it, returning the table that the recursions specify.
recursion %where% ranges
recursion %where% ranges
recursion |
Specification of the dynamic programming recursion. |
ranges |
Specification of the index-ranges the recursion should compute values over. |
A filled out dynamic programming table.
# Fibonnaci numbers fib <- { F[n] <- 1 ? n <= 2 F[n] <- F[n-1] + F[n-2] } %where% { n <- 1:10 } fib # Edit distance x <- c("a", "b", "c") y <- c("a", "b", "b", "c") edit <- { E[1,j] <- j - 1 E[i,1] <- i - 1 E[i,j] <- min( E[i - 1,j] + 1, E[i,j - 1] + 1, E[i - 1,j - 1] + (x[i - 1] != y[j - 1]) ) } %where% { i <- 1:(length(x) + 1) j <- 1:(length(y) + 1) } edit
# Fibonnaci numbers fib <- { F[n] <- 1 ? n <= 2 F[n] <- F[n-1] + F[n-2] } %where% { n <- 1:10 } fib # Edit distance x <- c("a", "b", "c") y <- c("a", "b", "b", "c") edit <- { E[1,j] <- j - 1 E[i,1] <- i - 1 E[i,j] <- min( E[i - 1,j] + 1, E[i,j - 1] + 1, E[i - 1,j - 1] + (x[i - 1] != y[j - 1]) ) } %where% { i <- 1:(length(x) + 1) j <- 1:(length(y) + 1) } edit
This function takes the ranges
and recursions
of a specification and
evaluate the dynprog expression, returning a filled out dynamic programming
table.
eval_recursion(ranges, recursions)
eval_recursion(ranges, recursions)
ranges |
The ranges specification |
recursions |
The recursions specification |
The filled out dynamic programming table
We generally assume that patterns are on the form table[exprs]
where table
is the name of the dynamic programming table. This
function extract that name.
get_table_name(patterns)
get_table_name(patterns)
patterns |
The patterns used in the recursion. |
The table part of the pattern.
Takes the full dynprog expression and construct a list of condition tests for each component of the recursion.
make_condition_checks(ranges, patterns, conditions, recursions)
make_condition_checks(ranges, patterns, conditions, recursions)
ranges |
The ranges specifications |
patterns |
The patterns specifications |
conditions |
The conditions specifications |
recursions |
The recursions specification |
A list of calls, one per recursion, for testing conditions.
Takes a pattern from the DSL and make a comparison of the pattern specification against range variables.
make_pattern_match(pattern, range_vars)
make_pattern_match(pattern, range_vars)
pattern |
An expression on the form |
range_vars |
A list of the variables used in the ranges. |
An expression that tests pattern
against range_vars
.
This function calls make_pattern_match()
for each pattern in patterns
and return a list of all the pattern test expressions.
make_pattern_tests(patterns, range_vars)
make_pattern_tests(patterns, range_vars)
patterns |
A list of the patterns used in a recursion. |
range_vars |
The variables used in the ranges. |
A list of pattern check expressions.
This function creates an if
-statement for testing if a case can be
applied.
make_recursion_case(test_expr, value_expr, continue)
make_recursion_case(test_expr, value_expr, continue)
test_expr |
The expression that must be true for the case to be applied |
value_expr |
The value to compute if the test is true |
continue |
The next case to check if this one isn't true |
An if
-statement for checking and potentially evaluating one case.
if
-statements of a recursion.String together the case if
-statements of a recursion.
make_update_expr(ranges, patterns, conditions, recursions)
make_update_expr(ranges, patterns, conditions, recursions)
ranges |
The ranges specification |
patterns |
The patterns specification |
conditions |
The conditions specifications |
recursions |
The recursions specification |
A series of if
-else
-statements for evaluating a recursion.
Parses the ranges and return a list of index variables an the values they should iterate over. The ranges are returned as a list with the range variables as its names and the range values as the list components.
parse_ranges(ranges)
parse_ranges(ranges)
ranges |
The quosure wrapping the input to the specification. |
A parsed specification for ranges.
Parse the recursion part of an expressions.
parse_recursion(recursion)
parse_recursion(recursion)
recursion |
The quosure wrapping the recursion of the specification. |
The parser return a list with the following components:
recursion_env: The environment in which expressions should be evaluated.
patterns: A list of patterns, one per recursion case.
conditions: A list of conditions, one per recursion case.
recursions: A list of expressions, one per recursion case.
A parsed specification for recursions.