Skip to content

Type search #55

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

Merged
merged 2 commits into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions server/Main.hs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}

module Main (main) where

import Control.Monad (unless)
import Control.Monad (unless, (>=>))
import Control.Monad.Error.Class (throwError)
import Control.Monad.IO.Class (liftIO)
import Control.Monad.Logger (runLogger')
import Control.Monad.State (State)
import qualified Control.Monad.State as State
import Control.Monad.Trans (lift)
import Control.Monad.Error.Class (throwError)
import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
import Control.Monad.Trans.Reader (runReaderT)
import qualified Data.Aeson as A
import Data.Aeson ((.=))
import qualified Data.ByteString.Lazy as BL
import Data.List (foldl')
import Data.Function (on)
import Data.List (foldl', nubBy)
import qualified Data.Map as M
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text as T
Expand All @@ -30,12 +35,14 @@ import qualified Language.PureScript.CodeGen.JS as J
import qualified Language.PureScript.CoreFn as CF
import qualified Language.PureScript.Errors.JSON as P
import qualified Language.PureScript.Interactive as I
import qualified Language.PureScript.TypeChecker.TypeSearch as TS
import System.Environment (getArgs)
import System.Exit (exitFailure)
import System.FilePath ((</>))
import System.FilePath.Glob (glob)
import qualified System.IO as IO
import System.IO.UTF8 (readUTF8File)
import qualified Text.Parsec.Combinator as Parsec
import Web.Scotty
import qualified Web.Scotty as Scotty

Expand Down Expand Up @@ -90,6 +97,58 @@ server bundled externs initEnv port = do
Scotty.json $ A.object [ "error" .= err ]
Right comp ->
Scotty.json $ A.object [ "js" .= comp ]
get "/search" $ do
query <- param "q"
Scotty.setHeader "Access-Control-Allow-Origin" "*"
Scotty.setHeader "Content-Type" "application/json"
case tryParseType query of
Nothing -> Scotty.json $ A.object [ "error" .= ("Cannot parse type" :: Text) ]
Just ty -> do
let elabs = lookupAllConstructors initEnv ty
search = M.toList . TS.typeSearch (Just []) initEnv (P.emptyCheckState initEnv)
results = nubBy ((==) `on` fst) $ do
elab <- elabs
let strictMatches = search (replaceTypeVariablesAndDesugar (\nm s -> P.Skolem nm s (P.SkolemScope 0) Nothing) elab)
flexMatches = search (replaceTypeVariablesAndDesugar (const P.TUnknown) elab)
take 50 (strictMatches ++ flexMatches)
Scotty.json $ A.object [ "results" .= [ P.showQualified P.runIdent k
| (k, _) <- take 50 results
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are doing take 50 twice? (Line 113)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it's deliberate. The idea is to explore the search space lazily for every combination of type constructors with the specified names. I want the nested searches to be bounded, but also the top-level search itself. Really I should probably use LogicT or something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right. Of course :)

]
]

lookupAllConstructors :: P.Environment -> P.Type -> [P.Type]
lookupAllConstructors env = P.everywhereOnTypesM $ \case
P.TypeConstructor (P.Qualified Nothing tyCon) -> P.TypeConstructor <$> lookupConstructor env tyCon
other -> pure other
where
lookupConstructor :: P.Environment -> P.ProperName 'P.TypeName -> [P.Qualified (P.ProperName 'P.TypeName)]
lookupConstructor env nm =
[ q
| (q@(P.Qualified (Just mn) thisNm), _) <- M.toList (P.types env)
, thisNm == nm
]

-- | (Consistently) replace unqualified type constructors and type variables with unknowns.
--
-- Also remove the @ParensInType@ Constructor (we need to deal with type operators later at some point).
replaceTypeVariablesAndDesugar :: (Text -> Int -> P.Type) -> P.Type -> P.Type
replaceTypeVariablesAndDesugar f ty = State.evalState (P.everywhereOnTypesM go ty) (0, M.empty) where
go = \case
P.ParensInType ty -> pure ty
P.TypeVar s -> do
(next, m) <- State.get
case M.lookup s m of
Nothing -> do
let ty = f s next
State.put (next + 1, M.insert s ty m)
pure ty
Just ty -> pure ty
other -> pure other

tryParseType :: Text -> Maybe P.Type
tryParseType = hush (P.lex "") >=> hush (P.runTokenParser "" (P.parsePolyType <* Parsec.eof))
where
hush f = either (const Nothing) Just . f

bundle :: IO (Either Bundle.ErrorMessage String)
bundle = runExceptT $ do
Expand Down
4 changes: 2 additions & 2 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ flags: {}
packages:
- '.'
extra-deps:
- purescript-0.10.4
- bower-json-0.8.0
- purescript-0.10.5
- bower-json-1.0.0.1
- language-javascript-0.6.0.9
- parsec-3.1.11
5 changes: 3 additions & 2 deletions trypurescript.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: trypurescript
version: 0.10.4
version: 0.10.5
cabal-version: >=1.8
build-type: Simple
license: BSD3
Expand All @@ -20,11 +20,12 @@ executable trypurescript
filepath -any,
Glob -any,
scotty -any,
purescript ==0.10.4,
purescript ==0.10.5,
containers -any,
http-types >= 0.8.5,
transformers ==0.4.*,
mtl ==2.2.1,
parsec,
text -any,
time -any
hs-source-dirs: server
Expand Down