Skip to content

Commit b16928c

Browse files
committed
First stab at type search
1 parent d9dfcaa commit b16928c

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

server/Main.hs

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE DeriveAnyClass #-}
33
{-# LANGUAGE DeriveGeneric #-}
4+
{-# LANGUAGE LambdaCase #-}
45
{-# LANGUAGE OverloadedStrings #-}
56
{-# LANGUAGE TupleSections #-}
67

78
module Main (main) where
89

9-
import Control.Monad (unless)
10+
import Control.Monad (unless, (>=>))
11+
import Control.Monad.Error.Class (throwError)
1012
import Control.Monad.IO.Class (liftIO)
1113
import Control.Monad.Logger (runLogger')
14+
import Control.Monad.State (State)
15+
import qualified Control.Monad.State as State
1216
import Control.Monad.Trans (lift)
13-
import Control.Monad.Error.Class (throwError)
1417
import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
1518
import Control.Monad.Trans.Reader (runReaderT)
1619
import qualified Data.Aeson as A
1720
import Data.Aeson ((.=))
1821
import qualified Data.ByteString.Lazy as BL
1922
import Data.List (foldl')
23+
import qualified Data.Map as M
2024
import Data.String (fromString)
2125
import Data.Text (Text)
2226
import qualified Data.Text as T
@@ -30,12 +34,14 @@ import qualified Language.PureScript.CodeGen.JS as J
3034
import qualified Language.PureScript.CoreFn as CF
3135
import qualified Language.PureScript.Errors.JSON as P
3236
import qualified Language.PureScript.Interactive as I
37+
import qualified Language.PureScript.TypeChecker.TypeSearch as TS
3338
import System.Environment (getArgs)
3439
import System.Exit (exitFailure)
3540
import System.FilePath ((</>))
3641
import System.FilePath.Glob (glob)
3742
import qualified System.IO as IO
3843
import System.IO.UTF8 (readUTF8File)
44+
import qualified Text.Parsec.Combinator as Parsec
3945
import Web.Scotty
4046
import qualified Web.Scotty as Scotty
4147

@@ -90,6 +96,47 @@ server bundled externs initEnv port = do
9096
Scotty.json $ A.object [ "error" .= err ]
9197
Right comp ->
9298
Scotty.json $ A.object [ "js" .= comp ]
99+
get "/search" $ do
100+
query <- param "q"
101+
case tryParseType query of
102+
Nothing -> Scotty.json $ A.object [ "error" .= ("Cannot parse type" :: Text) ]
103+
Just ty -> do
104+
let ty' = replaceTypeVariablesAndDesugar ty
105+
let results = TS.typeSearch (Just []) initEnv (P.emptyCheckState initEnv) ty'
106+
Scotty.json $ A.object [ "results" .= A.object [ P.showQualified P.runIdent k .= P.prettyPrintType v
107+
| (k, v) <- take 20 (M.toList results)
108+
]
109+
]
110+
111+
-- | (Consistently) replace unqualified type constructors and type variables with unknowns.
112+
--
113+
-- Also remove the @ParensInType@ Constructor (we need to deal with type operators later at some point).
114+
replaceTypeVariablesAndDesugar :: P.Type -> P.Type
115+
replaceTypeVariablesAndDesugar ty = State.evalState (P.everywhereOnTypesM go ty) (0, M.empty) where
116+
go = \case
117+
P.ParensInType ty -> pure ty
118+
P.TypeConstructor (P.Qualified Nothing tyCon) -> do
119+
(next, m) <- State.get
120+
case M.lookup (Left tyCon) m of
121+
Nothing -> do
122+
let ty = P.TUnknown next
123+
State.put (next + 1, M.insert (Left tyCon) ty m)
124+
pure ty
125+
Just ty -> pure ty
126+
P.TypeVar s -> do
127+
(next, m) <- State.get
128+
case M.lookup (Right s) m of
129+
Nothing -> do
130+
let ty = P.TUnknown next
131+
State.put (next + 1, M.insert (Right s) ty m)
132+
pure ty
133+
Just ty -> pure ty
134+
other -> pure other
135+
136+
tryParseType :: Text -> Maybe P.Type
137+
tryParseType = hush (P.lex "") >=> hush (P.runTokenParser "" (P.parsePolyType <* Parsec.eof))
138+
where
139+
hush f = either (const Nothing) Just . f
93140

94141
bundle :: IO (Either Bundle.ErrorMessage String)
95142
bundle = runExceptT $ do

stack.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ flags: {}
33
packages:
44
- '.'
55
extra-deps:
6-
- purescript-0.10.4
7-
- bower-json-0.8.0
6+
- purescript-0.10.5
7+
- bower-json-1.0.0.1
88
- language-javascript-0.6.0.9
99
- parsec-3.1.11

trypurescript.cabal

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: trypurescript
2-
version: 0.10.4
2+
version: 0.10.5
33
cabal-version: >=1.8
44
build-type: Simple
55
license: BSD3
@@ -20,11 +20,12 @@ executable trypurescript
2020
filepath -any,
2121
Glob -any,
2222
scotty -any,
23-
purescript ==0.10.4,
23+
purescript ==0.10.5,
2424
containers -any,
2525
http-types >= 0.8.5,
2626
transformers ==0.4.*,
2727
mtl ==2.2.1,
28+
parsec,
2829
text -any,
2930
time -any
3031
hs-source-dirs: server

0 commit comments

Comments
 (0)