-
Notifications
You must be signed in to change notification settings - Fork 0
/
FreeIO.cs
212 lines (172 loc) · 7.04 KB
/
FreeIO.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Modified from the original https://gist.github.com/louthy/524fbe8965d3a2aae1b576cdd8e971e4
- removed dependency on [language-ext](https://github.com/louthy/language-ext)
- separated monadic boilerplate, so you may concentrate on describing the operations and interpretation of the program
- removed `IO<A>.Faulted` to simplify the examples. It can be added back in straightforward manner.
Useful links:
- [John DeGoes: Beyond Free Monads - λC Winter Retreat 2017](https://www.youtube.com/watch?v=A-lmrvsUi2Y)
- [Free and Tagless compared - how not to commit to a monad too early](https://softwaremill.com/free-tagless-compared-how-not-to-commit-to-monad-too-early)
- [John A De Goes - ZIO: Next-Generation Effects in Scala 2019](https://www.youtube.com/watch?v=mkSHhsJXjdc)
Requires C# 7.2
For `LiveRunner` to work you need "d:/some_text_file.txt" with couple of lines of text
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace FreeIO
{
using static Unit;
using static NumberLinesOperations;
public class Program
{
public static async Task Main()
{
// Describe program without running it:
var program = NumberLines(@"C:\Dev\SharpIO\some_text_file.txt");
// Actually running the program using different runners (interpreters):
TestRunner.Run(program);
TestRunner.Run(program, skipLogging: true);
LiveRunner.Run(program);
await AsyncLiveRunner.RunAsync(program);
}
// Program description
private static IO<Unit> NumberLines(string path) =>
from lines in ReadAllLines(path)
from _1 in Log($"There are {lines.Count()} lines")
from _2 in Log("Pre-pending the line numbers")
let newLines = Enumerable.Range(1, int.MaxValue).Zip(lines, (i, line) => $"{i}: {line}")
let newFile = path + ".prefixed"
from _3 in WriteAllLines(newFile, newLines)
from _4 in Log($"Lines prepended and file saved successfully to '{newFile}'")
select unit;
}
public readonly struct WriteAllLines
{
public readonly string Path;
public readonly IEnumerable<string> Lines;
public WriteAllLines(string path, IEnumerable<string> lines) => (Path, Lines) = (path, lines);
}
public readonly struct ReadAllLines
{
public readonly string Path;
public ReadAllLines(string path) => Path = path;
}
public readonly struct Log
{
public readonly string Message;
public Log(string message) => Message = message;
}
public static class NumberLinesOperations
{
public static IO<Unit> WriteAllLines(string path, IEnumerable<string> lines) =>
new WriteAllLines(path, lines).ToIO();
public static IO<IEnumerable<string>> ReadAllLines(string path) =>
new ReadAllLines(path).ToIO<ReadAllLines, IEnumerable<string>>();
public static IO<Unit> Log(string message) =>
new Log(message).ToIO();
}
public static class LiveRunner
{
public static A Run<A>(IO<A> program)
{
switch (program)
{
case IO<ReadAllLines, IEnumerable<string>, A> p:
return Run(p.Next(File.ReadAllLines(p.Input.Path)));
case IO<WriteAllLines, Unit, A> p:
return Run(p.Do(x => File.WriteAllLines(x.Path, x.Lines)));
case IO<Log, Unit, A> p:
return Run(p.Do(x => Console.WriteLine(x.Message)));
default:
return program.Exit();
}
}
}
public static class AsyncLiveRunner
{
public static async Task<A> RunAsync<A>(IO<A> program)
{
switch (program)
{
case IO<ReadAllLines, IEnumerable<string>, A> p:
return await RunAsync(p.Next(await File.ReadAllLinesAsync(p.Input.Path)));
case IO<WriteAllLines, Unit, A> p:
return await RunAsync(p.Do(async x => await File.WriteAllLinesAsync(x.Path, x.Lines)));
case IO<Log, Unit, A> p:
return await RunAsync(p.Do(i => Console.WriteLine(i.Message)));
default:
return program.Exit();
}
}
}
public static class TestRunner
{
// Example of non-recursive (stack-safe) interpreter
public static A Run<A>(IO<A> program, bool skipLogging = false)
{
IEnumerable<string> ReadAllLines(string path) => new[] { "Hello", "World", path };
while (true)
switch (program)
{
case IO<ReadAllLines, IEnumerable<string>, A> p:
program = p.Next(ReadAllLines(p.Input.Path));
break;
case IO<WriteAllLines, Unit, A> p:
program = p.Skip();
break;
case IO<Log, Unit, A> p:
program = skipLogging ? p.Skip() : p.Do(x => Console.WriteLine(x.Message));
break;
default:
return program.Exit();
}
}
}
// Monadic IO implementation, can be reused, published to NuGet, etc.
//-------------------------------------------------------------------
public interface IO<out A>
{
IO<B> Bind<B>(Func<A, IO<B>> f);
}
public struct Return<A> : IO<A>
{
public readonly A Result;
public Return(A a) => Result = a;
public IO<B> Bind<B>(Func<A, IO<B>> f) => f(Result);
}
public class IO<I, O, A> : IO<A>
{
public readonly I Input;
public readonly Func<O, IO<A>> Next;
public IO(I input, Func<O, IO<A>> next) => (Input, Next) = (input, next);
public IO<B> Bind<B>(Func<A, IO<B>> f) => new IO<I, O, B>(Input, r => Next(r).Bind(f));
}
public static class IOMonad
{
public static IO<A> Lift<A>(this A a) =>
new Return<A>(a);
public static IO<B> Select<A, B>(this IO<A> m, Func<A, B> f) =>
m.Bind(a => f(a).Lift());
public static IO<C> SelectMany<A, B, C>(this IO<A> m, Func<A, IO<B>> f, Func<A, B, C> project) =>
m.Bind(a => f(a).Bind(b => project(a, b).Lift()));
public static A Exit<A>(this IO<A> m) =>
((Return<A>)m).Result;
}
public static class IOMonadSugar
{
public static IO<R> ToIO<I, R>(this I input) => new IO<I, R, R>(input, IOMonad.Lift);
public static IO<Unit> ToIO<I>(this I input) => input.ToIO<I, Unit>();
public static IO<A> Skip<I, A>(this IO<I, Unit, A> io) => io.Next(unit);
public static IO<A> Do<I, A>(this IO<I, Unit, A> io, Action<I> effect)
{
effect(io.Input);
return io.Skip();
}
}
public struct Unit
{
public static readonly Unit unit = new Unit();
}
}