shen-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. data/.gitignore +4 -0
  2. data/.rspec +0 -0
  3. data/Gemfile +6 -0
  4. data/Gemfile.lock +20 -0
  5. data/MIT_LICENSE.txt +26 -0
  6. data/README.md +94 -0
  7. data/bin/shen_test_suite.rb +9 -0
  8. data/bin/srrepl +23 -0
  9. data/lib/kl.rb +7 -0
  10. data/lib/kl/absvector.rb +12 -0
  11. data/lib/kl/compiler.rb +253 -0
  12. data/lib/kl/cons.rb +51 -0
  13. data/lib/kl/empty_list.rb +12 -0
  14. data/lib/kl/environment.rb +123 -0
  15. data/lib/kl/error.rb +4 -0
  16. data/lib/kl/internal_error.rb +7 -0
  17. data/lib/kl/lexer.rb +186 -0
  18. data/lib/kl/primitives/arithmetic.rb +60 -0
  19. data/lib/kl/primitives/assignments.rb +18 -0
  20. data/lib/kl/primitives/booleans.rb +17 -0
  21. data/lib/kl/primitives/error_handling.rb +13 -0
  22. data/lib/kl/primitives/generic_functions.rb +22 -0
  23. data/lib/kl/primitives/lists.rb +21 -0
  24. data/lib/kl/primitives/streams.rb +38 -0
  25. data/lib/kl/primitives/strings.rb +55 -0
  26. data/lib/kl/primitives/symbols.rb +17 -0
  27. data/lib/kl/primitives/time.rb +17 -0
  28. data/lib/kl/primitives/vectors.rb +30 -0
  29. data/lib/kl/reader.rb +40 -0
  30. data/lib/kl/trampoline.rb +14 -0
  31. data/lib/shen_ruby.rb +7 -0
  32. data/lib/shen_ruby/version.rb +3 -0
  33. data/shen-ruby.gemspec +26 -0
  34. data/shen/README.txt +17 -0
  35. data/shen/lib/shen_ruby/shen.rb +124 -0
  36. data/shen/license.txt +34 -0
  37. data/shen/release/benchmarks/N_queens.shen +45 -0
  38. data/shen/release/benchmarks/README.shen +14 -0
  39. data/shen/release/benchmarks/benchmarks.shen +56 -0
  40. data/shen/release/benchmarks/bigprog +2173 -0
  41. data/shen/release/benchmarks/br.shen +13 -0
  42. data/shen/release/benchmarks/einstein.shen +33 -0
  43. data/shen/release/benchmarks/heatwave.gif +0 -0
  44. data/shen/release/benchmarks/interpreter.shen +219 -0
  45. data/shen/release/benchmarks/picture.jpg +0 -0
  46. data/shen/release/benchmarks/plato.jpg +0 -0
  47. data/shen/release/benchmarks/powerset.shen +10 -0
  48. data/shen/release/benchmarks/prime.shen +10 -0
  49. data/shen/release/benchmarks/short.shen +129 -0
  50. data/shen/release/benchmarks/text.txt +68 -0
  51. data/shen/release/k_lambda/core.kl +1002 -0
  52. data/shen/release/k_lambda/declarations.kl +1021 -0
  53. data/shen/release/k_lambda/load.kl +94 -0
  54. data/shen/release/k_lambda/macros.kl +479 -0
  55. data/shen/release/k_lambda/prolog.kl +1309 -0
  56. data/shen/release/k_lambda/reader.kl +1058 -0
  57. data/shen/release/k_lambda/sequent.kl +556 -0
  58. data/shen/release/k_lambda/sys.kl +582 -0
  59. data/shen/release/k_lambda/t-star.kl +3493 -0
  60. data/shen/release/k_lambda/toplevel.kl +223 -0
  61. data/shen/release/k_lambda/track.kl +208 -0
  62. data/shen/release/k_lambda/types.kl +455 -0
  63. data/shen/release/k_lambda/writer.kl +108 -0
  64. data/shen/release/k_lambda/yacc.kl +280 -0
  65. data/shen/release/test_programs/Chap13/problems.txt +26 -0
  66. data/shen/release/test_programs/README.shen +53 -0
  67. data/shen/release/test_programs/TinyLispFunctions.txt +16 -0
  68. data/shen/release/test_programs/TinyTypes.shen +55 -0
  69. data/shen/release/test_programs/binary.shen +24 -0
  70. data/shen/release/test_programs/bubble_version_1.shen +28 -0
  71. data/shen/release/test_programs/bubble_version_2.shen +22 -0
  72. data/shen/release/test_programs/calculator.shen +21 -0
  73. data/shen/release/test_programs/cartprod.shen +23 -0
  74. data/shen/release/test_programs/change.shen +25 -0
  75. data/shen/release/test_programs/classes-defaults.shen +94 -0
  76. data/shen/release/test_programs/classes-inheritance.shen +100 -0
  77. data/shen/release/test_programs/classes-typed.shen +74 -0
  78. data/shen/release/test_programs/classes-untyped.shen +46 -0
  79. data/shen/release/test_programs/depth_.shen +14 -0
  80. data/shen/release/test_programs/einstein.shen +33 -0
  81. data/shen/release/test_programs/fruit_machine.shen +46 -0
  82. data/shen/release/test_programs/interpreter.shen +219 -0
  83. data/shen/release/test_programs/metaprog.shen +85 -0
  84. data/shen/release/test_programs/minim.shen +193 -0
  85. data/shen/release/test_programs/mutual.shen +11 -0
  86. data/shen/release/test_programs/n_queens.shen +45 -0
  87. data/shen/release/test_programs/newton_version_1.shen +33 -0
  88. data/shen/release/test_programs/newton_version_2.shen +24 -0
  89. data/shen/release/test_programs/parse.prl +14 -0
  90. data/shen/release/test_programs/parser.shen +52 -0
  91. data/shen/release/test_programs/powerset.shen +10 -0
  92. data/shen/release/test_programs/prime.shen +10 -0
  93. data/shen/release/test_programs/proof_assistant.shen +81 -0
  94. data/shen/release/test_programs/proplog_version_1.shen +25 -0
  95. data/shen/release/test_programs/proplog_version_2.shen +27 -0
  96. data/shen/release/test_programs/qmachine.shen +67 -0
  97. data/shen/release/test_programs/red-black.shen +55 -0
  98. data/shen/release/test_programs/search.shen +56 -0
  99. data/shen/release/test_programs/semantic_net.shen +44 -0
  100. data/shen/release/test_programs/spreadsheet.shen +35 -0
  101. data/shen/release/test_programs/stack.shen +27 -0
  102. data/shen/release/test_programs/streams.shen +20 -0
  103. data/shen/release/test_programs/strings.shen +59 -0
  104. data/shen/release/test_programs/structures-typed.shen +71 -0
  105. data/shen/release/test_programs/structures-untyped.shen +42 -0
  106. data/shen/release/test_programs/tests.shen +294 -0
  107. data/shen/release/test_programs/types.shen +11 -0
  108. data/shen/release/test_programs/whist.shen +240 -0
  109. data/shen/release/test_programs/yacc.shen +136 -0
  110. data/spec/kl/cons_spec.rb +12 -0
  111. data/spec/kl/environment_spec.rb +306 -0
  112. data/spec/kl/lexer_spec.rb +149 -0
  113. data/spec/kl/primitives/generic_functions_spec.rb +29 -0
  114. data/spec/kl/primitives/symbols_spec.rb +21 -0
  115. data/spec/kl/reader_spec.rb +36 -0
  116. data/spec/spec_helper.rb +2 -0
  117. metadata +189 -0
