1- # Syntax
1+ # MeTTa language specification
22
3- ## S-expression grammar
3+ This document covers MeTTa language specification implemented by
4+ hyperon-experimental interpreter.
5+
6+ ## Syntax
7+
8+ ### S-expression grammar
49
510This is the S-expression grammar of the MeTTa language. The program is
611consisted of the atoms prefixed or not prefixed by the ` ! ` sign.
@@ -49,7 +54,7 @@ the `<token regexp>` from the tokenizer then a grounded atom is constructed by
4954argument. For instance it is possible instantiating the integer grounded atoms
5055by adding the pair ` ([0-9]+, <int parser>) ` into the tokenizer.
5156
52- ## Special expressions
57+ ### Special expressions
5358
5459This section lists atoms with some special meaning in the interpreter. The
5560expressions listed below are not the only expressions possible when the special
@@ -63,7 +68,7 @@ other languages. In MeTTa the programmer still can use these symbols in an
6368unusual context but it should be noted that the interpreter has a special
6469meaning for them.
6570
66- ### Function expression
71+ #### Function expression
6772
6873The ` = ` atom is used to define a function expression in the following form:
6974```
@@ -76,7 +81,7 @@ For example the `if` function can be defined as following:
7681(= (if False $then $else) $else)
7782```
7883
79- ### Type assignment
84+ #### Type assignment
8085
8186The ` : ` atom is used to define the type of the atom:
8287```
@@ -88,7 +93,7 @@ For example the `if` function could have the following type definition:
8893(: if (-> Bool Atom Atom $t))
8994```
9095
91- ### Function type
96+ #### Function type
9297
9398The ` -> ` atom is used to introduce a type of a function:
9499```
@@ -100,7 +105,7 @@ For example the type of the `if` function:
100105(-> Bool Atom Atom $t)
101106```
102107
103- ### Elementary types
108+ #### Elementary types
104109
105110There are the number of symbols which are used as the basic types:
106111- ` Type ` - the type of any type
@@ -117,7 +122,7 @@ These types plus `Atom` are referred below as meta-types. `Atom` is a special
117122meta-type which matches any atom and used for changing the order of evaluation.
118123
119124
120- ### Special function results
125+ #### Special function results
121126
122127- ` Empty ` - the function doesn't return any result, it is different from the
123128 void or unit result in other languages
@@ -147,7 +152,7 @@ it could be used to introduce custom type constructors. This could be
147152implemented by making an interpreter treating any expression which has
148153` ErrorType ` type as an error.
149154
150- ### Minimal MeTTa instructions
155+ #### Minimal MeTTa instructions
151156
152157The minimal MeTTa is an attempt to create an assembly language for MeTTa.
153158Minimal MeTTa related atoms are listed here because while they are not a part
@@ -192,7 +197,7 @@ Minimal MeTTa instructions:
192197(* ) ` call-native ` instruction cannot be called from a MeTTa program, but it can
193198be returned by a grounded function for the further evaluation.
194199
195- # Interpretation
200+ ## Interpretation
196201
197202MeTTa program is interpreted atom by atom. If an atom is not prefixed by ` ! ` it
198203is added into the atomspace of the top module. Hierarchy of modules is
@@ -230,9 +235,9 @@ Two special functions are not specified: `match_atoms` and `merge_bindings`.
230235They will be specified in the following sections. For now one can rely on
231236intuitive understanding how two-side unification and bindings merging works.
232237
233- ## Evaluation
238+ ### Evaluation
234239
235- ### Evaluate atom (metta)
240+ #### Evaluate atom (metta)
236241
237242```
238243Input:
@@ -267,7 +272,7 @@ else:
267272 return $error
268273```
269274
270- ### Cast types (type_cast)
275+ #### Cast types (type_cast)
271276
272277```
273278Input:
@@ -290,7 +295,7 @@ for $t in $types:
290295return [((Error $atom (BadType $type $t)), $bindings) for $t in $no_match]
291296```
292297
293- ### Match types (match_types)
298+ #### Match types (match_types)
294299
295300```
296301Input:
@@ -308,7 +313,7 @@ if $type1 == %Undefined% or $type1 == Atom
308313return match_atoms($type1, $type2)
309314```
310315
311- ### Interpret expression (interpret_expression)
316+ #### Interpret expression (interpret_expression)
312317
313318```
314319Input:
@@ -350,7 +355,7 @@ if <$actual_types contains non function types or %Undefined% type>:
350355return $tuples + $errors
351356```
352357
353- ### Interpret tuple (interpret_tuple)
358+ #### Interpret tuple (interpret_tuple)
354359
355360```
356361Input:
@@ -376,7 +381,7 @@ for ($h, $hb) in metta($head, %Undefined%, $space, $bindings):
376381return $result
377382```
378383
379- ### Check if function type is applicable (check_if_function_type_is_applicable)
384+ #### Check if function type is applicable (check_if_function_type_is_applicable)
380385
381386```
382387Input:
@@ -421,7 +426,7 @@ else:
421426 return Ok($results)
422427```
423428
424- ### Check argument type (check_argument_type)
429+ #### Check argument type (check_argument_type)
425430
426431```
427432Input:
@@ -444,7 +449,7 @@ for $t in $actual_types:
444449return $result
445450```
446451
447- ### Interpret function (interpret_function)
452+ #### Interpret function (interpret_function)
448453```
449454Input:
450455- $atom - atom to be evaluated
@@ -472,7 +477,7 @@ for ($h, $hb) in metta($op, $op_type, $space, $bindings):
472477return $result
473478```
474479
475- ### Interpret arguments (interpret_args)
480+ #### Interpret arguments (interpret_args)
476481
477482```
478483Input:
@@ -501,7 +506,7 @@ for ($h, $hb) in metta($atom, $type, $space, $bindings):
501506return $result
502507```
503508
504- ### Call MeTTa expression (metta_call)
509+ #### Call MeTTa expression (metta_call)
505510
506511```
507512Input:
@@ -542,7 +547,7 @@ else:
542547 return $results
543548```
544549
545- ## Matching
550+ ### Matching
546551
547552This section explains the atom matching algorithm. The result of matching is
548553list of variable binding sets. If two atoms matched don't contain grounded
@@ -565,7 +570,7 @@ explain how binding set is modified. For example `$bindings - { $b <- $b_value
565570} + { $a = $b }` means one need remove relation ` $b <- $b_value` from $bindings
566571and add relation ` $a = $b ` .
567572
568- ### Match atoms (match_atoms)
573+ #### Match atoms (match_atoms)
569574
570575```
571576Input:
@@ -607,7 +612,7 @@ else:
607612return filter(lambda $b: <$b doesn't have variable loops>, $results)
608613```
609614
610- ### Merge bindings (merge_bindings)
615+ #### Merge bindings (merge_bindings)
611616
612617```
613618Input:
@@ -626,7 +631,7 @@ for $rel in <set of "assign value to var" or "vars are equal" relations of $righ
626631return $result
627632```
628633
629- ### Add variable binding to binding set (add_var_binding)
634+ #### Add variable binding to binding set (add_var_binding)
630635
631636```
632637Input:
@@ -649,7 +654,7 @@ else:
649654 return $result
650655```
651656
652- ### Add variable equality to binding set (add_var_equality)
657+ #### Add variable equality to binding set (add_var_equality)
653658
654659```
655660Input:
0 commit comments