Skip to content

Commit

Permalink
clean up set section
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfitz committed Oct 5, 2019
1 parent 8e634e5 commit c77aa66
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 223 deletions.
4 changes: 2 additions & 2 deletions msg/COS_ListPre.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ for (var i=0; i<8; i++) {


cos.add("define pair | list 2");
cos.add("define first | ? lst | head $lst");
cos.add("define second | ? lst | head | tail $lst");
cos.add("define first | ? x:list | head $x:list");
cos.add("define second | ? x:list | head | tail $x:list");


var examples = cos.prand(10,3);
Expand Down
6 changes: 3 additions & 3 deletions msg/COS_NewType.scm
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
(set! (contents) (remove-element (x) (get! (contents))))))
((= (msg) =)
(lambda ((c container))
(set= (self inventory) (c inventory))))
(set:= (self inventory) (c inventory))))
0))))));

# Can pass anything to container function to create an object
Expand All @@ -198,9 +198,9 @@
(pocket add 77);
(pocket add 88);
(pocket add 99);
(set= (pocket inventory) (vector 77 88 99));
(set:= (pocket inventory) (vector 77 88 99));
(pocket remove 88);
(set= (pocket inventory) (vector 77 99));
(set:= (pocket inventory) (vector 77 99));

(define pocket2 (container new));
(pocket2 add 77);
Expand Down
78 changes: 78 additions & 0 deletions msg/COS_Set.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env node

const cos = require('./cosmic');

cos.seed(42);
cos.add('intro element');
cos.add('define element | ? x | ? y:list | not | = $undefined | list:find $y:list $x');

for (let i=0; i<5; i++) {
const hset = new Map();
const lst = [];
for (let j=0; j<6; j++) {
const x = cos.irand(10);
if (!hset.has(x)) {
hset.set(x, 1);
lst.push(x);
}
}
for (let j=0; j<3; j++) {
const mem = lst[cos.irand(lst.length)];
cos.add(`element ${mem} | ${cos.vector(lst)}`);
}
}

for (let i=0; i<5; i++) {
const hset = new Map();
const lst = [];
for (let j=0; j<6; j++) {
const x = cos.irand(10);
if (!hset.has(x)) {
hset.set(x, 1);
lst.push(x);
}
}
const mem = lst.shift();
cos.add(`not | element ${mem} | ${cos.vector(lst)}`);
}

cos.doc('Set some rules for set equality.');

cos.add(`define set:<= | ? x | ? y |
if (= 0 | list-length $x) $true |
and (element (head $x) $y) |
set:<= (tail $x) $y`);

cos.add(`define set:= | ? x | ? y |
and (set:<= $x $y) (set:<= $y $x)`);

cos.add(`set:= (vector 1 5 9) (vector 5 1 9)`);
cos.add(`set:= (vector 1 5 9) (vector 9 1 5)`);
cos.add(`not | set:= (vector 1 5 9) (vector 1 5)`)

cos.doc(`let's go leave ourselves wide open to Russell's paradox
by using characteristic functions since it doesn't really matter
within the bounds of this message`);

cos.add(`element 5 | all | ? x | = 15 | + $x 10`);
cos.add(`element 3 | all | ? x | = (* $x 3) (+ $x 6)`);

cos.add(`define set:0 | vector`);
cos.add(`element 0 $set:int:+`);
cos.add(`forall | ? x | => (element $x $set:int:+) (element (+ $x 1) $set:int:+)`);

for (let i=1; i<10; i++) {
cos.add(`element ${i} $set:int:+`);
}

cos.add(`define set:true:false | vector $true $false`);
cos.add(`element $true $set:true:false`);
cos.add(`element $false $set:true:false`);

cos.add(`define set:even | all | ? x | exists | ? y |
and (element $y $set:int:+) (= $x | * 2 $y)`);

for (let i=0; i<=6; i++) {
cos.add(`element ${i} $set:int:+`)
cos.add(`${(i % 2 === 0) ? '' : 'not | '}element ${i} $set:even`);
}
1 change: 1 addition & 0 deletions msg/COS_Set.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# MATH introduce sets and set membership
216 changes: 0 additions & 216 deletions msg/COS_Set.pl

This file was deleted.

6 changes: 6 additions & 0 deletions msg/cosmic.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,11 @@ CosWrite.prototype.listExpression = function(lst) {
return this.stringify(this.listVerbose(lst, true));
}

CosWrite.prototype.vector = function(lst,wrap) {
const result = "vector " + lst.map(x => String(x)).join(" ");
if (wrap) { return `(${result})`; }
return result;
}


module.exports = new CosWrite();
4 changes: 2 additions & 2 deletions src/cosmicos/Evaluate.hx
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class Evaluate {
vocab.check("set!",28);
vocab.check("get!",29);
vocab.check("all",30);
vocab.check("natural-set",31);
vocab.check("set:int:+",31);
vocab.check("undefined",32);
vocab.check("!",33);
vocab.check("div",34);
Expand Down Expand Up @@ -364,7 +364,7 @@ class Evaluate {
function(x) {
return Math.sqrt(x);
});
mem.add(vocab.get("natural-set"),
mem.add(vocab.get("set:int:+"),
mem.get(vocab.get("all"))(function (x) { return x>=0; }));
mem.add(vocab.get("div"), function(x:Dynamic){
return function(y:Dynamic) : Dynamic {
Expand Down
8 changes: 8 additions & 0 deletions variant/iconic/vocab.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,14 @@
{
"title": "exists",
"text": "&#8707;"
},
{
"title": "element",
"alias": "list:in"
},
{
"title": "set",
"text": "&#936;"
}
]
}

0 comments on commit c77aa66

Please sign in to comment.