Skip to content

Documentation

Lakshay edited this page Nov 11, 2023 · 6 revisions

Expr

Expressions are the fundamental units in sock. They are of two types;

  1. Sym (Symbol): x y pi
  2. Fun (Functions): pair(a, b) swap(pair(a, b))

Rule

Rule describes the transformation of left expression to right expression

Syntax

<rule_name:[a-z0-9A-Z]> := <left_expr:Expr> = <right_expr:Expr>

Example

swapPair := swap(pair(a, b)) = pair(b, a)

limit := lim(x, a, f(x)) = f(a)

Shapes

Shapes are the most important operations in sock. It transforms the expressions following the transformations mentioned in the provided rule(s). Modifiers can also be added which describes "how to transform?" or "which rule to use?" and so on.

Syntax

<expr_name:[a-z0-9A-Z]> := <expr:Expr> {
   <rule_name:[a-z0-9A-Z] | <expr_mod:Expr_Mod>;
   ...
} <mod:Mod>

Example

expr := swap(pair(a, b)) {
    swapPair | over;
} dump

_ := swap(pair(f(x), lim(h, y, f(h)))) {
    limit | all ;
    ?     | over;
} dump

Special names

  1. _ is a special expr_name. If you name your shape _, it will not store resulting expression. Meant for use-and-throw expressions which you don't want to preserve.
  2. ? is a special rule_name. If you name your rule ?, sock will apply every rule to the expression if it matches the pattern to that rule. It can only be used with the all expression modifier.

Expr_mod

  1. all: It will apply the rule recursively to the expression and every subexpression if they matches the pattern.
  2. over: It will apply the rule to the expression if it matches the pattern.

Mod

  1. void: Similar to Python's pass it does nothing but required to complete the syntax.
  2. dump: Dump the expression to stdout.
  3. $dump: Dump the value of expression to stdout.

Macros

Useful for making a shorthand for a big piece of code.

Syntax

@<macro_name:[a-z0-9A-Z]> := <anything>

Example

@add := {add | all; add0 | all;}

On of the use case for this is _ := add(@1, @2) @add $dump
@add Expands to {add | all; add0 | all;} at runtime.

Miscellaneous

Comments

# this is a comment
rule := a = a # this is an inline comment

Clone this wiki locally