Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
records [2021/10/26 17:07] – created sprowellrecords [2021/10/28 18:27] (current) – Added grammar for simple rules and binds. sprowell
Line 3: Line 3:
 ===== Records ===== ===== Records =====
  
-A //record// is an unordered collection of terms, where each term has an associated unique name.  The following is an example.+A //record// is an unordered collection of terms, where each term has an associated unique name given by an untyped symbol.  The following is an example.
  
-<code>+<code ->
 { name => "Fred", height => %(5,10), { name => "Fred", height => %(5,10),
   birthday => { year => 1996, month => 3, day => 21 } }   birthday => { year => 1996, month => 3, day => 21 } }
Line 11: Line 11:
  
 Note that the terms in a record can be anything, including other records. Note that the terms in a record can be anything, including other records.
 +
 +<code>
 +record = "{" ~ { untyped_symbol ~ "=>" ~ term ~ ","? }+ ~ "}"
 +</code>
  
 ===== Binds ===== ===== Binds =====
  
-A //bind// associates variables by name with terms.  It looks much like a record, except that it uses variable names instead of symbols.  The variables //cannot// have types or guards; this is for performance.+A //bind// associates variables by name with terms.  It looks much like a record, except that it uses variables instead of symbols.  The variables //cannot// have types or guards; this is for performance.
  
-<code>+<code ->
 { $x => 21, $y => ( $z -> 21 ) } { $x => 21, $y => ( $z -> 21 ) }
 </code> </code>
Line 24: Line 28:
 When a binding is applied to a term, free instances of the named variables are replaced with the associated term. When a binding is applied to a term, free instances of the named variables are replaced with the associated term.
  
-<code>+<code ->
 { $x => 21 } . iadd(17, $x) { $x => 21 } . iadd(17, $x)
 --> iadd(17, 21) --> iadd(17, 21)
Line 30: Line 34:
 </code> </code>
  
-Again, this works for free variables, only.  Lambda variables are no rewritten.+Again, this works for free variables, only.  Lambda variables, which are not free, are not rewritten.
  
-<code>+<code ->
 { $x => 21 } . ( $x -> 17 ) { $x => 21 } . ( $x -> 17 )
 --> $x -> 17 --> $x -> 17
 +</code>
 +
 +<code>
 +bind = "{" ~ { untyped_variable ~ "=>" ~ term ","? }* ~ "}"
 </code> </code>
  
 ===== Simple Rules ===== ===== Simple Rules =====
  
-A //simple rule// is very much like a bind.  In fact, a bind can be thought of as a performance-optimized special case of a simple rule.  A simple rule is a sequence of lambda expressions, in order.  Note that lambdas use the `->` arrow, where records and binds use the `=>` arrow.+A //simple rule// is a sequence of lambda expressions, in order.  Note that lambdas use the `->` arrow, where records and binds use the `=>` arrow.
  
-<code>+<code ->
 { %($x: INTEGER, $y: INTEGER) -> iadd($x,$y), { %($x: INTEGER, $y: INTEGER) -> iadd($x,$y),
   %($x: FLOAT, $y: FLOAT) -> fadd($x,$y),   %($x: FLOAT, $y: FLOAT) -> fadd($x,$y),
Line 50: Line 58:
 When a simple rule is applied to a term, each of the lambdas is tried, in order.  The result of the entire apply is the result of the first lambda that succeeds.  If no lambda succeeds, the system generates an //error// To avoid this case you can use the `ANY` pattern.  For instance, if the last lambda is `ANY -> ANY` then if no prior lambda matches, the argument is unchanged. When a simple rule is applied to a term, each of the lambdas is tried, in order.  The result of the entire apply is the result of the first lambda that succeeds.  If no lambda succeeds, the system generates an //error// To avoid this case you can use the `ANY` pattern.  For instance, if the last lambda is `ANY -> ANY` then if no prior lambda matches, the argument is unchanged.
  
-<code>+<code ->
 { %($x: INTEGER, $y: INTEGER) -> iadd($x,$y), { %($x: INTEGER, $y: INTEGER) -> iadd($x,$y),
   %($x: FLOAT, $y: FLOAT) -> fadd($x,$y),   %($x: FLOAT, $y: FLOAT) -> fadd($x,$y),
Line 58: Line 66:
 </code> </code>
  
-===== Special Form ===== +<code> 
 +simple_rule "{" ~ { lambda ~ ","? }+ ~ "}" 
 +</code>