aontu 0.0.7 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/op/op.ts ADDED
@@ -0,0 +1,17 @@
1
+ /* Copyright (c) 2021 Richard Rodger, MIT License. */
2
+
3
+
4
+ import { Context } from '../unify'
5
+ import { Val } from '../val'
6
+
7
+ import { disjunct } from './disjunct'
8
+ import { unite } from './unite'
9
+
10
+
11
+ type Operation = (ctx: Context, a?: Val, b?: Val) => Val
12
+
13
+ export {
14
+ Operation,
15
+ disjunct,
16
+ unite,
17
+ }
@@ -0,0 +1,82 @@
1
+ /* Copyright (c) 2021 Richard Rodger, MIT License */
2
+
3
+
4
+ // import { Site } from '../lang'
5
+ import { Context } from '../unify'
6
+ import {
7
+ Val,
8
+ MapVal,
9
+ ConjunctVal,
10
+ DisjunctVal,
11
+ RefVal,
12
+ PrefVal,
13
+ TOP,
14
+ Nil,
15
+ DONE
16
+ } from '../val'
17
+ import { Operation } from './op'
18
+
19
+
20
+ // Vals should only have to unify downwards (in .unify) over Vals they understand.
21
+ // and for complex Vals, TOP, which means self unify if not yet done
22
+ const unite: Operation = (ctx: Context, a?: Val, b?: Val) => {
23
+ let out = a
24
+
25
+ //console.log('Ua', a && a.canon, b && b.canon)
26
+
27
+ if (b && (TOP === a || !a)) {
28
+ //console.log('Utb', b.canon)
29
+ out = b
30
+ }
31
+
32
+ else if (a && (TOP === b || !b)) {
33
+ //console.log('Uta', a.canon)
34
+ out = a
35
+ }
36
+
37
+ else if (a && b && TOP !== b) {
38
+ if (a instanceof Nil) {
39
+ out = update(a, b)
40
+ }
41
+ else if (b instanceof Nil) {
42
+ out = update(b, a)
43
+ }
44
+ else if (
45
+ b instanceof ConjunctVal ||
46
+ b instanceof DisjunctVal ||
47
+ b instanceof RefVal ||
48
+ b instanceof PrefVal
49
+ ) {
50
+
51
+ //console.log('U', a.canon, b.canon)
52
+ return b.unify(a, ctx)
53
+ }
54
+ else if (a.constructor === b.constructor && a.peg === b.peg) {
55
+ out = update(a, b)
56
+ }
57
+ else {
58
+ out = a.unify(b, ctx)
59
+ }
60
+ }
61
+
62
+ if (!out) {
63
+ out = Nil.make(ctx, 'unite', a, b)
64
+ }
65
+
66
+ if (DONE !== out.done) {
67
+ out = out.unify(TOP, ctx)
68
+ }
69
+
70
+ return out
71
+ }
72
+
73
+
74
+ function update(x: Val, _y: Val) {
75
+ // TODO: update x with y.site
76
+ return x
77
+ }
78
+
79
+
80
+ export {
81
+ unite
82
+ }
package/lib/unify.ts CHANGED
@@ -8,28 +8,72 @@ import {
8
8
  Val,
9
9
  RefVal,
10
10
  MapVal,
11
+ Nil,
11
12
  } from './val'
12
13
 
13
14
  import {
14
15
  Lang
15
16
  } from './lang'
16
17
 
18
+ import {
19
+ unite
20
+ } from './op/op'
21
+
22
+
23
+ type Path = string[]
24
+
25
+
17
26
 
18
27
  class Context {
19
- root: MapVal
20
- constructor(cfg: any) {
28
+ root: Val // Starting Val, root of paths.
29
+ path: Path // Path to current Val.
30
+ err: Nil[] // Nil error log of current unify.
31
+ vc: number // Val counter to create unique val ids.
32
+
33
+
34
+ constructor(cfg: {
35
+ root: Val
36
+ err?: Nil[],
37
+ vc?: number
38
+ }) {
21
39
  this.root = cfg.root
40
+ this.path = []
41
+ this.err = cfg.err || []
42
+
43
+ // Multiple unify passes will keep incrementing Val counter.
44
+ this.vc = null == cfg.vc ? 1_000_000_000 : cfg.vc
45
+ }
46
+
47
+
48
+ clone(cfg: {
49
+ root: Val,
50
+ path?: Path
51
+ }): Context {
52
+ return new Context({
53
+ root: cfg.root,
54
+ err: this.err,
55
+ vc: this.vc,
56
+ })
22
57
  }
23
58
 
59
+
60
+ descend(key: string): Context {
61
+ return this.clone({
62
+ root: this.root,
63
+ path: this.path.concat(key),
64
+ })
65
+ }
66
+
67
+
24
68
  find(ref: RefVal) {
25
69
 
26
70
  // TODO: relative paths
27
- if (ref.absolute) {
71
+ if (this.root instanceof MapVal && ref.absolute) {
28
72
  let node: MapVal = this.root
29
73
  let pI = 0
30
74
  for (; pI < ref.parts.length && node instanceof MapVal; pI++) {
31
75
  let part = ref.parts[pI]
32
- node = node.val[part]
76
+ node = node.peg[part]
33
77
  }
34
78
 
35
79
  if (pI === ref.parts.length) {
@@ -43,7 +87,8 @@ class Context {
43
87
  class Unify {
44
88
  root: Val
45
89
  res: Val
46
- dc = 0
90
+ err: Nil[]
91
+ dc: number
47
92
  lang: Lang
48
93
 
49
94
  constructor(root: Val | string, lang?: Lang) {
@@ -54,15 +99,29 @@ class Unify {
54
99
 
55
100
  this.root = root
56
101
  this.res = root
102
+ this.err = []
57
103
 
58
104
  let res = root
59
- let ctx: Context
60
- while (this.dc < 111 && DONE !== res.done) {
61
- ctx = new Context({ root: res })
62
- res = res.unify(TOP, ctx)
63
- this.dc++
105
+ let ctx: Context = new Context({
106
+ root: res,
107
+ err: this.err,
108
+ })
109
+
110
+
111
+ // TODO: derive maxdc from res deterministically
112
+ // perhaps parse should count intial vals, paths, etc?
113
+
114
+
115
+
116
+
117
+ let maxdc = 999
118
+ for (this.dc = 0; this.dc < maxdc && DONE !== res.done; this.dc++) {
119
+ //res = res.unify(TOP, ctx)
120
+ res = unite(ctx, res, TOP)
121
+ ctx = ctx.clone({ root: res })
64
122
  }
65
123
 
124
+
66
125
  this.res = res
67
126
  }
68
127
  }
@@ -70,5 +129,6 @@ class Unify {
70
129
 
71
130
  export {
72
131
  Context,
132
+ Path,
73
133
  Unify,
74
134
  }