Skip to content

Commit

Permalink
feat(#6): as-phi atom
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed May 30, 2024
1 parent 881999b commit 335edd2
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 19 deletions.
13 changes: 6 additions & 7 deletions eo2js-runtime/src/objects/org/eolang/as-phi.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
const object = require('../../../runtime/object')
const {LAMBDA} = require('../../../runtime/attribute/specials');
const ErFailure = require('../../../runtime/error/ErFailure');
const at_void = require('../../../runtime/attribute/at-void');
const {data} = require('../../../runtime/data');

/**
* As_phi.
* @return {Object} - As_phi object
* @todo #3:30min Implement as_phi atom. We need to implement the atom and make sure it
* works. For the details of implementation check the Java analogue on
* https://github.com/objectionary/eo/tree/master/eo-runtime/src/main/java/EOorg/EOeolang
*/
const as_phi = function() {
const obj = object('as_phi')
obj.assets[LAMBDA] = function(_) {
throw new ErFailure(
`Atom as_phi is not implemented yet`
obj.attrs['x'] = at_void('x')
obj.assets[LAMBDA] = function(self) {
return data.toObject(
self.take('x').φTerm()
)
}
return obj
Expand Down
3 changes: 3 additions & 0 deletions eo2js-runtime/src/runtime/attribute/at-lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const at_lambda = function(object, callback) {
},
copy: function(rho) {
return at_lambda(rho, callback)
},
φTerm: function() {
return LAMBDA
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions eo2js-runtime/src/runtime/attribute/at-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,28 @@
* @return {Object} - Attribute
*/
const at_once = function(origin) {
let cache = null
const cache = {
object: null,
term: null
}
return {
put: function(object) {
origin.put(object)
},
get: function() {
if (cache == null) {
cache = origin.get()
if (cache.object === null) {
cache.object = origin.get()
}
return cache
return cache.object
},
copy: function(rho) {
return at_once(origin.copy(rho))
},
φTerm: function() {
if (cache.term === null) {
cache.term = origin.φTerm()
}
return cache.term
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions eo2js-runtime/src/runtime/attribute/at-rho.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const ErFailure = require('../error/ErFailure');
const {RHO} = require('./specials');
const at_term = require('./at-term');

/**
* Attribute that keeps \rho.
* @param {Object} [object] - Rho object
* @return {Object} - Rho attribute
*/
const at_rho = function(object = null) {
let rho = object
return {
return at_term({
put: function(obj) {
if (rho == null) {
rho = obj
Expand All @@ -22,7 +24,7 @@ const at_rho = function(object = null) {
copy: function(_) {
return at_rho(rho)
}
}
})
}

module.exports = at_rho
7 changes: 4 additions & 3 deletions eo2js-runtime/src/runtime/attribute/at-safe.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
const validated = require('../validated');
const safe = require('../safe');
const at_term = require('./at-term');

/**
* Attribute that catches {@link ErFailure} and throws {@link ErError}.
* @param {object} origin - Original attribute
* @return {Object} - Attribute
*/
const at_safe = function(origin) {
return {
return at_term({
put: function(object) {
return origin.put(object)
origin.put(object)
},
get: function() {
return validated(() => safe(origin.get()))
},
copy: function(rho) {
return at_safe(origin.copy(rho))
}
}
})
}

module.exports = at_safe
15 changes: 15 additions & 0 deletions eo2js-runtime/src/runtime/attribute/at-term.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Attribute as φTerm.
* @param {Object} origin - Original attribute
* @return {Object} - Attribute with default φTerm
*/
const at_term = function(origin) {
return {
...origin,
φTerm: function() {
return origin.get().φTerm()
}
}
}

module.exports = at_term
11 changes: 10 additions & 1 deletion eo2js-runtime/src/runtime/attribute/at-void.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const ErFailure = require('../error/ErFailure');
const {EMPTY} = require('./specials');

/**
* Void attribute.
Expand All @@ -14,7 +15,6 @@ const at_void = function(name, object = null) {
throw new ErFailure(`Void attribute '${name}' is already set, can't reset`)
}
obj = object
return true
},
get: function() {
if (obj == null) {
Expand All @@ -24,6 +24,15 @@ const at_void = function(name, object = null) {
},
copy: function(_) {
return at_void(name, obj)
},
φTerm: function() {
let term
if (obj === null) {
term = EMPTY
} else {
term = obj.φTerm()
}
return term
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion eo2js-runtime/src/runtime/attribute/specials.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Attribute and asset names.
* @type {{PHI: string, DELTA: string, SIGMA: string, RHO: string, VTX: string, LAMBDA: string}}
* @type {{PHI: string, DELTA: string, SIGMA: string, RHO: string, VTX: string, LAMBDA: string, EMPTY: string}}
*/
module.exports = {
PHI: 'φ',
DELTA: 'Δ',
LAMBDA: 'λ',
RHO: 'ρ',
EMPTY: 'Ø'
}
24 changes: 24 additions & 0 deletions eo2js-runtime/src/runtime/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ const object = function(name = 'object') {
*/
forma: function() {
return name
},
/**
* Represent self as φ term.
* @return {String} - Self as φ calculus term
*/
φTerm: function() {
const list = []
const binding = (left, right) => `${left}${right}`
if (this.assets.hasOwnProperty(DELTA)) {
list.push(binding(DELTA, `[${this.assets[DELTA].join(', ')}]`))
}
if (this.assets.hasOwnProperty(LAMBDA)) {
list.push(binding(LAMBDA, 'Lambda'))
}
list.push(
...Object.keys(this.attrs)
.filter((attr) => attr !== RHO)
.map((attr) => binding(attr, this.attrs[attr].φTerm()))
)
let term = name
if (list.length !== 0) {
term = ${vtx}·${term}⟦\n\t${list.sort().join(',\n').replaceAll(/\n/g, '\n\t')}\n⟧`
}
return term
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions eo2js-runtime/test/runtime/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,22 @@ describe('object', function() {
assert.deepStrictEqual(obj.assets, copy.assets)
})
})
describe('#φTerm()', function() {
it('should contain all properties', function() {
const somebody = object('somebody')
somebody.attrs['m'] = at_void('m')
const obj = object('x')
obj.attrs['y'] = at_void('y')
obj.attrs['s'] = at_simple(somebody)
obj.assets[DELTA] = [1, 2, 3]
obj.assets[LAMBDA] = () => {}
const term = obj.φTerm()
console.debug(term)
assert.ok(term.includes('·x⟦'))
assert.ok(term.includes('Δ ↦ [1, 2, 3],'))
assert.ok(term.includes('λ ↦ Lambda'))
assert.ok(term.includes('s ↦ λ,'))
assert.ok(term.includes('y ↦ Ø,'))
})
})
})
1 change: 0 additions & 1 deletion eo2js/test/it/runtime-tests.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const assert = require('assert');
* @type {string[]}
*/
const exclude = [
'as-phi-tests',
'rust-tests',
].map((name) => `org/eolang/${name}.test.js`)

Expand Down

1 comment on commit 335edd2

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 335edd2 May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 3-eac6ec12 disappeared from eo2js-runtime/src/objects/org/eolang/as_phi.js), that's why I closed #40. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.