1 author: Julien Moutinho <julm+symantic@autogeree.net>
2 bug-reports: Julien Moutinho <julm+symantic@autogeree.net>
9 This is an experimental library for composing, parsing, typing, compiling, transforming and interpreting
10 a custom DSL (Domain-Specific Language) expressing
11 a subset of GHC's Haskell type system:
13 * /first class functions/ (aka. /lambdas/),
14 * chosen /monomorphic types/ (like 'Bool' or 'Maybe'),
15 * chosen /rank-1 polymorphic types/ (like @(@'Maybe'@ a)@),
16 * chosen /type class instances/,
17 * chosen /type family instances/,
18 * and chosen /type constraints/;
20 where "chosen X" means declared in Haskell
21 and selected when composing the DSL.
23 In particular, this library is currently not able to:
25 * do /type inferencing/ for the argument of /lambdas/
26 (they must all be explicitely annotated, aka. /Church-style/),
27 * do /pattern matching/ (aka. /case/) (but /Church-encoding/ functions are often enough),
28 * do /rank-N polymorphic types/ (aka. /non-prenex forall/, like @(forall s. ST s a) -> a@).
30 And by itself, the DSL is only able to define new terms to be interpreted,
31 no new types, or other type-level structures.
35 Please be aware that despite its using of powerful ideas from clever people,
36 this remains a FUND-LESS SINGLE-PERSON EXPERIMENTAL LIBRARY.
37 Meaning that it IS NOT heavily tested and documented,
38 DOES NOT have a strong commitment to preserving backward compatibility,
39 MAY FAIL to comply with the <http://www.haskell.org/haskellwiki/Package_versioning_policy PVP>,
40 and CAN die without notice.
45 The main goal of this library is to enable the runtime interpretation of terms,
46 type-checked according to some types defined at composing-time (ie. GHC's compile-time).
48 Using a DSL enables to limit expressiveness in order to ease analysis.
49 Here the idea is that the more complex logic shall remain written in Haskell,
50 and then this library used to project an interface into a DSL
51 (using GHC's Haskell as a FFI (Foreign Function Interface)).
52 This in order to give runtime users the flexibility
53 to write programs not requiring a full-blown Haskell compiler,
54 yet enabling enough flexibility to let them express complex needs
55 with a reasonably advanced type-safe way
56 and a controlled environment of primitives.
60 * Enabling runtime users to enter some Haskell-like expressions
61 (maybe with a more convenient syntax wrt. the domain problem)
62 without using at runtime all the heavy machinery and ecosystem of GHC
63 (eg. by using <https://hackage.haskell.org/package/hint hint>),
64 but still leaning on primitive functions coded in GHC's Haskell.
65 * Limiting those expressions to be built from well-controlled expressions only.
66 * Run some analyzes/optimizations on those well-controlled expressions.
70 Please pick in <https://hackage.haskell.org/package/symantic-lib symantic-lib>
71 a few specific @Lib/*.hs@ files near what you want to do
72 and the corresponding @Lib\/*/Test.hs@ file,
73 if any in the <git://git.autogeree.net/symantic Git repository>,
74 to learn by examples how to use this library.
76 Those @Lib\/*/Test.hs@ files use <https://hackage.haskell.org/package/megaparsec megaparsec> as parser
77 and a default grammar somehow sticking to Haskell's,
78 but staying context-free (so in particular: insensitive to the indentation),
79 and supporting prefix and postfix operators.
80 This grammar — itself written as a symantic embedded DSL
81 with <https://hackage.haskell.org/package/symantic-grammar symantic-grammar> —
82 can be reused to build other ones, is not bound to a specific parser,
83 and can produce its own EBNF rendition.
87 This library would probably be much worse than it is
88 without the following seminal works:
90 * <http://okmij.org/ftp/tagless-final/ Finally Tagless> by Jacques Carette, Oleg Kiselyov, and Chung-chieh Shan.
91 * <http://cs.brynmawr.edu/~rae/papers/2016/thesis/eisenberg-thesis.pdf Dependent Types in Haskell> by Richard A. Eisenberg.
96 Terms are encoded in the <http://okmij.org/ftp/tagless-final/ Tagless-Final> way (aka. the /symantic/ way)
97 which leverages the /type class/ system of Haskell — instead of using /data types/ — to form an embedded DSL.
98 More specifically, a /class/ encodes the /syntax/ of terms (eg. 'Sym_Bool')
99 and its /class instances/ on a dedicated type encodes their /semantics/
100 (eg. @(Sym_Bool Eval)@ interprets a term as a value of its type
101 in the host language ('Bool' in Haskell here),
102 or @(Sym_Bool View)@ interprets a term as a textual rendition, etc.).
104 DSL are then composed/extended by selecting those symantic /classes/
105 (and in an embedded DSL those could even be automatically inferred,
106 when @NoMonomorphismRestriction@ is on).
107 Otherwise, when using symantics for a non-embedded DSL
108 — the whole point of this library — the /classes/ composing the DSL
109 are selected manually at GHC's compile-time,
110 through the /type-level list/ @ss@ given to 'readTerm'.
112 Moreover, those symantic @term@s are parameterized by the type of the value they encode,
113 in order to get the same type safety as with plain Haskell values.
114 Hence the symantic /classes/ have the higher kind: @((* -> *) -> Constraint)@
115 instead of just @(* -> Constraint)@.
117 Amongst those symantics, 'Sym_Lambda' introduces /lambda abstractions/ by an higher-order approach,
118 meaning that they directly reuse GHC's internal machinery
119 to abstract or instantiate variables,
120 which I think is by far the most efficient and simplest way of doing it
121 (no (generalized or not) DeBruijn encoding
122 like in <https://hackage.haskell.org/package/bound bound>'s @Monad@s).
124 * __Singleton for any type__.
125 To typecheck terms using a @(@'Type'@ src vs t)@ which acts as a /singleton type/
126 for any Haskell /type index/ @t@ of any kind,
127 which is made possible with the dependant Haskell extensions:
128 especially @TypeFamilies@, @GADTs@ and @TypeInType@.
130 * __Type constants using 'Typeable'__.
131 /Type constant/ could be introduced by indexing them amongst a /type-level list/,
132 but since they are /monomorphic types/, using 'Typeable' simplifies
133 the machinery, and is likely more space/time efficient, including at GHC-compile-time.
135 * __Type variables using a type-level list__.
136 Handling /type variables/ is done by indexing them amongst a @vs@ /type-level list/,
137 where each /type variable/ is wrapped inside a @Proxy@ to handle different kinds.
138 Performing a substitution (in 'substVar') preserves the /type index/ @t@,
139 which is key for preserving any associated 'Term'.
140 Unifying /type variables/ is done with 'unsafeCoerce' (in 'unifyType'),
141 which I think is necessary and likely safe.
143 -- NOTE: no longer used.
144 -- * __Extensible data type__.
145 -- To inject a type into a /type-level list/
146 -- or project a /type-level list/ onto a type,
147 -- to compose an /extensible data type/
148 -- (eg. the 'Token' @GADT@ gathering the 'TokenT' /data instances/,
149 -- that a parser can build and then give to 'compile').
150 -- This type-level programming requires @UndecidableInstances@,
151 -- but not @OverlappingInstances@.
153 -- There is a similarity with
154 -- <http://dx.doi.org/10.1017/S0956796808006758 Data types à la carte>
155 -- (see for instance <https://hackage.haskell.org/package/compdata compdata>
156 -- or <https://hackage.haskell.org/package/syntactic syntactic>).
157 -- Those also enable to compose a DSL using some machinery
158 -- based upon (co)free(r) (co)monads and (cata|ana)morphisms.
159 -- Which library fits best your problem domain and brain is for you to judge :)
160 -- On that topic, see also:
161 -- <https://www.youtube.com/watch?v=qaAKRxO21fU Stop Paying for Free Monads>.
163 -- Here, I just came up using /type-level lists/ by hacking
164 -- <https://hackage.haskell.org/package/glambda glambda>'s @Elem@.
168 * @GADTs@ for knowing types by pattern-matching terms,
169 or building terms by using type classes.
170 * @Rank2Types@ or @ExistentialQuantification@ for parsing @GADT@s.
171 * @TypeInType@ (introduced in GHC 8.0.1)
172 for 'Type' to also bind a kind equality for the type @t@ it encodes.
173 Which makes the /type application/ ('TyApp')
174 give us an /arrow kind/ for the Haskell /type constructor/
175 it applies an Haskell type to, releaving me from tricky workarounds.
176 * @ConstraintKinds@ for @cs@ to contain 'Constraint's,
177 or defining /type synonym/ of /type classes/,
178 or merging /type constraints/.
179 * @DataKinds@ for type-level data structures (eg. /type-level lists/).
180 * @TypeFamilies@ for type-level programming.
181 * @UndecidableInstances@ for type-level programming
182 that may never terminate.
183 * @PolyKinds@ for avoiding a lot of uses of 'Proxy'.
184 * @TypeApplications@ for having a more concise syntax
185 to build 'Type' (eg. 'tyConst'@ @\@Bool).
186 * @DefaultSignatures@ for providing identity transformations of terms,
187 and thus avoid boilerplate code when a transformation
188 does not need to alter all semantics.
189 Almost as explained in <https://ro-che.info/articles/2016-02-03-finally-tagless-boilerplate Reducing boilerplate in finally tagless style>.
193 There are some of them hidding in there,
194 and the whole thing is far from being perfect!
195 Whatever your comments, problem reports, or questions, they are welcome!
196 You have my email address, so… just send me some emails :]
200 * Study to which point /type inferencing/ is doable,
201 now that 'Type' is powerful enough to contain 'TyVar's.
202 * Study to which point error messages can be improved,
203 now that there is a 'Source' carried through all 'Kind's or 'Type's,
204 it should enable some nice reports.
205 Still, a lot of work and testing remain to be done,
206 and likely some ideas to find too…
207 * Add more terms in <https://hackage.haskell.org/package/symantic-lib symantic-lib>.
208 * Add more transformations.
209 * Support module imports.
210 * Study where to put @INLINE@, @INLINEABLE@ or @SPECIALIZE@ pragmas.
211 * Study how to support /rank-N polymorphic types/,
212 special cases can likely use the /boxed polymorphism/ workaround,
213 but even if GHC were supporting /impredicative types/,
214 I'm currently clueless about how to do this.
219 license-file: COPYING
220 maintainer: Julien Moutinho <julm+symantic@autogeree.net>
222 stability: experimental
223 synopsis: Library for Typed Tagless-Final Higher-Order Composable DSL
224 tested-with: GHC==8.0.2
225 -- PVP: +-+------- breaking API changes
226 -- | | +----- non-breaking API additions
227 -- | | | +--- code changes with no API change
228 version: 6.0.0.20170611
230 Source-Repository head
231 location: git://git.autogeree.net/symantic
242 MultiParamTypeClasses
254 default-language: Haskell2010
257 Language.Symantic.Compiling
258 Language.Symantic.Compiling.Beta
259 Language.Symantic.Compiling.Grammar
260 Language.Symantic.Compiling.Module
261 Language.Symantic.Compiling.Read
262 Language.Symantic.Compiling.Term
263 Language.Symantic.Interpreting
264 Language.Symantic.Interpreting.Dup
265 Language.Symantic.Interpreting.Eval
266 Language.Symantic.Interpreting.View
267 Language.Symantic.Transforming
268 Language.Symantic.Transforming.Beta
269 Language.Symantic.Transforming.Trans
270 Language.Symantic.Typing
271 Language.Symantic.Typing.Grammar
272 Language.Symantic.Typing.Kind
273 Language.Symantic.Typing.List
274 Language.Symantic.Typing.Peano
275 Language.Symantic.Typing.Read
276 Language.Symantic.Typing.Show
277 Language.Symantic.Typing.Type
278 Language.Symantic.Typing.Unify
279 Language.Symantic.Typing.Variable