Skip to content

Commit debf3f9

Browse files
authored
Merge pull request #126 from engboris/syntax-macros
Syntax fix and improvement
2 parents 30c8473 + 7e4d0ae commit debf3f9

26 files changed

+345
-240
lines changed

README.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,21 @@ philosophy).
3636
Finite state machine
3737

3838
```
39+
(new-declaration (spec X Y) (:= X Y))
3940
(new-declaration (:: Tested Test)
40-
(== @(exec { @#Tested #Test }) ok))
41+
(== @(interact @#Tested #Test) ok))
4142
4243
(spec binary {
4344
[(-i []) ok]
4445
[(-i [0|X]) (+i X)]
4546
[(-i [1|X]) (+i X)]})
4647
4748
'input words
48-
(:= e (+i []))
49-
(:: e binary)
50-
51-
(:= 0 (+i [0]))
52-
(:: 0 binary)
53-
54-
(:= 000 (+i [0 0 0]))
55-
(:: 000 binary)
56-
57-
(:= 010 (+i [0 1 0]))
58-
(:: 010 binary)
59-
60-
(:= 110 (+i [1 1 0]))
61-
(:: 110 binary)
49+
(:= e (+i [])) (:: e binary)
50+
(:= 0 (+i [0])) (:: 0 binary)
51+
(:= 000 (+i [0 0 0])) (:: 000 binary)
52+
(:= 010 (+i [0 1 0])) (:: 010 binary)
53+
(:= 110 (+i [1 1 0])) (:: 110 binary)
6254
6355
'''
6456
automaton accepting words ending with 00
@@ -77,17 +69,17 @@ automaton accepting words ending with 00
7769
7870
(:= kill (-a _ _))
7971
80-
<show exec { @(exec { @#e #a1 }) #kill }>
81-
<show exec { @(exec { @#000 #a1 }) #kill }>
82-
<show exec { @(exec { @#010 #a1 }) #kill }>
83-
<show exec { @(exec { @#110 #a1 }) #kill }>
72+
(show (process (interact @#e #a1) #kill))
73+
(show (process (interact @#000 #a1) #kill))
74+
(show (process (interact @#010 #a1) #kill))
75+
(show (process (interact @#110 #a1) #kill))
8476
```
8577

8678
More examples can be found in `examples/`.
8779

8880
## Learn
8981

90-
This project is still in (chaotic) development, hence the syntax and features
82+
This project is still in development, hence the syntax and features
9183
are still changing frequently.
9284

9385
To learn more about the current implementation of stellogen:

bin/sgen.ml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@ let parse input_file =
1313
let run input_file =
1414
let expr = parse input_file in
1515
let preprocessed = Expr.preprocess expr in
16-
let p = Expr.program_of_expr preprocessed in
17-
let _ = Stellogen.Sgen_eval.eval_program p in
18-
()
16+
match Expr.program_of_expr preprocessed with
17+
| Ok p ->
18+
let _ = Stellogen.Sgen_eval.eval_program p in
19+
()
20+
| Error e ->
21+
let open Stellogen.Sgen_eval in
22+
begin
23+
match pp_err (ExprError e) with
24+
| Ok pp ->
25+
Out_channel.output_string Out_channel.stderr pp;
26+
()
27+
| Error _ -> ()
28+
end
1929

2030
let preprocess_only input_file =
2131
let expr = parse input_file in

