|
6 | 6 | * Describe language grammar as functions that take a free-bound "evaluator" function param instead of data types (a "final algebra"/"object algebra")
|
7 | 7 | * ✔: Performance (no tag dispatch), allows for partial evaluation extension, and expression problem solution
|
8 | 8 | * Explanations:
|
9 |
| - * ELIU in C# https://higherlogics.blogspot.com/2008/09/mostly-tagless-interpreters-in-c.html |
10 |
| - * http://lambda-the-ultimate.org/node/4572 |
11 |
| - * Papers: |
12 |
| - http://okmij.org/ftp/tagless-final/JFP.pdf |
13 |
| - http://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf |
| 9 | + * ELIU in C# <https://higherlogics.blogspot.com/2008/09/mostly-tagless-interpreters-in-c.html> |
| 10 | + * <http://lambda-the-ultimate.org/node/4572> |
| 11 | + * Papers: |
| 12 | + <http://okmij.org/ftp/tagless-final/JFP.pdf> |
| 13 | + <http://www.cs.utexas.edu/~wcook/Drafts/2012/ecoop2012.pdf> |
14 | 14 | * Sample implementations:
|
15 | 15 | * Simple C++ example
|
16 | 16 | ````cpp
|
|
233 | 233 | }
|
234 | 234 | ````
|
235 | 235 |
|
236 |
| - * Implementations in different languages: https://i.cs.hku.hk/~bruno/oa/ |
237 |
| - * C# (2008) |
238 |
| - * Snippet: http://lambda-the-ultimate.org/node/4572#comment-72110 |
239 |
| - * More complete: http://lambda-the-ultimate.org/node/2569#comment-43805 |
240 |
| - * Expanded Version (2009): https://higherlogics.blogspot.com/2009/06/mobile-code-in-c-via-finally-tagless.html |
241 |
| - * Advanced Query language in C#: https://higherlogics.blogspot.com/2019/09/building-query-dsl-in-c.html |
242 |
| - * Snippet with Pratt parser (http://lambda-the-ultimate.org/node/4572#comment-72110) |
| 236 | + * Implementations in different languages: <https://i.cs.hku.hk/~bruno/oa/> |
| 237 | + * C# (2008) |
| 238 | + * Snippet: <http://lambda-the-ultimate.org/node/4572#comment-72110> |
| 239 | + * More complete: <http://lambda-the-ultimate.org/node/2569#comment-43805> |
| 240 | + * Expanded Version (2009): <https://higherlogics.blogspot.com/2009/06/mobile-code-in-c-via-finally-tagless.html> |
| 241 | + * Advanced Query language in C#: <https://higherlogics.blogspot.com/2019/09/building-query-dsl-in-c.html> |
| 242 | + * Snippet with Pratt parser (<http://lambda-the-ultimate.org/node/4572#comment-72110>) |
243 | 243 | * Syntax to semantic constructors
|
244 | 244 |
|
245 | 245 | ````csharp
|
|
258 | 258 |
|
259 | 259 | class MathGrammar<T> : Grammar<T>
|
260 | 260 | {
|
261 |
| - public MathGrammar(IMathSemantics<T> math) |
262 |
| - { |
263 |
| - Infix("+", 10, math.Add); Infix("-", 10, math.Sub); |
264 |
| - Infix("*", 20, math.Mul); Infix("/", 20, math.Div); |
265 |
| - InfixR("^", 30, math.Pow); Postfix("!", 30, math.Fact); |
266 |
| - Prefix("-", 100, math.Neg); Prefix("+", 100, math.Pos); |
| 261 | + public MathGrammar(IMathSemantics<T> math) |
| 262 | + { |
| 263 | + Infix("+", 10, math.Add); Infix("-", 10, math.Sub); |
| 264 | + Infix("*", 20, math.Mul); Infix("/", 20, math.Div); |
| 265 | + InfixR("^", 30, math.Pow); Postfix("!", 30, math.Fact); |
| 266 | + Prefix("-", 100, math.Neg); Prefix("+", 100, math.Pos); |
267 | 267 |
|
268 |
| - Group("(", ")", int.MaxValue); |
269 |
| - Match("(digit)", char.IsDigit, 0, math.Int); |
270 |
| - SkipWhile(char.IsWhiteSpace); |
271 |
| - } |
| 268 | + Group("(", ")", int.MaxValue); |
| 269 | + Match("(digit)", char.IsDigit, 0, math.Int); |
| 270 | + SkipWhile(char.IsWhiteSpace); |
| 271 | + } |
272 | 272 | }
|
273 | 273 | ````
|
274 | 274 |
|
|
285 | 285 | public int Pow(int lhs, int rhs) { return (int)Math.Pow(lhs, rhs); }
|
286 | 286 | public int Neg(int arg) { return -arg; }
|
287 | 287 | public int Pos(int arg) { return arg; }
|
288 |
| - public int Fact(int arg) |
289 |
| - { |
290 |
| - return arg == 0 || arg == 1 ? 1 : arg * Fact(arg - 1); |
291 |
| - } |
| 288 | + public int Fact(int arg) { return arg == 0 || arg == 1 ? 1 : arg * Fact(arg - 1); } |
292 | 289 | }
|
293 | 290 | ````
|
294 | 291 |
|
|
297 | 294 | ````csharp
|
298 | 295 | interface IEquationSemantics<T> : IMathSemantics<T>
|
299 | 296 | {
|
300 |
| - T Var(string name); |
301 |
| - T Let(T x, T value, T body); |
| 297 | + T Var(string name); |
| 298 | + T Let(T x, T value, T body); |
302 | 299 | }
|
303 | 300 | class EquationParser<T> : MathGrammar<T>
|
304 | 301 | {
|
305 |
| - public EquationParser(IEquationSemantics<T> eq) : base(eq) |
306 |
| - { |
307 |
| - Match("(ident)", char.IsLetter, 0, eq.Var); |
308 |
| - TernaryPrefix("let", "=", "in", 90, eq.Let); |
309 |
| - } |
| 302 | + public EquationParser(IEquationSemantics<T> eq) : base(eq) |
| 303 | + { |
| 304 | + Match("(ident)", char.IsLetter, 0, eq.Var); |
| 305 | + TernaryPrefix("let", "=", "in", 90, eq.Let); |
| 306 | + } |
310 | 307 | }
|
311 | 308 | ````
|
0 commit comments