Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First attempt at a style guide #3408

Merged
merged 13 commits into from
Oct 16, 2024
1,028 changes: 989 additions & 39 deletions packages/ai/files/squiggleDocs.md

Large diffs are not rendered by default.

84 changes: 46 additions & 38 deletions packages/ai/squiggleLibraries/ozziegooen/helpers.squiggle
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ expect = sTest.expect
@hide
describe = sTest.describe

@hide
styleSquiggleCode(code) = "
```squiggle
" + code + "
```"

@doc(
"
round(num, n)

Rounds the number `num` to `n` decimal places.
round(num, n) -> string

Example:
round(3.14159, 2) -> \"3.14\"
"
Rounds the number `num` to `n` decimal places." +
styleSquiggleCode("round(3.14159, 2) -> \"3.14\"")
)
export round(num, n) = {
export round(num, n) = if typeOf(num) != "Number" then throw(
"Was given a " + typeOf(num) + ", must be given a Number"
) else {
asString = String.make(num)
splitString = String.split(asString, "")
if List.findIndex(splitString, {|r| r == "e"}) != -1 then {
Expand Down Expand Up @@ -63,19 +68,20 @@ roundTests = describe(
"rounds a negative number",
{|| expect(round(-2.7182, 2)).toBe("-2.71")}
),
test(
"throws when given distribution",
{|| expect({|| round(1 to 10, 2)}).toThrowAnyError()}
),
]
)

@doc(
"
formatTime(hours)
formatTime(hours) -> string

Converts a number of hours to a formatted string indicating time in
seconds, minutes, hours, days, months, or years.

Example:
formatTime(1) -> \"**1** hours\"
"
seconds, minutes, hours, days, months, or years. Bolds the result." +
styleSquiggleCode("formatTime(1) -> \"**1** hours\"")
)
export formatTime(hours) = {
secondsInMinute = 60
Expand Down Expand Up @@ -129,16 +135,16 @@ formatTimeTests = describe(
)

@doc(
"## Linear or Quadratic Interpolation
```squiggle
@import('hub:ozziegooen/helpers' as h)
"## Linear or Quadratic Interpolation" +
styleSquiggleCode(
"@import('hub:ozziegooen/helpers' as h)

h.interpolate([{x: 0, y:10}, {x:10, y:20}], 'linear')(4) -> 15
h.interpolate([{x: 0, y:10}, {x:10, y:20}], 'quadratic')(4) -> 11.6
h.interpolate([{x: 0, y:10}, {x:10, y:20}], 'linear')(4) // 15
h.interpolate([{x: 0, y:10}, {x:10, y:20}], 'quadratic')(4) // 11.6

//makes a graph
foo(t:[0,30]) = h.interpolate([{x: 0, y:10}, {x:10, y:20}, {x:20, y:10}], 'quadratic')(t)
"
foo(t:[0,30]) = h.interpolate([{x: 0, y:10}, {x:10, y:20}, {x:20, y:10}], 'quadratic')(t)"
)
)
export interpolate(points, type) = {
sortedPoints = List.sortBy(points, {|f| f.x}) //TODO: Sort, somehow
Expand All @@ -163,7 +169,6 @@ export interpolate(points, type) = {
c = leftPoint.y + a * leftPoint.x ^ 2
a * x ^ 2 + b * x + c
} else { foo: "Invalid interpolate type" }

} else if x > sortedPoints[i - 1].x then sortedPoints[List.length(
sortedPoints
) -
Expand Down Expand Up @@ -230,7 +235,7 @@ interpolationTests = describe(
)

//myShape = [{ x: 4, y: 10 }, { x: 20, y: 40 }, { x: 30, y: 20 }]

@hide
plot(fn, xPoints) = Plot.numericFn(
fn,
{
Expand Down Expand Up @@ -287,6 +292,20 @@ You have to enter data in the format of x and y values, as shown below, then get
}
)

@doc(
"
distributionListRange(dists, lowQuantile, highQuantile)

Returns a range that is the minimum and maximum of the low and high quantiles of the distributions in the list." +
styleSquiggleCode(
"distributionListRange([dist1, dist2, dist3], 0.05, 0.95) -> {min: 1, max: 10}"
)
)
export distributionListRange(dists, lowQuantile, highQuantile) = {
min: Number.min(List.map(dists, {|d| Dist.quantile(d, lowQuantile)})),
max: Number.max(List.map(dists, {|d| Dist.quantile(d, highQuantile)})),
}

@startOpen
@notebook
readme = [
Expand All @@ -303,24 +322,10 @@ To use the functions from this library in your projects, import it as follows:
```
## Functions Overview

### round
Rounds a given number to a specified number of decimal places.

Example:

```squiggle
h.round(3.423, 2) // Returns: \"3.42\"
```",
### round",
Tag.getDoc(round),
"---",
"### formatTime
Converts a given number of hours into a human-readable time format, such as seconds, minutes, hours, days, months, or years.

Example:

```squiggle
h.formatTime(4.23) // Enter the number of hours and format the result
```",
"### formatTime",
Tag.getDoc(formatTime),
"---",
"### interpolate
Expand All @@ -341,4 +346,7 @@ h.interpolate([{x: 0, y: 10}, {x: 10, y: 20}], 'quadratic')(4) // Returns: 11.6
### Interpolation Calculator
This tool helps visualize and compare the results of linear and quadratic interpolations for a given set of data points. Below is an example use case integrated with the library.",
interpolationCalculator,
"---",
"### distributionListRange",
Tag.getDoc(distributionListRange),
]
4 changes: 2 additions & 2 deletions packages/ai/squiggleLibraries/ozziegooen/sTest.squiggle
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ comparisonTests = describe(
]
)

errorTests = describe(
errorTests = System.version == "0.9.6-0" ? describe(
"Error Throwing Tests",
[
test(
Expand All @@ -314,4 +314,4 @@ errorTests = describe(
{|| expect({|| throw("UnexpectedError")}).toNotThrow()}
),
]
)
) : false
2 changes: 1 addition & 1 deletion packages/ai/src/squiggle/README.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/ai/src/squiggle/squiggleLibraryContents.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/ai/src/steps/adjustToFeedbackStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Please review the code and output according to the following criteria:
7. **Edge Cases**: Ensure that edge cases are handled and add simple error handling where appropriate.
8. **Remove Redundant Comments**: Delete any comments that no longer serve a purpose (e.g., comments explaining previous changes).
9. **Sensitivity Analysis**: Do not add sensitivity analysis functions.
10. **Style Guide**: Follow the included Squiggle Style Guide.

If no adjustments are required (this should be the usual outcome), respond with:

Expand Down
20 changes: 16 additions & 4 deletions packages/ai/src/steps/fixCodeUntilItRunsStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,24 @@ function editExistingSquiggleCodePrompt(code: Code): PromptPair {
const error = codeErrorString(code);
const advice = getSquiggleAdvice(error, code.source);
const fullPrompt = `You are an expert Squiggle code debugger. Your task is solely to fix an error in the given Squiggle code.

Follow these steps:

1. Analyze the Code and Error Message: Carefully review the original code, the error message, and any provided advice.
2. Maintain Code Integrity: Fix the error while preserving the original code structure and functionality. Avoid unrelated changes.
3. Choose the Most Efficient Fix: If multiple solutions exist, implement the most straightforward and effective one.
4. Avoid Repetition of Past Mistakes: Review previous attempts (if any) and do not repeat the same errors.
1. **Comprehensive Analysis**: Carefully analyze the code and error message to identify both the immediate cause of the error and any related issues that could stem from the same root cause. Consider errors related to the logic, structure, or syntax that may not be reflected directly in the error message.

2. **Find and Fix All Instances**: If the error is found in multiple locations or is part of a larger pattern, apply the fix to **all occurrences**. Ensure that no instances of the same issue remain in the code. Make sure to read through the rest of the code after your initial fix idea, to find potential additional fixes.

3. **Reasoning and Fix Documentation**: Provide detailed reasoning for your chosen fix, including an explanation of why it is the most appropriate solution. For each fix, explain how it resolves the error and ensure that the corrected code maintains the intended functionality.

4. **Return Valid Code**: Ensure that the corrected code can execute successfully without further errors. If the fix involves structural changes, ensure the code still returns the necessary values or outputs where applicable.

5. **Reflection and Code Review**: After making changes, reflect on the solution to ensure that all parts of the code are consistent with the fix. If the fix needs to be applied elsewhere, ensure that similar corrections are made globally in the code.

### Additional Requirements:
- Use Squiggle documentation to support your solution where necessary.
- If the error message points to a location distant from the actual issue (due to limitations in error handling), carefully evaluate the context and resolve the issue where appropriate. The real issue is often a few lines away from the place the bug is thought to be.
- Often, once you make one fix, another obvious issue emerges. Think through if this will happen - if so, try to fix this now as well.
- Use SEARCH/REPLACE blocks for each fix, ensuring that they capture the necessary scope for each correction.

<original_code_with_line_numbers>
${addLineNumbers(code.source)}
Expand Down
Loading
Loading