examples/automata.sg

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1+
(new-declaration (spec X Y) (:= X Y))
12
(new-declaration (:: Tested Test)
2-
(== @(exec { @#Tested #Test }) ok))
3+
(== @(interact @#Tested #Test) ok))
34

45
(spec binary {
56
[(-i []) ok]
67
[(-i [0|X]) (+i X)]
78
[(-i [1|X]) (+i X)]})
89

910
'input words
10-
(:= e (+i []))
11-
(:: e binary)
12-
13-
(:= 0 (+i [0]))
14-
(:: 0 binary)
15-
16-
(:= 000 (+i [0 0 0]))
17-
(:: 000 binary)
18-
19-
(:= 010 (+i [0 1 0]))
20-
(:: 010 binary)
21-
22-
(:= 110 (+i [1 1 0]))
23-
(:: 110 binary)
11+
(:= e (+i [])) (:: e binary)
12+
(:= 0 (+i [0])) (:: 0 binary)
13+
(:= 000 (+i [0 0 0])) (:: 000 binary)
14+
(:= 010 (+i [0 1 0])) (:: 010 binary)
15+
(:= 110 (+i [1 1 0])) (:: 110 binary)
2416

2517
(:= (initial Q) [(-i W) (+a W Q)])
2618
(:= (accept Q) [(-a [] Q) accept])
@@ -39,7 +31,7 @@ automaton accepting words ending with 00
3931

4032
(:= kill (-a _ _))
4133

42-
<show exec { @(exec { @#e #a1 }) #kill }>
43-
<show exec { @(exec { @#000 #a1 }) #kill }>
44-
<show exec { @(exec { @#010 #a1 }) #kill }>
45-
<show exec { @(exec { @#110 #a1 }) #kill }>
34+
(show (process (interact @#e #a1) #kill))
35+
(show (process (interact @#000 #a1) #kill))
36+
(show (process (interact @#010 #a1) #kill))
37+
(show (process (interact @#110 #a1) #kill))

examples/binary4.sg

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
1-
(spec u4 [(-b _ 1 _) (-b _ 2 _) (-b _ 3 _) (-b _ 4 _) ok])
2-
1+
(new-declaration (spec X Y) (:= X Y))
32
(new-declaration (:: Tested Test)
4-
(== @(exec (process #Test #Tested)) ok))
3+
(== @(interact (process #Test #Tested)) ok))
4+
5+
(spec u4 [(-b _ 1 _) (-b _ 2 _) (-b _ 3 _) (-b _ 4 _) ok])
56

67
(:= (make_bin Name X1 X2 X3 X4)
78
{ [(+b Name 1 X1)] [(+b Name 2 X2)] [(+b Name 3 X3)] [(+b Name 4 X4)] })
89

9-
(:= b1 #(make_bin b1 0 0 0 1))
10-
(:: b1 u4)
11-
12-
(:= b2 #(make_bin b2 0 0 1 1))
13-
(:: b2 u4)
10+
(:= b1 #(make_bin b1 0 0 0 1)) (:: b1 u4)
11+
(:= b2 #(make_bin b2 0 0 1 1)) (:: b2 u4)
1412

1513
(show #b1)
1614
(show #b2)
1715

16+
(:= (if A = X then R = Z) [(-b A I X) (+b R I Z)])
1817
(:= (if A = X and B = Y then R = Z) [(-b A I X) (-b B I Y) (+b R I Z)])
1918

20-
'''
21-
'FIXME
22-
23-
(:= (and AA BB RR) {
24-
#(if AA = 0 and BB = XX then RR = 0)
25-
#(if AA = 1 and BB = XX then RR = XX) })
26-
(show #(and b1 b2 r1))
27-
(show (process #b1 #(and b1 b2 r1) #b2))
19+
(:= (and A B R) {
20+
#(if A = 0 and B = _ then R = 0)
21+
#(if A = 1 and B = X then R = X) })
22+
(:= rand (process #b1 #(and b1 b2 r) #b2))
23+
(show #rand)
24+
(== #rand #(make_bin r 0 0 0 1))
2825

2926
(:= (or A B R) {
30-
[(-b A I 0) (-b B I X) (+b R I X)]
31-
[(-b A I 1) (-b B I X) (+b R I 1)]})
32-
33-
(:= (xor A B R) {
34-
[(-b A I 1) (-b B I 0) (+b R I 1)]
35-
[(-b A I 0) (-b B I 1) (+b R I 1)]
36-
[(-b A I 0) (-b B I 0) (+b R I 0)]
37-
[(-b A I 1) (-b B I 1) (+b R I 0)]})
27+
#(if A = 0 and B = X then R = X)
28+
#(if A = 1 and B = Y then R = 1) })
29+
(:= ror (process #b1 #(or b1 b2 r) #b2))
30+
(show #ror)
31+
(== #ror #(make_bin r 0 0 1 1))
32+
33+
(:= (not A R) {
34+
#(if A = 1 then R = 0)
35+
#(if A = 0 then R = 1) })
36+
(:= rnot (process #b1 #(not b1 r)))
37+
(show #rnot)
38+
(== #rnot #(make_bin r 1 1 1 0))
39+
40+
(:= rnand (process #b1 #(and b1 b2 r1) #b2 #(not r1 r2)))
41+
(show #rnand)
42+
43+
'''
44+
(:= rxor (process #rnand #(and r2 r r3) #ror))
45+
(show #rxor)
3846
'''

examples/circuits.sg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ FIXME
88
[(+not 1 0)] [(+not 0 1)]
99
[(+and 1 X X)][(+and 0 X 0)]})
1010

11-
<show exec (process
11+
<show interact (process
1212
'inputs
1313
[(-1 X) (+c0 X)]
1414
'layer 1
@@ -21,7 +21,7 @@ FIXME
2121
[(-c4 R) R]
2222
#semantics)>
2323

24-
<show exec (process
24+
<show interact (process
2525
'inputs
2626
{
2727
[(-0 X) (+c0 X)]

examples/lambda.sg

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[(-id X) (-arg X)]
88
@[(+arg [r|X]) (out X)]})
99

10-
<show exec { #id #id_arg #linker }>
10+
(show (interact #id #id_arg #linker))
1111

1212
' id x
1313
(:= var_x [(x (exp X Y)) (+arg (exp [l|X] Y))])
@@ -16,7 +16,7 @@
1616
[(-id X) (-arg X)]
1717
@[(+arg [r|X]) (out X)]})
1818

19-
<show exec { #id #var_x #linker }>
19+
(show (interact #id #var_x #linker))
2020

2121
' lproj x
2222
(:= lproj {

examples/linear_lambda.sg

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
(new-declaration (spec X Y) (:= X Y))
12
(new-declaration (:: Tested Test)
2-
(== @(exec { #Tested #Test }) ok))
3+
(== @(interact #Tested #Test) ok))
34

45
' identity function (\x -> x)
56
(:= id [(+id [l|X]) (+id [r|X])])
@@ -11,7 +12,7 @@
1112
[(-id X) (-arg X)]
1213
@[(+arg [r|X]) (out X)]])
1314

14-
<show exec { #id #id_arg #linker }>
15+
(show (interact #id #id_arg #linker))
1516

1617
' id x
1718
(:= x_arg [(x X) (+arg [l X])])
@@ -20,7 +21,7 @@
2021
[(-id X) (-arg X)]
2122
@[(+arg [r|X]) (out X)]])
2223

23-
<show exec { #id #x_arg #linker }>
24+
(show (interact #id #x_arg #linker))
2425

2526
' linear types
2627
(spec (larrow a a) {

examples/mall.sg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
(:= cut [(-5 X) (-3 X)])
1111

12-
<show exec (process
12+
<show interact (process
1313
#with
1414
{ #plus #cut })>

examples/mll.sg

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
(new-declaration (spec X Y) (:= X Y))
12
(new-declaration (:: Tested Test)
2-
(== @(exec { #Tested #Test }) ok))
3+
(== @(interact #Tested #Test) ok))
34

45
' test of linear identity
56
(spec (larrow a a) {
@@ -42,17 +43,17 @@
4243
[+cuts [
4344
[(-7 X) (-8 X)]]]})
4445

45-
(:= vehicle <eval exec { #ps1 @[-vehicle] }>)
46-
(:= cuts <eval exec { #ps1 @[-cuts] }>)
46+
(:= vehicle (eval (interact #ps1 @[-vehicle])))
47+
(:= cuts (eval (interact #ps1 @[-cuts])))
4748

48-
<show exec { #vehicle #cuts }>
49+
(show (interact #vehicle #cuts))
4950

5051
(spec (tens a b) {
5152
[(-1 [g|X]) (-2 [g|X]) (+3 [g|X])]
5253
@[(-3 [g|X]) ok]})
5354

5455
(new-declaration (::lin Tested Test)
55-
(== @(linexec { #Tested #Test }) ok))
56+
(== @(fire #Tested #Test) ok))
5657

5758
' does not typecheck
5859
' (::lin vehicle ((tens a a) linear))

examples/nat.sg

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1+
(new-declaration (spec X Y) (:= X Y))
12
(new-declaration (:: Tested Test)
2-
(== @(exec { @#Tested #Test }) ok))
3+
(== @(interact @#Tested #Test) ok))
34

45
(spec nat {
56
[(-nat 0) ok]
67
[(-nat (s N)) (+nat N)]})
78

8-
(:= 0 (+nat 0))
9-
(:: 0 nat)
10-
11-
(:= 1 (+nat (s 0)))
12-
(:: 1 nat)
13-
14-
(:= 2 <+nat s s 0>)
15-
(:: 2 nat)
9+
(:= 0 (+nat 0)) (:: 0 nat)
10+
(:= 1 (+nat (s 0))) (:: 1 nat)
11+
(:= 2 <+nat s s 0>) (:: 2 nat)
1612

1713
(:= add1 [(-nat X) (+nat (s X))])
1814

1915
(:= is_empty {
2016
[(-nat 0) (res 1)]
2117
[(-nat (s _)) (res 0)]})
2218

23-
<show exec { @#add1 #2 }>
24-
<show exec { #is_empty @#0 }>
25-
<show exec { #is_empty @#1 }>
19+
(show (interact @#add1 #2))
20+
(show (interact #is_empty @#0))
21+
(show (interact #is_empty @#1))

0 commit comments

Comments
 (0)