1- POC: TypeScript code generation from a haskell-like syntax for ADT (algebraic data types)
1+ TypeScript code generation from a haskell-like syntax for ADT (algebraic data types)
22
33# Installation
44
@@ -75,9 +75,11 @@ Syntax: `{ name :: type }`
7575
7676Example
7777
78- ` ` ` ts
79- // record ---v
80- console.log(run('data User = User { name :: string, surname :: string }'))
78+ Source
79+
80+ ` ` ` haskell
81+ -- record ---v
82+ data User = User { name :: string, surname :: string }
8183` ` `
8284
8385Output
@@ -100,9 +102,11 @@ Syntax: `(type1, type2, ...types)`
100102
101103Example
102104
103- ` ` ` ts
104- // tuple ---v
105- console.log(run('data Tuple2 A B = Tuple2 (A, B)'))
105+ Source
106+
107+ ` ` ` haskell
108+ -- tuple ---v
109+ data Tuple2 A B = Tuple2 (A, B)
106110` ` `
107111
108112Output
@@ -124,9 +128,83 @@ Syntax: `(<name> :: <constraint>)`
124128
125129Example
126130
131+ Source
132+
133+ ` ` ` haskell
134+ -- constraint ---v
135+ data Constrained (A :: string) = Fetching | GotData A
136+ ` ` `
137+
138+ Output
139+
140+ ` ` ` ts
141+ export type Constrained<A extends string> =
142+ | {
143+ readonly type: 'Fetching'
144+ }
145+ | {
146+ readonly type: 'GotData'
147+ readonly value0: A
148+ }
149+ ` ` `
150+
151+ # ` fp-ts ` encoding
152+
153+ Example
154+
155+ Source
156+
157+ ` ` ` haskell
158+ data Option A = None | Some A
159+ ` ` `
160+
161+ Output
162+
127163` ` ` ts
128- // constraint ---v
129- console.log(run('data Constrained (A :: string) = Fetching | GotData A'))
164+ declare module 'fp-ts/lib/HKT' {
165+ interface URI2HKT<A> {
166+ Option: Option<A>
167+ }
168+ }
169+
170+ export const URI = 'Option'
171+
172+ export type URI = typeof URI
173+
174+ export type Option<A> = None<A> | Some<A>
175+
176+ export class None<A> {
177+ static value: Option<never> = new None()
178+ readonly _tag: 'None' = 'None'
179+ readonly _A!: A
180+ readonly _URI!: URI
181+ private constructor() {}
182+ fold<R>(onNone: R, _onSome: (value0: A) => R): R {
183+ return onNone
184+ }
185+ foldL<R>(onNone: () => R, _onSome: (value0: A) => R): R {
186+ return onNone()
187+ }
188+ }
189+
190+ export class Some<A> {
191+ readonly _tag: 'Some' = 'Some'
192+ readonly _A!: A
193+ readonly _URI!: URI
194+ constructor(readonly value0: A) {}
195+ fold<R>(_onNone: R, onSome: (value0: A) => R): R {
196+ return onSome(this.value0)
197+ }
198+ foldL<R>(_onNone: () => R, onSome: (value0: A) => R): R {
199+ return onSome(this.value0)
200+ }
201+ }
202+
203+ export const none: Option<never> = None.value
204+
205+ export function some<A>(value0: A): Option<A> {
206+ return new Some(value0)
207+ }
130208` ` `
131209
132210# Options
@@ -146,13 +224,15 @@ export interface Options {
146224 * or a single object literal ` tag - > handler `
147225 */
148226 handlersStyle: { type: 'positional' } | { type: 'record'; handlersName: string }
227+ encoding: 'literal' | 'fp-ts'
149228}
150229
151230export const defaultOptions: Options = {
152231 tagName: 'type',
153232 foldName: 'fold',
154233 matcheeName: 'fa',
155- handlersStyle: { type: 'positional' }
234+ handlersStyle: { type: 'positional' },
235+ encoding: 'literal'
156236}
157237` ` `
158238
@@ -179,7 +259,7 @@ lenses.tagName.set('tag')(defaultOptions)
179259- ` ast ` module : internal model - > TypeScript AST
180260- ` model ` module : internal model
181261- ` printer ` module : internal model - > TypeScript code
182- - ` parser ` module : haskell - like syntax - > internal model
262+ - ` haskell ` module : haskell - like syntax - > internal model
183263- ` index ` module : haskell - like syntax - > TypeScript code
184264
185265# Roadmap
0 commit comments