-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a way to define sum type? #1
Comments
It's still very early stage, please come back later :)) |
@srghma Yeah. We should definitely handle Sum types. Finally we want to cover the whole PS :-) |
I want to also point out that I've made a possible mistake and initially was looking into purescript cst as a main source of inspiration. AST from cst is designed for a parser and we should probably look more into AST inside the compiler too as you suggested above @srghma. This internal AST is already used for printing: https://github.com/purescript/purescript/tree/master/src/Language/PureScript/Pretty/ |
So I copied code mainly from here (this is representation used by parser - close to the "source code"): and not from here (this is representation used to represent language constructs internally - it has a printer): The question is: which is a better inspiration for the simplest possible codegen? What do you think? Maybe we should ask PS core team :-) |
It is also worth to add that purty uses cst representation for its codegen. I think that the reason is that it needs to be able to work closely with and transform existing source code. P.S. |
Todo: also add pretty printing lib that supports columns Useful for printing
https://hackage.haskell.org/package/boxes-0.1.5/docs/src/Text-PrettyPrint-Boxes.html#Box https://pursuit.purescript.org/packages/purescript-boxes/2.0.1/docs/Text.PrettyPrint.Boxes#v:columns |
@srghma @flip111 @jvliwanag When we commit to do only |
researchfrom cst data Type a
= TypeVar a (Name Ident) -- e.g. `a` in `Foo a`
| TypeConstructor a (QualifiedName (N.ProperName 'N.TypeName)) -- e.g. `Text` in `Foo Text`
| TypeWildcard a SourceToken -- e.g. this is `_` in `foo :: _`
| TypeString a SourceToken PSString -- e.g. `"foo"` in `foo = SProxy :: SProxy "foo"`
| TypeHole a (Name Ident) -- not in ast, e.g. `?hole` in `foo :: ?hole`
| TypeOp a (Type a) (QualifiedName (N.OpName 'N.TypeOpName)) (Type a) -- e.g. it's `$` or `<<<` in `data Foo = Array <<< Maybe $ Int`
| TypeOpName a (QualifiedName (N.OpName 'N.TypeOpName)) -- not in ast, this is `(..)` in `foo :: Foo ((..) :: Type)`
| TypeApp a (Type a) (Type a) -- e.g. `Eff` is `TypeApp (TypeApp "Eff" (ROW)) Unit)` in `forall e. Eff ( console :: CONSOLE, foo :: FOO | e ) Unit`
| TypeForall a SourceToken (NonEmpty (TypeVarBinding a)) SourceToken (Type a)
| TypeConstrained a (Constraint a) SourceToken (Type a) -- e.g. in `foo :: Applicative f => Traversal f => f a` it is `TypeConstrained (Constraint ....) (TypeConstrained (Constraint ....) (f a))`
| TypeRow a (Wrapped (Row a)) -- e.g. `(...)`
| TypeRecord a (Wrapped (Row a)) -- e.g. `{ ... }`
| TypeArr a (Type a) SourceToken (Type a) -- not in ast, e.g. `foo :: Foo -> Bar` is eq to `TypeArr ... (TypeConstructor Foo) ... (TypeConstructor Bar)`
| TypeArrName a SourceToken -- not in ast, this is `(->)` in `foo :: Foo (->)`
| TypeKinded a (Type a) SourceToken (Kind a) -- e.g. this is `(a :: Type)` in `data Foo (a :: Type)`
| TypeParens a (Wrapped (Type a)) -- e.g. this is `(Bar a)` in `data Foo = Foo (Bar a)` from ast data Type a
= TUnknown a Int -- no in cst
| TypeVar a Text
| TypeConstructor a (Qualified (ProperName 'TypeName))
| TypeWildcard a (Maybe Text)
| TypeLevelString a PSString
| TypeOp a (Qualified (OpName 'TypeOpName))
| TypeApp a (Type a) (Type a)
| ForAll a Text (Maybe (Kind a)) (Type a) (Maybe SkolemScope)
| ConstrainedType a (Constraint a) (Type a)
| Skolem a Text Int SkolemScope -- not in cst
| REmpty a
| RCons a Label (Type a) (Type a)
| KindedType a (Type a) (Kind a)
| ParensInType a (Type a)
| BinaryNoParensType a (Type a) (Type a) (Type a) -- not in ast
is parsed to
https://pastebin.com/GRd6J7je (not all is pretty-printed)
I think the purescript-cst is better than ast for us
I think yes, we should rename
I'll try to add sum types support. As of |
Just to give a bit of context, the original goal of this project has been to assist in writing ffi bindings to JS libraries. In fact @paluh already has working bindings for react-mui from which he has derived this project from. The approach for writing ffi bindings is interesting. A program interacts with the typescript compiler which then reads through the typescript source code. This program utilizes the codegen library to emit purescript code. As such, one can easily update the purescript ffi just by rerunning the codegen. Now, the codegen however is purpose built for the react-mui library. Doing it for other js/ts project will involve a bit of copying code. In order to help with this, we've decided to fork off the codegen portion of the purescript-react-mui into this project and remove the mui specific stuff. We've also expanded the scope of this to (ideally) emit any purescript code. From which we can build code patterns that will later on be the backbone of doing automated ffi. Expanding the scope however, we then end up with a number of options:
The advantage of (2) as a user is that it's easy to tie in other js based tooling. Ex - typescript compiler. As a user of purescript, it is also easier to use purescript libraries. The disadvantage is that it will be hard to keep the library up to date. Also, doing this is not a simple task. The advantage of (3) is we get to work directly with the purescript compiler codebase. Problem is it's in haskell. An option that lies between (2) and (3) is keep the code in haskell. Then instead of writing a port as described in (2), we instead write purescript bindings to the haskell library (though I'm not exactly sure how to do this.) To be honest, I've been on and off which approach to take. An early attempt at (3) by transforming purty to a library is -- https://gitlab.com/jvliwanag/purty/-/tree/6.1.0-lib . But I haven't visited it lately to make my suggestion on this. For me though, it's a choice between (1) and (3). |
the 2 is ideal, but maybe hard |
@srghma Thanks for your interest and your research.
@jvliwanag . To be honest I'm only considering these two options because my main focus is to work on TS/PS tooling and interaction... (Or I'm just cached by a "Commitment/Consistency" trap :-)). |
Wow! @srghma PR looks incredible! |
How to define
data Foo = Bar | Baz
?I don't see anything similar
The text was updated successfully, but these errors were encountered: