mini_kraken 0.1.10 → 0.2.01

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +334 -0
  3. data/CHANGELOG.md +55 -1
  4. data/README.md +98 -8
  5. data/lib/mini_kraken.rb +8 -1
  6. data/lib/mini_kraken/core/any_value.rb +5 -1
  7. data/lib/mini_kraken/core/association_walker.rb +1 -1
  8. data/lib/mini_kraken/core/atomic_term.rb +1 -0
  9. data/lib/mini_kraken/core/base_arg.rb +10 -0
  10. data/lib/mini_kraken/core/conde.rb +143 -0
  11. data/lib/mini_kraken/core/cons_cell.rb +29 -1
  12. data/lib/mini_kraken/core/def_relation.rb +50 -0
  13. data/lib/mini_kraken/core/disj2.rb +1 -0
  14. data/lib/mini_kraken/core/environment.rb +1 -1
  15. data/lib/mini_kraken/core/equals.rb +2 -5
  16. data/lib/mini_kraken/core/formal_arg.rb +22 -0
  17. data/lib/mini_kraken/core/formal_ref.rb +25 -0
  18. data/lib/mini_kraken/core/goal.rb +12 -4
  19. data/lib/mini_kraken/core/goal_arg.rb +5 -3
  20. data/lib/mini_kraken/core/goal_template.rb +62 -0
  21. data/lib/mini_kraken/core/k_boolean.rb +31 -0
  22. data/lib/mini_kraken/core/k_symbol.rb +11 -0
  23. data/lib/mini_kraken/core/relation.rb +7 -0
  24. data/lib/mini_kraken/core/variable.rb +10 -4
  25. data/lib/mini_kraken/core/variable_ref.rb +1 -0
  26. data/lib/mini_kraken/core/vocabulary.rb +2 -1
  27. data/lib/mini_kraken/glue/dsl.rb +210 -0
  28. data/lib/mini_kraken/glue/run_star_expression.rb +2 -3
  29. data/lib/mini_kraken/version.rb +1 -1
  30. data/mini_kraken.gemspec +6 -3
  31. data/spec/.rubocop.yml +13 -0
  32. data/spec/core/conde_spec.rb +147 -0
  33. data/spec/core/cons_cell_spec.rb +35 -0
  34. data/spec/core/def_relation_spec.rb +96 -0
  35. data/spec/core/duck_fiber_spec.rb +1 -1
  36. data/spec/core/goal_template_spec.rb +74 -0
  37. data/spec/core/k_boolean_spec.rb +107 -0
  38. data/spec/core/k_symbol_spec.rb +4 -0
  39. data/spec/glue/dsl_chap1_spec.rb +594 -0
  40. data/spec/glue/run_star_expression_spec.rb +222 -7
  41. data/spec/support/factory_methods.rb +15 -0
  42. metadata +24 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec7b984a9f0c857f74c8a8989c5da2072398775573b7ca6679d99f96ef13eac1
4
- data.tar.gz: cdebdebae82c424b9ae701f959765f0905322798a371e2aec42ed9a229e35a4f
3
+ metadata.gz: f51855ac3179a111a4ace328340402fa167818773086076abf90ed845c31d248
4
+ data.tar.gz: f6bde0d3d7b872461d7cb0b087eaab821240f14317445e95f4d832d09af51e2f
5
5
  SHA512:
6
- metadata.gz: 2ce3553527adeebef7adc69ec5a861fe65f9f451e54a81e23f00769923ced6b6a6011846ae3e9a673c8b9094a7dcce6ea6f8d263d55c8f543ef0de309e34c11c
7
- data.tar.gz: 90b45061cde1119ad648359063682f411f9e5705a294340976bd027561b1d94da7afbfc9d0d27dce67942e1407d4d4ea40df85cf3fe67c9fca998d22052b6187
6
+ metadata.gz: ae6319164a2f6212bdbdea35dd5b5befa86648b685063dea79d220b70e5852c3e8b343d2d37ebfe6584aa3966b44174e91df2866d63e03a219a0008a5b382d66
7
+ data.tar.gz: 70eb204ab842ea45527bd5ca8661dcd98081422cae4d6848d220bc70e080da94b3b7078f3602f1143535821a9894309b9bec39ec5f4d6c60edcfd21cfa5c8f4f
@@ -0,0 +1,334 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'exp/**/*'
4
+
5
+ Layout/ArgumentAlignment:
6
+ Enabled: false
7
+
8
+ Layout/ArrayAlignment:
9
+ Enabled: true
10
+ EnforcedStyle: with_fixed_indentation
11
+
12
+ Layout/CaseIndentation:
13
+ Enabled: false
14
+
15
+ Layout/ClosingHeredocIndentation:
16
+ Enabled: false
17
+
18
+ Layout/CommentIndentation:
19
+ Enabled: false
20
+
21
+ Layout/ElseAlignment:
22
+ Enabled: false
23
+
24
+ Layout/EmptyLines:
25
+ Enabled: false
26
+
27
+ Layout/EndAlignment:
28
+ Enabled: false
29
+
30
+ Layout/EndOfLine:
31
+ Enabled: true
32
+ EnforcedStyle: lf
33
+
34
+ Layout/FirstArgumentIndentation:
35
+ Enabled: false
36
+
37
+ Layout/IndentationWidth:
38
+ Enabled: false
39
+
40
+ Layout/IndentationConsistency:
41
+ Enabled: true
42
+
43
+ Layout/HeredocIndentation:
44
+ Enabled: false
45
+
46
+ Layout/MultilineHashBraceLayout:
47
+ Enabled: true
48
+
49
+ Layout/MultilineMethodCallBraceLayout:
50
+ Enabled: true
51
+ EnforcedStyle: same_line
52
+
53
+ Layout/SpaceAroundOperators:
54
+ Enabled: true
55
+
56
+ Layout/SpaceInsideParens:
57
+ Enabled: true
58
+
59
+ Layout/IndentationStyle:
60
+ Enabled: true
61
+
62
+ Layout/SpaceAroundMethodCallOperator:
63
+ Enabled: true
64
+
65
+ Layout/TrailingEmptyLines:
66
+ Enabled: true
67
+
68
+ Layout/TrailingWhitespace:
69
+ Enabled: true
70
+
71
+ Lint/Loop:
72
+ Enabled: true
73
+
74
+ Lint/RaiseException:
75
+ Enabled: true
76
+
77
+ Lint/RescueException:
78
+ Enabled: true
79
+
80
+ Lint/StructNewOverride:
81
+ Enabled: true
82
+
83
+ Lint/UnusedMethodArgument:
84
+ Enabled: true
85
+
86
+ Lint/UselessAccessModifier:
87
+ Enabled: true
88
+
89
+ Lint/Void:
90
+ Enabled: false
91
+
92
+ Lint/UselessAssignment:
93
+ Enabled: true
94
+
95
+ Metrics/AbcSize:
96
+ Enabled: false
97
+
98
+ Metrics/BlockLength:
99
+ Enabled: true
100
+ Max: 350
101
+
102
+ Metrics/BlockNesting:
103
+ Enabled: true
104
+ Max: 5
105
+
106
+ Metrics/ClassLength:
107
+ Enabled: true
108
+ Max: 350
109
+
110
+ Metrics/CyclomaticComplexity:
111
+ Enabled: false
112
+
113
+ Layout/LineLength:
114
+ Enabled: false
115
+ Max: 90
116
+
117
+ Metrics/MethodLength:
118
+ Enabled: true
119
+ Max: 50
120
+
121
+ Metrics/ModuleLength:
122
+ Enabled: true
123
+ Max: 500
124
+
125
+ Metrics/PerceivedComplexity:
126
+ Enabled: false
127
+
128
+ Naming/ConstantName:
129
+ Enabled: false
130
+
131
+ Naming/ClassAndModuleCamelCase:
132
+ Enabled: false
133
+
134
+ Naming/BlockParameterName:
135
+ Enabled: true
136
+
137
+ Naming/MethodParameterName:
138
+ Enabled: false
139
+
140
+ Naming/VariableName:
141
+ Enabled: false
142
+
143
+ Style/Alias:
144
+ Enabled: true
145
+
146
+ Layout/HashAlignment:
147
+ Enabled: false
148
+
149
+ Style/AsciiComments:
150
+ Enabled: false
151
+
152
+ Style/BarePercentLiterals:
153
+ Enabled: false
154
+
155
+ Style/BlockComments:
156
+ Enabled: false
157
+
158
+ Style/CharacterLiteral:
159
+ Enabled: false
160
+
161
+ Style/ClassCheck:
162
+ Enabled: false
163
+
164
+ Style/ClassVars:
165
+ Enabled: false
166
+
167
+ Style/ColonMethodCall:
168
+ Enabled: false
169
+
170
+ Style/CommentAnnotation:
171
+ Enabled: false
172
+
173
+ Style/CommentedKeyword:
174
+ Enabled: false
175
+
176
+ Style/ConditionalAssignment:
177
+ Enabled: false
178
+
179
+ Style/DefWithParentheses:
180
+ Enabled: true
181
+
182
+ Style/Documentation:
183
+ Enabled: false
184
+
185
+ Style/ExpandPathArguments:
186
+ Enabled: false
187
+
188
+ Style/ExponentialNotation:
189
+ Enabled: true
190
+
191
+ Style/GuardClause:
192
+ Enabled: false
193
+
194
+ Style/HashEachMethods:
195
+ Enabled: true
196
+
197
+ Style/HashTransformKeys:
198
+ Enabled: true
199
+
200
+ Style/HashTransformValues:
201
+ Enabled: true
202
+
203
+ Style/IfUnlessModifier:
204
+ Enabled: false
205
+
206
+ Style/InverseMethods:
207
+ Enabled: true
208
+
209
+ Style/MissingRespondToMissing:
210
+ Enabled: false
211
+
212
+ Style/Next:
213
+ Enabled: false
214
+
215
+ Style/NumericLiterals:
216
+ Enabled: false
217
+
218
+ Style/RaiseArgs:
219
+ Enabled: true
220
+
221
+ Style/RedundantReturn:
222
+ Enabled: false
223
+
224
+ Style/RedundantSelf:
225
+ Enabled: true
226
+
227
+ Style/RegexpLiteral:
228
+ Enabled: false
229
+
230
+ Style/PercentLiteralDelimiters:
231
+ Enabled: false
232
+
233
+ Style/StderrPuts:
234
+ Enabled: false
235
+
236
+ Style/StringLiterals:
237
+ Enabled: true
238
+
239
+ Style/TernaryParentheses:
240
+ Enabled: false
241
+
242
+ Style/UnlessElse:
243
+ Enabled: false
244
+
245
+ # Rubocop complains when it doesn't find an explicit setting for the following cops:
246
+ Layout/EmptyLinesAroundAttributeAccessor:
247
+ Enabled: true
248
+
249
+ Lint/BinaryOperatorWithIdenticalOperands:
250
+ Enabled: true
251
+
252
+ Lint/DeprecatedOpenSSLConstant:
253
+ Enabled: true
254
+
255
+ Lint/DuplicateElsifCondition:
256
+ Enabled: true
257
+
258
+ Lint/DuplicateRescueException:
259
+ Enabled: true
260
+
261
+ Lint/EmptyConditionalBody:
262
+ Enabled: true
263
+
264
+ Lint/FloatComparison:
265
+ Enabled: true
266
+
267
+ Lint/MissingSuper:
268
+ Enabled: true
269
+
270
+ Lint/MixedRegexpCaptureTypes:
271
+ Enabled: true
272
+
273
+ Lint/OutOfRangeRegexpRef:
274
+ Enabled: true
275
+
276
+ Lint/SelfAssignment:
277
+ Enabled: true
278
+
279
+ Lint/TopLevelReturnWithArgument:
280
+ Enabled: true
281
+
282
+ Lint/UnreachableLoop:
283
+ Enabled: true
284
+
285
+ Style/AccessorGrouping:
286
+ Enabled: true
287
+
288
+ Style/ArrayCoercion:
289
+ Enabled: true
290
+
291
+ Style/BisectedAttrAccessor:
292
+ Enabled: true
293
+
294
+ Style/CaseLikeIf:
295
+ Enabled: true
296
+
297
+ Style/ExplicitBlockArgument:
298
+ Enabled: true
299
+
300
+ Style/GlobalStdStream:
301
+ Enabled: true
302
+
303
+ Style/HashAsLastArrayItem:
304
+ Enabled: true
305
+
306
+ Style/HashLikeCase:
307
+ Enabled: true
308
+
309
+ Style/OptionalBooleanParameter:
310
+ Enabled: true
311
+
312
+ Style/RedundantAssignment:
313
+ Enabled: true
314
+
315
+ Style/RedundantFetchBlock:
316
+ Enabled: true
317
+
318
+ Style/RedundantFileExtensionInRequire:
319
+ Enabled: true
320
+
321
+ Style/RedundantRegexpCharacterClass:
322
+ Enabled: true
323
+
324
+ Style/RedundantRegexpEscape:
325
+ Enabled: true
326
+
327
+ Style/SingleArgumentDig:
328
+ Enabled: true
329
+
330
+ Style/SlicingWithRange:
331
+ Enabled: true
332
+
333
+ Style/StringConcatenation:
334
+ Enabled: true
@@ -1,4 +1,58 @@
1
- ## [0.1.10] - 2020-06-10
1
+ ## [0.2.01] - 2020-08-07
2
+ - The DSL (Domain Specific Language) now supports `defrel` and boolean literals.
3
+
4
+ ### CHANGED
5
+ - Constructor `DefRelation#initialize` now freezes any new class instance.
6
+ - Constructor `GoalTemplate#initialize` now freezes any new class instance.
7
+ - Mixin module `Core::DSL` new method `defrel` to build custom relations.
8
+ - File `.rubocop.yml` to please Rubocop 0.89
9
+
10
+ ## [0.2.00] - 2020-07-12
11
+ - First release of DSL (Domain Specific Language)
12
+ - Fix defect for fused variables that remain fresh
13
+
14
+ ### NEW
15
+ - Mix-in module `Glue::DSL` hosting methods for implementing the DSL.
16
+ - Method `ConsCell#to_s` uses the Lisp convention for representing lists.
17
+
18
+ ### CHANGED
19
+ - File `README.md` Added a couple of examples of DSL use.
20
+ - Method `AnyValue#==` can compare with symbols with format '_' + integer literal (e.g. :_0).
21
+
22
+ ## [0.1.13] - 2020-07-01
23
+ - Cover all frames from Chapter One of "Reasoned Scheme" book.
24
+ - Fix defect for fused variables that remain fresh
25
+
26
+ ### CHANGED
27
+ - Method `Variable#quote` now takes into account of cases when variables are fused.
28
+ - Method `Vocabulary#names_fused` now copes with cases where no variable with given name can be found.
29
+
30
+ ## [0.1.12] - 2020-06-29
31
+ - Supports `conde`, that is, a relation that can take an arbitrary number of arguments.
32
+ - Cover all frames but one from Chapter One of "Reasoned Scheme" book.
33
+
34
+ ### New
35
+ - Class `Conde` a relation that succeeds for each of its successful arguments.
36
+
37
+ ### CHANGED
38
+ - Method `Goal#validated_actuals` add into account polyadic relations (= relations with arbitrary number of arguments)
39
+
40
+ ## [0.1.11] - 2020-06-25
41
+ - Supports `defrel`, that is, the capability to define new relations by combining other relations.
42
+ - Covers frames from "The Reasoned Scheme" book up to frame [1:87]
43
+
44
+ ### New
45
+ - Class `BaseArg` a generalization of goal or goal template argument.
46
+ - Class `DefRelation` A specialization of `Relation` class aimed for user-defined relation.
47
+ - Class `FormalArg` to represent goal template argument(s).
48
+ - Class `FormalRef` an allusion to a formal argument in a goal template.
49
+ - Class `GoalTemplate` a representation of a goal parametrized with formal arguments.
50
+ - Class `KBoolean` a MiniKraken representation of a boolean value.
51
+
52
+ ### CHANGED
53
+ - File `README.md` minor change: added more TODO's.
54
+
55
+ ## [0.1.10] - 2020-06-13
2
56
  - Supports frames from "The Reasoned Scheme" book up to frame [1:81]
3
57
 
4
58
  ### New
data/README.md CHANGED
@@ -4,24 +4,39 @@
4
4
  [![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/famished-tiger/mini_kraken/blob/master/LICENSE.txt)
5
5
 
6
6
  ### What is __mini_kraken__ ?
7
- An implemention of the [miniKanren](http://minikanren.org/) relational programming language in Ruby.
7
+ A library containing an implementation of the [miniKanren](http://minikanren.org/)
8
+ relational programming language in Ruby.
8
9
  *miniKanren* is a small language for relational (logic) programming.
9
- Based on the reference implementation, in Scheme from the "The Reasoned Schemer" book.
10
+ Based on the reference implementation, in Scheme from the "The Reasoned Schemer" book.
10
11
  Daniel P. Friedman, William E. Byrd, Oleg Kiselyov, and Jason Hemann: "The Reasoned Schemer", Second Edition,
11
12
  ISBN: 9780262535519, (2018), MIT Press.
12
13
 
13
14
  ### Features
15
+ - Pure Ruby implementation, not a port from another language
16
+ - Object-Oriented design
17
+ - No runtime dependencies
18
+
19
+ ### miniKanren Features
14
20
  - [X] ==
15
21
  - [X] run\*
16
- - [X] fresh
22
+ - [X] fresh
23
+ - [X] conde
17
24
  - [X] conj2
18
- - [X] disj2
25
+ - [X] disj2
26
+ - [X] defrel
19
27
 
20
28
  ### TODO
21
- - [ ] defrel
22
- - [ ] conde
29
+
23
30
  - [ ] Occurs check
24
31
 
32
+ List-centric relations from Chapter 2
33
+ - [ ] caro
34
+ - [ ] cdro
35
+ - [ ] conso
36
+ - [ ] nullo
37
+ - [ ] pairo
38
+ - [ ] singletono
39
+
25
40
  ## Installation
26
41
 
27
42
  Add this line to your application's Gemfile:
@@ -38,9 +53,84 @@ Or install it yourself as:
38
53
 
39
54
  $ gem install mini_kraken
40
55
 
41
- ## Usage
56
+ ## Examples
57
+
58
+ The following __MiniKraken__ examples use its DSL (Domain Specific Language).
59
+
60
+ ### Example 1
61
+ Let's first begin with a rather simplistic example.
62
+
63
+ ```ruby
64
+ require 'mini_kraken' # Load MiniKraken library
65
+
66
+ extend(MiniKraken::Glue::DSL) # Add DSL method to self (object in context)
67
+
68
+ result = run_star('q', equals(q, :pea))
69
+ puts result # => (:pea)
70
+ ```
71
+
72
+ The two first lines in the above code snippet are pretty standard:
73
+ - The first line loads the `mini_kraken` library.
74
+ - The second line add the DSL methods to the current object.
75
+
76
+ The next line constitutes a trivial `miniKanren` program.
77
+ The aim of a `miniKanren` program is to find one or more solutions involving the given logical variable(s)
78
+ and satisfying one or more goals to the `run_star method.
79
+ In our example, the `run_star` method instructs `MiniKraken` to find all solutions,
80
+ knowing that each successful solution:
81
+ - binds a value to the provided variable `q` and
82
+ - meets the goal `equals(q, :pea)`.
83
+
84
+ The goal `equals(q, :pea)` succeeds because the logical variable `q` is _fresh_ (that is,
85
+ not yet bound to a value) and will be bound to the symbol `:pea` as a side effect
86
+ of the goal `equals`.
87
+
88
+ So the above program succeeds and the only found solution is obtained by binding
89
+ the variable `q` to the value :pea. Hence the result of the `puts` method.
90
+
91
+ ### Example 2
92
+ The next example illustrates the behavior of a failing `miniKanren` program.
93
+
94
+ ```ruby
95
+ require 'mini_kraken' # Load MiniKraken library
96
+
97
+ extend(MiniKraken::Glue::DSL) # Add DSL method to self (object in context)
98
+
99
+ # Following miniKanren program fails
100
+ result = run_star('q', [equals(q, :pea), equals(q, :pod)])
101
+ puts result # => ()
102
+ ```
103
+ In this example, we learn that `run_star` can take multiple goals placed in an array.
104
+ The program fails to find a solution since it is not possible to satisfy the two `equals` goals simultaneously.
105
+ In case of failure, the `run_star` returns an empty list represented as `()` in the output.
106
+
107
+
108
+ ### Example 3
109
+ The next example shows the use two logical variables.
110
+
111
+ ```ruby
112
+ # In this example and following, one assumes that DSL is loaded as shown in Example 1
113
+
114
+ result = run_star(['x', 'y'], [equals(:hello, x), equals(y, :world)])
115
+ puts result # => ((:hello :world))
116
+ ```
42
117
 
43
- TODO: Write usage instructions here
118
+ This time, `run_star` takes two logical variables -`x` and `y`- and successfully finds the solution `x = :hello, y = :world`.
119
+
120
+ ### Example 4
121
+ The next example shows the use of `disj2` goals.
122
+ ```ruby
123
+ result = run_star(['x', 'y'],
124
+ [
125
+ disj2(equals(x, :blue), equals(x, :red)),
126
+ disj2(equals(y, :sea), equals(:mountain, y))
127
+ ])
128
+ puts result # => ((:blue :sea) (:blue :mountain) (:red :sea) (:red :mountain))
129
+ ```
130
+
131
+ Here, `run_star` takes two logical variables and two `disj2` goals.
132
+ A `disj2` succeeds if any of its arguments succeeds.
133
+ This program finds four distinct solutions for x, y pairs.
44
134
 
45
135
  ## Development
46
136