git @ Cat's Eye Technologies Samovar / b8e2a2e
Remove support for functions, as I've concluded they're unneeded. Chris Pressey 6 years ago
4 changed file(s) with 6 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
8484 end
8585 ???> IndexError
8686
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
11287 no need for functions
11388 ---------------------
11489
2828 def nu_format(self):
2929 return self.pre.format() + u" " + u' '.join([unicode(t) for t in self.terms]) + u" " + self.post.format()
3030
31 def format(self, unifier, functions):
31 def format(self, unifier):
3232 acc = u''
3333 for t in self.terms:
3434
3535 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
4236
4337 s = unicode(t)
4438 if (acc == u'') or (s in (u'.', u',', u'!', u'"', u"'")):
4640 else:
4741 acc += u' ' + s
4842 return acc
49
50
51 class Function(AST):
52 pass
5343
5444
5545 class Situation(AST):
5454 def generate_move(self):
5555 candidates = self.get_candidate_rules()
5656 rule, unifier = random.choice(candidates)
57 move = rule.format(unifier, self.world.functions)
57 move = rule.format(unifier)
5858 self.update_state(unifier, rule)
5959 return move
6060
00 # encoding: UTF-8
11
2 from samovar.ast import World, Rule, Function, Situation, Cond, Assert, Retract
2 from samovar.ast import World, Rule, Situation, Cond, Assert, Retract
33 from samovar.terms import Term, Var
44 from samovar.scanner import Scanner
55
66
7 # World ::= {Rules | Functions | Situations}.
7 # World ::= {Rules | Situations}.
88 # Rules ::= "rules" {Rule} "end".
9 # Functions ::= "functions" {Function} "end".
109 # Situations ::= "situations" {Situation} "end".
1110 # Rule ::= Cond {Term | Punct} Cond.
12 # Function ::= Term "→" Term.
1311 # Situation ::= Cond.
1412 # Cond ::= "[" Expr {"," Expr} "]".
1513 # Expr ::= Term | "~" Term.
2422
2523 def world(self):
2624 rules = []
27 functions = []
2825 situations = []
29 while self.scanner.on('rules', 'functions', 'situations'):
26 while self.scanner.on('rules', 'situations'):
3027 if self.scanner.on('rules'):
3128 rules.extend(self._section('rules', self.rule))
32 if self.scanner.on('functions'):
33 functions.extend(self._section('functions', self.function))
3429 if self.scanner.on('situations'):
3530 situations.extend(self._section('situations', self.situation))
36 return World(rules=rules, functions=functions, situations=situations)
31 return World(rules=rules, situations=situations)
3732
3833 def _section(self, heading, method):
3934 items = []
5045 terms.append(self.term())
5146 post = self.cond()
5247 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)
5948
6049 def situation(self):
6150 cond = self.cond()