@@ -0,0 +1,25 @@
1
+ (define backchain
2
+ Conc Assumptions -> (backchain* [Conc] Assumptions Assumptions))
3
+
4
+ (define backchain*
5
+ [] _ _ -> proved
6
+ [[P & Q] | Goals] _ Assumptions
7
+ -> (backchain* [P Q | Goals] Assumptions Assumptions)
8
+ [P | Goals] [[P <= | Subgoal] | _] Assumptions
9
+ <- (backchain* (append Subgoal Goals) Assumptions Assumptions)
10
+ Goals [_ | Rest] Assumptions -> (backchain* Goals Rest Assumptions)
11
+ _ _ _ -> (fail))
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
@@ -0,0 +1,27 @@
1
+ (define backchain
2
+ Conc Assumptions -> (backchain* Conc Assumptions Assumptions))
3
+
4
+ (define backchain*
5
+ P [P | _] _ -> true
6
+ [P & Q] _ Assumptions
7
+ -> (and (backchain* P Assumptions Assumptions)
8
+ (backchain* Q Assumptions Assumptions))
9
+ P [[P <= Q] | _] Assumptions
10
+ <- (fail-if (/. X (= X false)) (backchain* Q Assumptions Assumptions))
11
+ P [_ | Rest] Assumptions -> (backchain* P Rest Assumptions)
12
+ _ _ _ -> false)
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+
@@ -0,0 +1,67 @@
1
+ (datatype progression
2
+
3
+ X : A; S : (A --> A); E : (A --> boolean);
4
+ ==========================================
5
+ [X S E] : (progression A);)
6
+
7
+ (define force
8
+ {(progression A) --> A}
9
+ [X S E] -> X)
10
+
11
+ (define delay
12
+ {(progression A) --> (progression A)}
13
+ [X S E] -> [(S X) S E])
14
+
15
+ (define end?
16
+ {(progression A) --> boolean}
17
+ [X S E] -> (E X))
18
+
19
+ (define push
20
+ {A --> (progression A) --> (progression A)}
21
+ X [Y S E] -> [X (/. Z (if (= Z X) Y (S Z))) E])
22
+
23
+ (define forall
24
+ {(progression A) --> (A --> boolean) --> boolean}
25
+ [X S E] P -> (if (E X) true (and (P X) (forall [(S X) S E] P))))
26
+
27
+ (define exists
28
+ {(progression A) --> (A --> boolean) --> boolean}
29
+ [X S E] P -> (if (E X) false (or (P X) (exists [(S X) S E] P))))
30
+
31
+ (define super
32
+ {(progression A) --> (A --> B) --> (B --> C --> C) --> C --> C}
33
+ [X S E] P F Y -> (if (E X) Y (F (P X) (super [(S X) S E] P F Y))))
34
+
35
+ (define forall
36
+ {(progression A) --> (A --> boolean) --> boolean}
37
+ Progression P -> (super Progression P and true))
38
+
39
+ (define exists
40
+ {(progression A) --> (A --> boolean) --> boolean}
41
+ Progression P -> (super Progression P or false))
42
+
43
+ (define for
44
+ {(progression A) --> (A --> B) --> number}
45
+ Progression P -> (super Progression P progn 0))
46
+
47
+ (define progn
48
+ {A --> B --> B}
49
+ X Y -> Y)
50
+
51
+ (define filter
52
+ {(progression A) --> (A --> boolean) --> (list A)}
53
+ Progression P -> (super Progression (/. X (if (P X) [X] [])) append []))
54
+
55
+ (define next-prime
56
+ {number --> number}
57
+ N -> (if (prime? (+ N 1)) (+ N 1) (next-prime (+ N 1))))
58
+
59
+ (define prime?
60
+ {number --> boolean}
61
+ X -> (prime-help X (/ X 2) 2))
62
+
63
+ (define prime-help
64
+ {number --> number --> number --> boolean}
65
+ X Max Div -> false where (integer? (/ X Div))
66
+ X Max Div -> true where (> Div Max)
67
+ X Max Div -> (prime-help X Max (+ 1 Div)))
@@ -0,0 +1,55 @@
1
+ \**\ \* Copyright (c) 2011, Justin Grant <justin at imagine27 dot com> *\ \* All rights reserved. *\ \**\ \* Redistribution and use in source and binary forms, with or without modification, *\ \* are permitted provided that the following conditions are met: *\ \**\ \* Redistributions of source code must retain the above copyright notice, this list *\ \* of conditions and the following disclaimer. *\ \* Redistributions in binary form must reproduce the above copyright notice, this *\ \* list of conditions and the following disclaimer in the documentation and/or *\ \* other materials provided with the distribution. *\ \* Neither the name of the <ORGANIZATION> nor the names of its contributors may be *\ \* used to endorse or promote products derived from this software without specific *\ \* prior written permission. *\ \**\ \* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND *\ \* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED *\ \* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE *\ \* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR *\ \* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *\ \* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; *\ \* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *\ \* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *\ \* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, *\ \* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *\ \**\ (datatype tree-node
2
+
3
+ Key : number; Val : B;
4
+ ======================
5
+ [Key Val] : tree-node;)
6
+
7
+ (datatype color
8
+
9
+ if (element? Color [red black])
10
+ _______________________________
11
+ Color : color;)
12
+
13
+ (datatype tree
14
+
15
+ if (empty? Tree)
16
+ ________________
17
+ Tree : tree;
18
+
19
+ Color : color; LTree : tree; TreeNode : tree-node; RTree : tree;
20
+ ================================================================
21
+ [Color LTree TreeNode RTree] : tree;)
22
+
23
+ (define node-key
24
+ {tree-node --> number}
25
+ [Key Val] -> Key)
26
+
27
+ (define make-tree-black
28
+ {tree --> tree}
29
+ [Color A X B] -> [black A X B])
30
+
31
+ (define member
32
+ {tree-node --> tree --> boolean}
33
+ X [] -> false
34
+ X [Color A Y B] -> (cases (< (node-key X) (node-key Y)) (member X A)
35
+ (< (node-key Y) (node-key X)) (member X B)
36
+ true true))
37
+
38
+ (define balance
39
+ {tree --> tree}
40
+ [black [red [red A X B] Y C] Z D] -> [red [black A X B] Y [black C Z D]]
41
+ [black [red A X [red B Y C]] Z D] -> [red [black A X B] Y [black C Z D]]
42
+ [black A X [red [red B Y C] Z D]] -> [red [black A X B] Y [black C Z D]]
43
+ [black A X [red B Y [red C Z D]]] -> [red [black A X B] Y [black C Z D]]
44
+ S -> S)
45
+
46
+ (define insert-
47
+ {tree-node --> tree --> tree}
48
+ X [] -> [red [] X []]
49
+ X [Color A Y B] -> (cases (< (node-key X) (node-key Y)) (balance [Color (insert- X A) Y B])
50
+ (< (node-key Y) (node-key X)) (balance [Color A Y (insert- X B)])
51
+ true [Color A Y B]))
52
+
53
+ (define insert
54
+ {tree-node --> tree --> tree}
55
+ X S -> (make-tree-black (insert- X S)))
@@ -0,0 +1,56 @@
1
+ (define breadth-first
2
+ {state --> (state --> (list state)) --> (state --> boolean) --> boolean}
3
+ Start F Test -> (b* F Test (F Start)))
4
+
5
+ (define b*
6
+ {(state --> (list state)) --> (state --> boolean) --> (list state) --> boolean}
7
+ F Test States -> true where (some Test States)
8
+ F Test States -> (let NewStates (mapcan F States)
9
+ (if (empty? NewStates)
10
+ false
11
+ (b* F Test NewStates))))
12
+
13
+ (define some
14
+ {(A --> boolean) --> (list A) --> boolean}
15
+ Test [] -> false
16
+ Test [X|Y] -> (or (Test X) (some Test Y)))
17
+
18
+ (define depth
19
+ {state --> (state --> (list state)) --> (state --> boolean) --> boolean}
20
+ Start _ Test -> true where (Test Start)
21
+ Start F Test -> (d* F Test (F Start)))
22
+
23
+ (define d*
24
+ {(state --> (list state)) --> (state --> boolean) --> (list state) --> boolean}
25
+ _ Test [State | _] -> true where (Test State)
26
+ F Test [State | States] <- (fail-if (= false) (d* F Test (F State)))
27
+ F Test [_ | States] -> (d* F Test States)
28
+ _ _ _ -> false)
29
+
30
+ (define hill
31
+ {(state --> number) --> state --> (state --> (list state)) --> (state --> boolean) --> boolean}
32
+ _ Start _ Test -> true where (Test Start)
33
+ E Start F Test -> (h* E F Test (order_states E (F Start))))
34
+
35
+ (define h*
36
+ {(state --> number) --> (state --> (list state)) --> (state --> boolean) --> (list state) --> boolean}
37
+ _ _ Test [State | _] -> true where (Test State)
38
+ E F Test [State | States]
39
+ <- (fail-if (/. X (= X false)) (h* E F Test (order_states E (F State))))
40
+ E F Test [_ | States] -> (h* E F Test States)
41
+ _ _ _ _ -> false)
42
+
43
+ (define order_states
44
+ {(state --> number) --> (list state) --> (list state)}
45
+ E States -> (sort (/. S1 (/. S2 (> (E S1) (E S2)))) States))
46
+
47
+ (define sort
48
+ {(A --> (A --> boolean)) --> (list A) --> (list A)}
49
+ R X -> (fix (/. Y (sort* R Y)) X))
50
+
51
+ (define sort*
52
+ {(A --> A --> boolean) --> (list A) --> (list A)}
53
+ _ [] -> []
54
+ _ [X] -> [X]
55
+ R [X Y | Z] -> [Y | (sort* R [X | Z])] where (R Y X)
56
+ R [X Y | Z] -> [X | (sort* R [Y | Z])])
@@ -0,0 +1,44 @@
1
+ (define query
2
+ [is Object Concept] -> (if (belongs? Object Concept) yes no))
3
+
4
+ (define belongs?
5
+ Object Concept -> (element? Concept (fix spread-activation [Object])))
6
+
7
+ (define spread-activation
8
+ [] -> []
9
+ [Vertex | Vertices] -> (union (accessible-from Vertex)
10
+ (spread-activation Vertices)))
11
+
12
+ (define accessible-from
13
+ Vertex -> [Vertex | (union (is_links Vertex) (type_links Vertex))])
14
+
15
+ (define is_links
16
+ Vertex -> (get-prop Vertex is_a []))
17
+
18
+ (define type_links
19
+ Vertex -> (get-prop Vertex type_of []))
20
+
21
+ (define assert
22
+ [Object is_a Type] -> (put Object is_a [Type | (is_links Object)])
23
+ [Type1 type_of Type2] -> (put Type1 type_of [Type2 | (type_links Type1)]))
24
+
25
+ (define get-prop
26
+ Ob Pointer Default -> (trap-error (get Ob Pointer) (/. E Default)))
27
+
28
+ (define clear
29
+ Ob -> (put Ob is_a (put Ob type_of [])))
30
+
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
@@ -0,0 +1,35 @@
1
+ (define assess-spreadsheet
2
+ Spreadsheet -> (map (/. Row (assign-fixed-values Row Spreadsheet))
3
+ Spreadsheet))
4
+
5
+ (define assign-fixed-values
6
+ [Index | Cells] Spreadsheet
7
+ -> [Index | (map (/. Cell (assign-cell-value Cell Spreadsheet)) Cells)])
8
+
9
+ (define assign-cell-value
10
+ [Attribute Value] _ -> [Attribute Value] where (fixed-value? Value)
11
+ [Attribute Value] Spreadsheet -> [Attribute (Value Spreadsheet)])
12
+
13
+ (define fixed-value?
14
+ \* number?, symbol? and string? are system functions - see appendix A *\
15
+ Value -> (or (number? Value) (or (symbol? Value) (string? Value))))
16
+
17
+ (define get'
18
+ \* spreads the spreadsheet! *\
19
+ Index Attribute Spreadsheet
20
+ -> (get-row Index Attribute Spreadsheet Spreadsheet))
21
+
22
+ (define get-row
23
+ \* looks for the right row using the index *\
24
+ Index Attribute [[Index | Cells] | _] Spreadsheet
25
+ -> (get-cell Attribute Cells Spreadsheet)
26
+ Index Attribute [_ | Rows] Spreadsheet
27
+ -> (get-row Index Attribute Rows Spreadsheet)
28
+ Index _ _ _ -> (error "Index ~A not found" Index))
29
+
30
+ (define get-cell
31
+ Attribute [[Attribute Value] | _] Spreadsheet
32
+ -> (if (fixed-value? Value) Value (Value Spreadsheet))
33
+ Attribute [_ | Cells] Spreadsheet
34
+ -> (get-cell Attribute Cells Spreadsheet)
35
+ Attribute _ _ -> (error "Attribute ~A not found" Attribute))
@@ -0,0 +1,27 @@
1
+ (declare empty-stack [A --> [stack B]])
2
+
3
+ (declare push [A --> [stack A] --> [stack A]])
4
+
5
+ (declare top [[stack A] --> A])
6
+
7
+ (declare pop [[stack A] --> [stack A]])
8
+
9
+ (define empty-stack
10
+ _ -> (/. X (if (or (= X pop) (= X top))
11
+ (error "this stack is empty~%")
12
+ (error "~A is not an operation on stacks.~%" X))))
13
+
14
+ (define push
15
+ X S -> (/. Y (if (= Y pop)
16
+ S
17
+ (if (= Y top)
18
+ X
19
+ (error "~A is not an operation on stacks.~%" Y)))))
20
+
21
+ (define top
22
+ S -> (S top))
23
+
24
+ (define pop
25
+ S -> (S pop))
26
+
27
+
@@ -0,0 +1,20 @@
1
+ (datatype progression
2
+
3
+ X : (A * (A --> A) * (A --> boolean));
4
+ ======================================
5
+ X : (progression A);)
6
+
7
+ (define delay
8
+ {(progression A) --> (progression A)}
9
+ (@p X F E) -> (if (not (E X))
10
+ (@p (F X) F E)
11
+ (error "progression exhausted!~%")))
12
+
13
+ (define force
14
+ {(progression A) --> A}
15
+ (@p X F E) -> X)
16
+
17
+ (define end?
18
+ {(progression A) --> boolean}
19
+ (@p X _ E) -> (E X))
20
+
@@ -0,0 +1,59 @@
1
+ \* Replace the first occurrence of a string Rem by Rep *\
2
+ (define subst-string
3
+ {string --> string --> string --> string}
4
+ _ _ "" -> ""
5
+ Rep (@s S Ss) (@s S Ss') <- (fail-if (= "failed!") (subst-string' Rep Ss Ss'))
6
+ Rep Rem (@s S Ss) -> (@s S (subst-string Rep Rem Ss)))
7
+
8
+ (define subst-string'
9
+ {string --> string --> string --> string}
10
+ Rep "" Ss -> (@s Rep Ss)
11
+ Rep (@s S Ss) (@s S Ss') -> (subst-string' Rep Ss Ss')
12
+ _ _ _ -> "failed!")
13
+
14
+
15
+ (define rwilli
16
+ {string --> string}
17
+ "" -> ""
18
+ (@s "Willi" Ss) -> (rwilli Ss)
19
+ (@s _ Ss) -> (rwilli Ss))
20
+
21
+
22
+ \* Length of a string. *\
23
+ (define strlen
24
+ {string --> number}
25
+ "" -> 0
26
+ (@s _ S) -> (+ 1 (strlen S)))
27
+
28
+ \* Trim characters from the front of a string. *\
29
+ (define trim-string-left
30
+ {(list string) --> string --> string}
31
+ _ "" -> ""
32
+ Trim (@s S Ss) -> (@s S Ss) where (not (element? S Trim))
33
+ Trim (@s _ Ss) -> (trim-string-left Trim Ss))
34
+
35
+ \* Trim characters from the end of a string. *\
36
+ (define trim-string-right
37
+ {(list string) --> string --> string}
38
+ Trim S -> (reverse-string (trim-string-left Trim (reverse-string S))))
39
+
40
+ \* Trim characters from the front and end of a string *\
41
+ (define trim-string
42
+ {(list string) --> string --> string}
43
+ Trim S -> (reverse-string (trim-string-left Trim (reverse-string (trim-string-left Trim S)))))
44
+
45
+ \* Reverse a string. *\
46
+ (define reverse-string
47
+ {string --> string}
48
+ "" -> ""
49
+ (@s S Ss) -> (@s (reverse-string Ss) S))
50
+
51
+ \* A string of digits? *\
52
+ (define alldigits?
53
+ {string --> boolean}
54
+ "" -> true
55
+ (@s S Ss) -> (and (digit? S) (alldigits? Ss)))
56
+
57
+ (define digit?
58
+ {string --> boolean}
59
+ S -> (element? S ["0" "1" "2" "3" "4" "5" "6" "7" "8" "9"]))
@@ -0,0 +1,71 @@
1
+ (define defstruct
2
+ Name Slots
3
+ -> (let Attributes (map fst Slots)
4
+ Types (map snd Slots)
5
+ Selectors (selectors Name Attributes)
6
+ Constructor (constructor Name Attributes)
7
+ Recognisor (recognisor Name)
8
+ ConstructorType (constructor-type Name Types)
9
+ SelectorTypes (selector-types Name Attributes Types)
10
+ RecognisorType (recognisor-type Name)
11
+ Name))
12
+
13
+ (define selector-types
14
+ _ [] [] -> (gensym (protect X))
15
+ Name [Attribute | Attributes] [Type | Types]
16
+ -> (let Selector (concat Name (concat - Attribute))
17
+ SelectorType [Name --> Type]
18
+ TypeDecl (declare Selector SelectorType)
19
+ (selector-types Name Attributes Types)))
20
+
21
+ (define recognisor-type
22
+ Name -> (let Recognisor (concat Name ?)
23
+ (declare Recognisor [Name --> boolean])))
24
+
25
+ (define constructor-type
26
+ Name Types -> (let Constructor (concat make- Name)
27
+ Type (assemble-type Types Name)
28
+ (declare Constructor Type)))
29
+
30
+ (define assemble-type
31
+ [ ] Name -> Name
32
+ [Type | Types] Name -> [Type --> (assemble-type Types Name)])
33
+
34
+ (declare defstruct [symbol --> [list [symbol * symbol]] --> symbol])
35
+
36
+ (define selectors
37
+ Name Attributes -> (map (/. A (selector Name A)) Attributes))
38
+
39
+ (define selector
40
+ Name Attribute
41
+ -> (let SelectorName (concat Name (concat - Attribute))
42
+ (eval [define SelectorName
43
+ (protect Structure) -> [let (protect LookUp) [assoc Attribute (protect Structure)]
44
+ [if [empty? (protect LookUp)]
45
+ [error "~A is not an attribute of ~A.~%"
46
+ Attribute Name]
47
+ [tail (protect LookUp)]]]])))
48
+
49
+ (define constructor
50
+ Name Attributes
51
+ -> (let ConstructorName (concat make- Name)
52
+ Parameters (params Attributes)
53
+ (eval [define ConstructorName |
54
+ (append Parameters
55
+ [-> [cons [cons structure Name]
56
+ (make-association-list Attributes
57
+ Parameters)]])])))
58
+
59
+ (define params
60
+ [] -> []
61
+ [_ | Attributes] -> [(gensym (protect X)) | (params Attributes)])
62
+
63
+ (define make-association-list
64
+ [] [] -> []
65
+ [A | As] [P | Ps] -> [cons [cons A P] (make-association-list As Ps)])
66
+
67
+ (define recognisor
68
+ Name -> (let RecognisorName (concat Name ?)
69
+ (eval [define RecognisorName
70
+ [cons [cons structure Name] _] -> true
71
+ _ -> false])))