Verified Commit f2a6ee88 authored by Camil Staps's avatar Camil Staps 🚀

Resolve #37: use type synonyms in unification

parent 5f7b54c4
Subproject commit 42cc5aa9649163d0a275e801a32b056c154f4d3b
Subproject commit c1ba15f2a4181153639831d4f1fc92ee89ecb604
......@@ -80,16 +80,20 @@ from Text.JSON import generic JSONEncode, generic JSONDecode, :: JSONNode
:: LocationResult :== (String, String, Maybe Int, Maybe Int)
:: StrUnifier :== ([(String,String)], [(String,String)])
:: StrUnifier
= { left_to_right :: [(String,String)]
, right_to_left :: [(String,String)]
, used_synonyms :: [(String,String)]
}
:: ShortClassResult = { cls_name :: String, cls_vars :: [String] }
derive JSONEncode Request, Response, Result, ShortClassResult, BasicResult,
FunctionResultExtras, TypeResultExtras, ClassResultExtras,
MacroResultExtras, ModuleResultExtras
MacroResultExtras, ModuleResultExtras, StrUnifier
derive JSONDecode Request, Response, Result, ShortClassResult, BasicResult,
FunctionResultExtras, TypeResultExtras, ClassResultExtras,
MacroResultExtras, ModuleResultExtras
MacroResultExtras, ModuleResultExtras, StrUnifier
instance zero Request
instance zero Response
......
......@@ -6,10 +6,10 @@ import Text.JSON
derive JSONEncode Request, Response, Result, ShortClassResult, BasicResult,
FunctionResultExtras, TypeResultExtras, ClassResultExtras,
MacroResultExtras, ModuleResultExtras
MacroResultExtras, ModuleResultExtras, StrUnifier
derive JSONDecode Request, Response, Result, ShortClassResult, BasicResult,
FunctionResultExtras, TypeResultExtras, ClassResultExtras,
MacroResultExtras, ModuleResultExtras
MacroResultExtras, ModuleResultExtras, StrUnifier
instance zero Request
where
......
......@@ -13,14 +13,16 @@ from StdMisc import abort
from TCPIP import :: IPAddress, :: Port, instance toString IPAddress
import Control.Applicative
import Control.Monad
import qualified Data.Foldable as Foldable
from Data.Foldable import class Foldable, instance Foldable Maybe
from Data.Func import $
import Data.Functor
import Data.List
import Data.Tuple
import Data.Maybe
import Data.Tuple
import System.CommandLine
import Data.Functor
import Control.Applicative
import Control.Monad
from Text import class Text(concat,trim,indexOf,toLowerCase,split),
instance Text String, instance + String
import Text.JSON
......@@ -186,12 +188,15 @@ where
# typeName = fromJust typeName
# types = findType typeName db
= [makeTypeResult (Just typeName) l td db \\ (l,td) <- types]
# mbType = snd <$> prepare_unification True (allTypes db_org) <$> (unify >>= parseType o fromString)
# mbPreppedType = prepare_unification True (allTypes db_org)
<$> (unify >>= parseType o fromString)
# usedSynonyms = 'Foldable'.concat (fst <$> mbPreppedType)
# mbType = snd <$> mbPreppedType
// Search normal functions
# filts = catMaybes [ (\t _ -> isUnifiable t db_org) <$> mbType
, (\n loc _ -> isNameMatch (size n*2/3) n $ getName loc) <$> name
]
# funs = map (\f -> makeFunctionResult name mbType Nothing f db_org) $ findFunction`` filts db
# funs = map (\f -> makeFunctionResult name mbType usedSynonyms Nothing f db_org) $ findFunction`` filts db
// Search macros
# macros = case (isNothing mbType,name) of
(True,Just n) = findMacro` (\loc _ -> isNameMatch (size n*2/3) n $ getName loc) db
......@@ -203,7 +208,7 @@ where
(size n*2/3) n f) <$> name
]
# members = findClassMembers`` filts db
# members = map (\(Location lib mod line iclline cls,vs,_,f,et) -> makeFunctionResult name mbType
# members = map (\(Location lib mod line iclline cls,vs,_,f,et) -> makeFunctionResult name mbType usedSynonyms
(Just {cls_name=cls,cls_vars=vs}) (Location lib mod line iclline f,et) db) members
// Search types
# lcName = if (isJust mbType && isType (fromJust mbType))
......@@ -340,10 +345,10 @@ where
}
)
makeFunctionResult :: (Maybe String) (Maybe Type) (Maybe ShortClassResult)
makeFunctionResult :: (Maybe String) (Maybe Type) [TypeDef] (Maybe ShortClassResult)
(Location, ExtendedType) TypeDB -> Result
makeFunctionResult
orgsearch orgsearchtype mbCls (fl, et=:(ET type tes)) db
orgsearch orgsearchtype usedsynonyms mbCls (fl, et=:(ET type tes)) db
= FunctionResult
( { library = lib
, filename = modToFilename mod
......@@ -355,7 +360,7 @@ where
}
, { func = fromJust (tes.te_representation <|>
(pure $ concat $ print False (fname,et)))
, unifier = toStrUnifier <$> finish_unification unisyns <$>
, unifier = toStrUnifier <$> finish_unification (unisyns ++ usedsynonyms) <$>
(orgsearchtype >>= unify [] unitype)
, cls = mbCls
, constructor_of = if tes.te_isconstructor
......@@ -383,7 +388,10 @@ where
{ StrUnifier
| left_to_right = map toStr unif.Unifier.left_to_right
, right_to_left = map toStr unif.Unifier.right_to_left
, used_synonyms = []
, used_synonyms = [
( concat $ [td.td_name," ":intersperse " " $ print False td.td_args]
, concat $ print False s)
\\ td=:{td_rhs=TDRSynonym s} <- unif.Unifier.used_synonyms]
}
where
toStr (var, type) = (var, concat $ print False type)
......
......@@ -250,8 +250,18 @@ function getResults(str, libs, include_builtins, include_core, page) {
highlightCallback, 'className') + '</code>');
if ('unifier' in extra &&
(extra['unifier'][0].length > 0 || extra['unifier'][1].length > 0))
(extra['unifier'].left_to_right.length > 0
|| extra['unifier'].right_to_left.length > 0))
meta.push('Unifier: ' + makeUnifier(extra['unifier']));
if ('unifier' in extra) {
var synonyms = extra['unifier'].used_synonyms;
for (var i in synonyms) {
console.log(synonyms[i]);
meta.push('Used the type synonym <code>' + highlightTypeDef(
':: ' + synonyms[i][0] + ' :== ' + synonyms[i][1],
highlightCallback) + '</code>.');
}
}
if ('generic_derivations' in extra) {
var derivations = makeInstanceTable(
......@@ -394,15 +404,15 @@ function getResults(str, libs, include_builtins, include_core, page) {
}
function makeUnifier(ufr) {
var from_left = ufr[0];
var from_right = ufr[1];
var from_left = ufr.left_to_right;
var from_right = ufr.right_to_left;
var s = '';
for (i in from_right) {
for (i in from_right)
s += '<tt>' + from_right[i][0] + '</tt> &rarr; <tt>' + from_right[i][1] + '</tt>; ';
}
for (i in from_left) {
for (i in from_left)
s += '<tt>' + from_left[i][1] + '</tt> &larr; <tt>' + from_left[i][0] + '</tt>; ';
}
return s.substring(0, s.length - 2);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment