Remove support for functions, as I've concluded they're unneeded.
Chris Pressey
6 years ago
84 | 84 | end |
85 | 85 | ???> IndexError |
86 | 86 | |
87 | functions | |
88 | --------- | |
89 | ||
90 | rules | |
91 | [actor(ρ)] | |
92 | ρ scratches their(ρ) head. | |
93 | [] | |
94 | end | |
95 | functions | |
96 | their(Alice) → her | |
97 | their(Bob) → his | |
98 | end | |
99 | situations | |
100 | [ | |
101 | actor(Alice), | |
102 | actor(Bob) | |
103 | ] | |
104 | end | |
105 | ||
106 | ===> Alice scratches her head. | |
107 | ===> Alice scratches her head. | |
108 | ===> Bob scratches his head. | |
109 | ===> Bob scratches his head. | |
110 | ===> Alice scratches her head. | |
111 | ||
112 | 87 | no need for functions |
113 | 88 | --------------------- |
114 | 89 |
28 | 28 | def nu_format(self): |
29 | 29 | return self.pre.format() + u" " + u' '.join([unicode(t) for t in self.terms]) + u" " + self.post.format() |
30 | 30 | |
31 | def format(self, unifier, functions): | |
31 | def format(self, unifier): | |
32 | 32 | acc = u'' |
33 | 33 | for t in self.terms: |
34 | 34 | |
35 | 35 | t = t.subst(unifier) |
36 | ||
37 | # now resolve functions. NOTE: this is a terrible hacky just-for-now implementation. TODO: better it. | |
38 | for f in functions: | |
39 | if t == f.sign: | |
40 | t = f.result | |
41 | break | |
42 | 36 | |
43 | 37 | s = unicode(t) |
44 | 38 | if (acc == u'') or (s in (u'.', u',', u'!', u'"', u"'")): |
46 | 40 | else: |
47 | 41 | acc += u' ' + s |
48 | 42 | return acc |
49 | ||
50 | ||
51 | class Function(AST): | |
52 | pass | |
53 | 43 | |
54 | 44 | |
55 | 45 | class Situation(AST): |
54 | 54 | def generate_move(self): |
55 | 55 | candidates = self.get_candidate_rules() |
56 | 56 | rule, unifier = random.choice(candidates) |
57 | move = rule.format(unifier, self.world.functions) | |
57 | move = rule.format(unifier) | |
58 | 58 | self.update_state(unifier, rule) |
59 | 59 | return move |
60 | 60 |
0 | 0 | # encoding: UTF-8 |
1 | 1 | |
2 | from samovar.ast import World, Rule, Function, Situation, Cond, Assert, Retract | |
2 | from samovar.ast import World, Rule, Situation, Cond, Assert, Retract | |
3 | 3 | from samovar.terms import Term, Var |
4 | 4 | from samovar.scanner import Scanner |
5 | 5 | |
6 | 6 | |
7 | # World ::= {Rules | Functions | Situations}. | |
7 | # World ::= {Rules | Situations}. | |
8 | 8 | # Rules ::= "rules" {Rule} "end". |
9 | # Functions ::= "functions" {Function} "end". | |
10 | 9 | # Situations ::= "situations" {Situation} "end". |
11 | 10 | # Rule ::= Cond {Term | Punct} Cond. |
12 | # Function ::= Term "→" Term. | |
13 | 11 | # Situation ::= Cond. |
14 | 12 | # Cond ::= "[" Expr {"," Expr} "]". |
15 | 13 | # Expr ::= Term | "~" Term. |
24 | 22 | |
25 | 23 | def world(self): |
26 | 24 | rules = [] |
27 | functions = [] | |
28 | 25 | situations = [] |
29 | while self.scanner.on('rules', 'functions', 'situations'): | |
26 | while self.scanner.on('rules', 'situations'): | |
30 | 27 | if self.scanner.on('rules'): |
31 | 28 | rules.extend(self._section('rules', self.rule)) |
32 | if self.scanner.on('functions'): | |
33 | functions.extend(self._section('functions', self.function)) | |
34 | 29 | if self.scanner.on('situations'): |
35 | 30 | situations.extend(self._section('situations', self.situation)) |
36 | return World(rules=rules, functions=functions, situations=situations) | |
31 | return World(rules=rules, situations=situations) | |
37 | 32 | |
38 | 33 | def _section(self, heading, method): |
39 | 34 | items = [] |
50 | 45 | terms.append(self.term()) |
51 | 46 | post = self.cond() |
52 | 47 | return Rule(pre=pre, terms=terms, post=post) |
53 | ||
54 | def function(self): | |
55 | sign = self.term() | |
56 | self.scanner.expect(u'→') | |
57 | result = self.term() | |
58 | return Function(sign=sign, result=result) | |
59 | 48 | |
60 | 49 | def situation(self): |
61 | 50 | cond = self.cond() |