Skip to content

Commit

Permalink
Update test programs
Browse files Browse the repository at this point in the history
  • Loading branch information
tizoc committed Jan 10, 2024
1 parent d154a07 commit 339b632
Show file tree
Hide file tree
Showing 40 changed files with 1,966 additions and 775 deletions.
34 changes: 17 additions & 17 deletions tests/binary.shen
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
(datatype binary

if (element? X [0 1])
_____________
X : zero-or-one;
if (element? X [0 1])
_____________
X : zero-or-one;

X : zero-or-one;
________________
[X] : binary;
X : zero-or-one;
________________
[X] : binary;

X : zero-or-one; Y : binary;
____________________________
[X | Y] : binary;
X : zero-or-one; Y : binary;
____________________________
[X | Y] : binary;

X : zero-or-one, [Y | Z] : binary >> P;
________________________________________
[X Y | Z] : binary >> P;)
X : zero-or-one, [Y | Z] : binary >> P;
________________________________________
[X Y | Z] : binary >> P;)

(define complement
{binary --> binary}
[0] -> [1]
[1] -> [0]
[1 N | X] -> [0 | (complement [N | X])]
[0 N | X] -> [1 | (complement [N | X])])
{binary --> binary}
[0] -> [1]
[1] -> [0]
[1 N | X] -> [0 | (complement [N | X])]
[0 N | X] -> [1 | (complement [N | X])])
19 changes: 16 additions & 3 deletions tests/bubble version 1.shen
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,20 @@
[X Y | Z] -> [X | (bubble [Y | Z])])

(define bubble-again-perhaps
\* no change as a result of bubbling - then the job is done *\
X X -> X
\* no change as a result of bubbling - then the job is done *\
X X -> X
\* else bubble again *\
X _ -> (bubble-sort X))
X _ -> (bubble-sort X))













14 changes: 14 additions & 0 deletions tests/bubble version 2.shen
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,17 @@
[X] -> [X]
[X Y | Z] -> [Y | (bubble [X | Z])] where (> Y X)
[X Y | Z] -> [X | (bubble [Y | Z])])














10 changes: 6 additions & 4 deletions tests/c-minus.shen
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,11 @@
========================================================
[Type Variable | Parameters] : parameters;)

(define c-minus
{string --> program}
File -> (compile (fn <program>) (read-file-as-unit-strings File)))
\\(spy +)

\\(define c-minus
\\{string --> program}
\\File -> (compile (fn <program>) (read-file-as-unit-strings File)))

(define read-file-as-unit-strings
{string --> (list string)}
Expand Down Expand Up @@ -368,7 +370,7 @@

(defcc <numeric>
{(list string) ==> string}
X := X where (element? X ["0" "1" "2""3""4""5" "6" "7" "8" "9"]);)
X := X where (element? X ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]);)

(define digit
{string --> integer}
Expand Down
17 changes: 10 additions & 7 deletions tests/calculator.shen
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(datatype arith-expr

X : number;
====================
[num X] : arith-expr;
X : number;
====================
[num X] : arith-expr;

if (element? Op [+ - * /])
X : arith-expr; Y : arith-expr;
===============================
[X Op Y] : arith-expr;)
if (element? Op [+ - * /])
X : arith-expr; Y : arith-expr;
===============================
[X Op Y] : arith-expr;)

(define do-calculation
{arith-expr --> number}
Expand All @@ -16,3 +16,6 @@
[X * Y] -> (* (do-calculation X) (do-calculation Y))
[X / Y] -> (/ (do-calculation X) (do-calculation Y))
[num X] -> X)



24 changes: 20 additions & 4 deletions tests/cartprod.shen
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
(define cartesian-product
[] _ -> []
[X | Y] Z -> (append (all-pairs-using-X X Z) (cartesian-product Y Z)))
[ ] _ -> [ ]
[X | Y] Z -> (append (all-pairs-using-X X Z) (cartesian-product Y Z)))

(define all-pairs-using-X
_ [] -> []
X [Y | Z] -> [[X Y] | (all-pairs-using-X X Z)])
_ [ ] -> [ ]
X [Y | Z] -> [[X Y] | (all-pairs-using-X X Z)])
















35 changes: 20 additions & 15 deletions tests/change.shen
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
(define count-change
Amount -> (count-change* Amount 200))
Amount -> (count-change* Amount 200))

(define count-change*
0 _ -> 1
_ 0 -> 0
Amount _ -> 0 where (> 0 Amount)
Amount Fst_Denom
-> (+ (count-change* (- Amount Fst_Denom) Fst_Denom)
(count-change* Amount (next-denom Fst_Denom))))
0 _ -> 1
_ 0 -> 0
Amount _ -> 0 where (> 0 Amount)
Amount Fst_Denom
-> (+ (count-change* (- Amount Fst_Denom) Fst_Denom)
(count-change* Amount (next-denom Fst_Denom))))

