-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experiment: dummy DB that maps (hashes of) contexts to presises
- Loading branch information
1 parent
312b366
commit 457dfa7
Showing
10 changed files
with
291 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"-5242397938801254731": [ | ||
"Test.PiVsFun.refl<10>" | ||
], | ||
"4884008893817283726": [ | ||
"Test.PiVsFun.refl<10>" | ||
], | ||
"7116324300784873135": [ | ||
"Test.PiVsFun.refl<10>" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["Test.PiVsFun.refl<10>"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"scope": [ | ||
{ | ||
"name": "Test.PiVsFun.refl<10>", | ||
"type": { | ||
"tag": "Pi", | ||
"name": "x", | ||
"domain": { | ||
"tag": "Sort", | ||
"sort": "Set" | ||
}, | ||
"codomain": { | ||
"tag": "Pi", | ||
"name": "y", | ||
"domain": { | ||
"tag": "Sort", | ||
"sort": "Set" | ||
}, | ||
"codomain": { | ||
"tag": "Application", | ||
"head": { | ||
"tag": "ScopeReference", | ||
"name": "Test.PiVsFun._≡_<4>" | ||
}, | ||
"arguments": [ | ||
{ | ||
"tag": "DeBruijn", | ||
"index": 1 | ||
}, | ||
{ | ||
"tag": "DeBruijn", | ||
"index": 0 | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"definition": { | ||
"tag": "Postulate" | ||
} | ||
} | ||
], | ||
"context": [ | ||
{ | ||
"tag": "Sort", | ||
"name": "x", | ||
"sort": "Set" | ||
}, | ||
{ | ||
"tag": "Sort", | ||
"name": "x", | ||
"sort": "Set" | ||
} | ||
], | ||
"goal": { | ||
"tag": "Application", | ||
"head": { | ||
"tag": "ScopeReference", | ||
"name": "Test.PiVsFun._≡_<4>" | ||
}, | ||
"arguments": [ | ||
{ | ||
"tag": "DeBruijn", | ||
"index": 1 | ||
}, | ||
{ | ||
"tag": "DeBruijn", | ||
"index": 1 | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
{-# LANGUAGE TypeApplications #-} | ||
-- | Store the JSON information in an SQL database for quick lookups. | ||
-- | ||
-- NB: "training" this model by feeding samples merely consists of adding DB rows | ||
module Main where | ||
|
||
import GHC.Generics | ||
|
||
import System.Environment ( getArgs ) | ||
import qualified Data.HashMap.Strict as M | ||
import Data.Maybe ( fromMaybe ) | ||
import Control.Monad ( forM_ ) | ||
import Control.Arrow ( first ) | ||
|
||
import Data.Aeson | ||
( ToJSON(..), genericToJSON | ||
, FromJSON(..), genericParseJSON | ||
, eitherDecodeFileStrict' ) | ||
|
||
import Data.Hashable ( Hashable, hash ) | ||
|
||
-- import Database.Persist.TH | ||
|
||
import Agda.Utils.Either ( caseEitherM ) | ||
|
||
import ToTrain ( names ) | ||
import Output hiding ( ScopeEntry, ScopeEntry' ) | ||
import qualified Output as O | ||
|
||
data ScopeEntry' = ScopeEntry | ||
{ _type :: Type | ||
, definition :: Definition | ||
} deriving (Generic, Show, Eq, Hashable) | ||
instance ToJSON ScopeEntry' where toJSON = genericToJSON jsonOpts | ||
instance FromJSON ScopeEntry' where parseJSON = genericParseJSON jsonOpts | ||
type ScopeEntry = Named ScopeEntry' | ||
data Context = Context | ||
{ scope :: [ScopeEntry] | ||
, context :: [Type] | ||
, goal :: Type | ||
} deriving (Generic, Show, Eq, Hashable) | ||
instance ToJSON Context where toJSON = genericToJSON jsonOpts | ||
instance FromJSON Context where parseJSON = genericParseJSON jsonOpts | ||
type Premises = [String] | ||
|
||
type Key' = Context | ||
type Key = Int -- hash of `Context` | ||
type Value = Premises | ||
type Database = M.HashMap Key Value | ||
|
||
instance Hashable a => Hashable (Named a) | ||
instance Hashable a => Hashable (Pretty a) | ||
instance Hashable a => Hashable (Reduced a) | ||
instance Hashable O.ScopeEntry'; instance Hashable Sample | ||
instance Hashable Definition; instance Hashable Clause; instance Hashable Term | ||
|
||
fileSamples :: FileData -> [(Key', Value)] | ||
fileSamples (Named{item = TrainData{..}}) = | ||
concatMap (map go . fromMaybe [] . holes . item) scopeLocal | ||
where | ||
usedIn :: Name -> Term -> Bool | ||
usedIn n = \case | ||
Pi _ (n' :~ ty) t -> n `usedIn` ty || ((n' /= n) && n `usedIn` t) | ||
Lam (n' :~ t) -> (n' /= n) && n `usedIn` t | ||
App hd ts -> case hd of {Ref n' -> n' == n; DB _ -> False} | ||
|| any (n `usedIn`) ts | ||
_ -> False | ||
|
||
go :: Sample -> (Key', Value) | ||
go Sample{..} = Context | ||
{ scope = fmap bareScope | ||
<$> scopeGlobal | ||
<> onlyRelevant scopeLocal | ||
<> onlyRelevant (fromMaybe [] scopePrivate) | ||
, context = thing . item <$> thing (ctx) | ||
, goal = original (thing goal) | ||
} .-> premises | ||
where (.->) = (,) | ||
onlyRelevant = filter $ (`usedIn` original (thing term)) . name | ||
bareScope :: O.ScopeEntry' -> ScopeEntry' | ||
bareScope O.ScopeEntry{..} = ScopeEntry {_type = original (thing _type), definition = thing definition} | ||
|
||
main :: IO () | ||
main = getArgs >>= \case | ||
("add" : dbJson : jsonFns) -> forM_ jsonFns $ \jsonFn -> do | ||
caseEitherM (eitherDecodeFileStrict' jsonFn) fail $ \fd -> do | ||
-- putStrLn "** File Data" >> print fd | ||
let samples = fileSamples fd | ||
putStrLn "** Samples" -- >> print samples | ||
forM_ samples $ \(k, _) -> do | ||
putStrLn "------" >> print k | ||
putStrLn "# " >> print (hash k) | ||
let db = M.fromList (first hash <$> samples) | ||
putStrLn "** Database" >> print db | ||
putStrLn "serializing database into a .json file" | ||
encodeFile dbJson db | ||
("query" : dbJson : ctxJson : []) -> | ||
caseEitherM (eitherDecodeFileStrict' @Database dbJson) fail $ \db -> do | ||
putStrLn "** Database" >> print db | ||
caseEitherM (eitherDecodeFileStrict' @Context ctxJson) fail $ \ctx -> do | ||
putStrLn "** Context" >> print ctx | ||
let hctx = hash ctx | ||
putStrLn "# " >> print hctx | ||
case M.lookup hctx db of | ||
Just premises -> putStrLn "** Premises" >> print premises | ||
Nothing -> putStrLn "NOT TRAINED ON THIS CONTEXT!" | ||
args -> fail | ||
$ "Usage: agda2train-db add <DB_JSON> <JSON>* \n" | ||
<> " or agda2train-db query <CONTEXT_JSON>\n\n" | ||
<> "User entered: agda2train-db " <> unwords args |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.