faylang / fay

A proper subset of Haskell that compiles to JavaScript
https://github.com/faylang/fay/wiki
BSD 3-Clause "New" or "Revised" License
1.29k stars 86 forks source link

ord instance for Text #411

Closed henrylaxen closed 9 years ago

henrylaxen commented 10 years ago

Dear Adam & co.

I pulled the most recent version of fay today (Sep 2, 2014) and it looked like you had made Text and instance of ord. I really wanted this, because I would like to share my data types between the Fay client and the Haskell server. In particular, I would like to use a Map on the server, and the key must be an instance of ord.

I have (as shared):

newtype Player = Player Text deriving (Eq, Ord, Typeable, Data, Read, Show)

this doesn't compile:

fay: ghc: ../../Games/GuessPhrase/SharedTypes.hs:12:17: No instance for (Ord Text) arising from the 'deriving' clause of a data type declaration Possible fix: use a standalone 'deriving instance' declaration, so you can specify the instance context yourself When deriving the instance for (Ord Player)

of course I know I can't do that.

On the other hand, looking at:

fay-text/Test.hs lines 52 and 53: eq "maximum" "x" (d $ T.maximum "axb") eq "minimum" "a" (d $ T.minimum "xay") work great, so the concept of ord must exist. Also looking at:

fay-text/src/Fay/Text/Type.hs lines 25 and 26 we see:

data Text deriving (Data, Eq, Ord, Read, Show, Typeable)

which looks promising, but when I add this test to fay-text/Test.hs line 54:

eq "ord" "true" (d $ "a" < "b")

I get:

fay: ghc: Test.hs:54:28: No instance for (Ord Text) arising from a use of ‘<’ In the second argument of ‘($)’, namely ‘"a" < "b"’ In the third argument of ‘eq’, namely ‘(d $ "a" < "b")’ In a stmt of a 'do' block: eq "ord" "true" (d $ "a" < "b")

So the bottom line is, is there any way to have a newtype type for Text in fay which I can use as a key in a Map, ie has an ord instance?

Best wishes, Henry Laxen nadine.and.henry@pobox.com

henrylaxen commented 10 years ago

Dear Adam,

This is not urgent or a show stopper. I can get around it with:

ifdef FAY

else

instance Ord Player where compare (Player p1) (Player p2) = compare p1 p2

endif

It just looked to me from the code and the changelog that Text should be an instance of ord. Best wishes, Henry Laxen

bergmark commented 10 years ago

Hi @henrylaxen,

The underlying issue is that when cabal install fay-text runs, FAY is undefined and thus the Fay part of the code is not type checked. When you then do fay --package fay-text Fay just passes -package fay-text to ghc and this just uses the interfaces from installation so the code path isn't type checked here either!

I ran across the no-typechecking issue a while ago but didn't consider that it was affecting programs that should be valid! The implementation is actually invalid, see this open GHC feature request: https://ghc.haskell.org/trac/ghc/ticket/7401

The reason Eq just works and Ord doesn't is that we reuse the Eq typeclass from GHC base but define our own Ord class since we want to prevent some usages that are invalid in Fay.

Good to see you have a workaround, I haven't thought of a good way to make this work inside fay-text itself. We could create a separate package for this instance that doesn't use CPP or change fay to always typecheck using source files instead of packages. haskell-packages may also provide a solution for this.

bergmark commented 9 years ago

I forgot to close this. The new fay-base provides Data.Text and has an Ord instance. fay-text just re-exports Data.Text so whichever module you import it should work.

henrylaxen commented 9 years ago

Great work Adam. Using fay make javascript almost "pleasurable." Best wishes, Henry Laxen

Adam Bergmark writes:

I forgot to close this. The new fay-base provides Data.Text and has an Ord instance. fay-text just re-exports Data.Text so whichever module you import it should work.


Reply to this email directly or view it on GitHub: https://github.com/faylang/fay/issues/411#issuecomment-63018222

Nadine and Henry Laxen The rest is silence Villa Alta #6
Calle Gaviota #10 Never try to teach a pig to sing Chapala It wastes your time
(376) 765-3181 And it annoys the pig

                         ;\ 
                        |' \ 
     _                  ; : ; 
    / `-.              /: : | 
   |  ,-.`-.          ,': : | 
   \  :  `. `.       ,'-. : | 
    \ ;    ;  `-.__,'    `-.| 
     \ ;   ;  :::  ,::'`:.  `. 
      \ `-. :  `    :.    `.  \ 
       \   \    ,   ;   ,:    (\ 
        \   :., :.    ,'o)): ` `-. 
       ,/,' ;' ,::"'`.`---'   `.  `-._ 
     ,/  :  ; '"      `;'          ,--`. 
    ;/   :; ;             ,:'     (   ,:) 
      ,.,:.    ; ,:.,  ,-._ `.     \""'/ 
      '::'     `:'`  ,'(  \`._____.-'"' 
         ;,   ;  `.  `. `._`-.  \\ 
         ;:.  ;:       `-._`-.\  \`. 
          '`:. :        |' `. `\  ) \ 
             ` ;:       |    `--\__,' 
               '`      ,' 
                    ,-'