(define next-denom
200 -> 100
100 -> 50
50 -> 20
20 -> 10
10 -> 5
5 -> 2
2 -> 1
1 -> 0)
200 -> 100
100 -> 50
50 -> 20
20 -> 10
10 -> 5
5 -> 2
2 -> 1
1 -> 0)





80 changes: 40 additions & 40 deletions tests/classes-defaults.shen
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
(datatype class

Slots : [slot];
_______________________________________
(defclass Class Slots) : (class Class);
Slots : [slot];
_______________________________________
(defclass Class Slots) : (class Class);

Attribute : symbol; Type : symbol;
===================================
(@p Attribute Type) : slot;
Attribute : symbol; Type : symbol;
===================================
(@p Attribute Type) : slot;

Default : Type; Attribute : symbol; Type : symbol;
==================================================
(@p Attribute Type Default) : slot;)
Default : Type; Attribute : symbol; Type : symbol;
==================================================
(@p Attribute Type Default) : slot;)

(define defclass
Class ClassDef -> (let Attributes (map fst ClassDef)
Types (record-attribute-types Class ClassDef)
Assoc (map assign-values ClassDef)
NewClassDef [[class | Class] | Assoc]
Store (put-prop Class classdef NewClassDef)
RecordClass (axiom Class Class [class Class])
Class))
Class ClassDef -> (let Attributes (map fst ClassDef)
Types (record-attribute-types Class ClassDef)
Assoc (map assign-values ClassDef)
NewClassDef [[class | Class] | Assoc]
Store (put-prop Class classdef NewClassDef)
RecordClass (axiom Class Class [class Class])
Class))

(define assign-values
(@p Attribute _ Value) -> [Attribute | Value]
(@p Attribute _) -> [Attribute | fail!])

(define axiom
DataType X A -> (eval [datatype DataType
________
X : A;]))
________
X : A;]))

(define record-attribute-types
_ [] -> []
Class [(@p Attribute Type _) | ClassDef]
-> (let DataTypeName (concat Class Attribute)
DataType (axiom DataTypeName Attribute [attribute Class Type])
(record-attribute-types Class ClassDef))
-> (let DataTypeName (concat Class Attribute)
DataType (axiom DataTypeName Attribute [attribute Class Type])
(record-attribute-types Class ClassDef))
Class [(@p Attribute Type) | ClassDef]
-> (let DataTypeName (concat Class Attribute)
DataType (axiom DataTypeName Attribute [attribute Class Type])
(record-attribute-types Class ClassDef)))
-> (let DataTypeName (concat Class Attribute)
DataType (axiom DataTypeName Attribute [attribute Class Type])
(record-attribute-types Class ClassDef)))

(declare make-instance [[class Class] --> [instance Class]])

(define make-instance
Class -> (let ClassDef (get-prop Class classdef [])
(if (empty? ClassDef)
(error "class ~A does not exist~%" Class)
ClassDef)))
Class -> (let ClassDef (get-prop Class classdef [])
(if (empty? ClassDef)
(error "class ~A does not exist~%" Class)
ClassDef)))

(declare get-value [[attribute Class A] --> [instance Class] --> A])

(define get-value
Attribute Instance -> (let LookUp (assoc Attribute Instance)
(get-value-test LookUp)))
Attribute Instance -> (let LookUp (assoc Attribute Instance)
(get-value-test LookUp)))

(define get-value-test
[ ] -> (error "no such attribute!~%")
Expand All @@ -64,7 +64,7 @@

(define has-value?
Attribute Instance -> (let LookUp (assoc Attribute Instance)
(has-value-test LookUp)))
(has-value-test LookUp)))

(define has-value-test
[ ] -> (error "no such attribute!~%")
Expand All @@ -75,20 +75,20 @@

(define has-attribute?
Attribute Instance -> (let LookUp (assoc Attribute Instance)
(not (empty? LookUp))))
(not (empty? LookUp))))

(declare change-value [[instance Class] --> [attribute Class A] --> A --> [instance Class]])

(define change-value
_ class _ -> (error "cannot change the class of an instance!~%")
[ ] _ _ -> (error "no such attribute!~%")
[[Attribute | _] | Instance] Attribute Value
-> [[Attribute | Value] | Instance]
[Slot | Instance] Attribute Value
-> [Slot | (change-value Instance Attribute Value)])
_ class _ -> (error "cannot change the class of an instance!~%")
[ ] _ _ -> (error "no such attribute!~%")
[[Attribute | _] | Instance] Attribute Value
-> [[Attribute | Value] | Instance]
[Slot | Instance] Attribute Value
-> [Slot | (change-value Instance Attribute Value)])

(declare instance-of [[instance Class] --> [class Class]])

(define instance-of
[[class | Class] | _] -> Class
_ -> (error "not a class instance!"))
[[class | Class] | _] -> Class
_ -> (error "not a class instance!"))
Loading

0 comments on commit 339b632

Please sign in to comment.