<commit_before>module ParseTreeInterpreter.RuleInterpreter where

{-
This module defines an evaluator for rules
-}


import TypeSystem
import Utils.ToString
import Utils.Utils

import ParseTreeInterpreter.FunctionInterpreter

import Data.Map (Map, empty, findWithDefault)
import Data.Either
import Data.List

import Data.Bifunctor (first)

import Control.Monad

import Lens.Micro hiding ((&))


proofThat	:: TypeSystem -> Relation -> [ParseTree] -> Either String Proof
proofThat ts
	= proofThat' ts . get relSymbol

-- tries to deduce a proof for application to a given relation, might return multiple (top level) proofs
proofThats'	:: TypeSystem -> Symbol -> [ParseTree] -> Either String [Proof]
proofThats' ts symbol args
	= inMsg ("While trying to proof that ("++symbol++") is applicable to \""++ toParsable' ", " args ++"\"") $
	  do	rules	<- ts & get tsRules |> return & findWithDefault (Left $ "No rules about a relation with symbol "++show symbol) symbol
		let results	= rules |> flip (interpretRule ts) args
		let successfull	= rights results
		assert Left (not $ null successfull) $ "Not a single rule matched:\n"++unlines (lefts results)
		return successfull

-- For a given relation + input, deduces all possible proofs, and takes the proof with the smallest size (if all proofs give the same conclusion)
proofThat'	:: TypeSystem -> Symbol -> [ParseTree] -> Either String Proof
proofThat' ts symbol args
	= do	successfull	<- proofThats' ts symbol args
		let conclusions	= successfull & filter isProof |> _proofConcl & nub
		assert Left (1 == length conclusions) $ "Multiple rules provided a proof with different conclusions:\n"++ 
				(successfull |> toParsable |> lines ||>> ("#  "++) |> unlines >>= (++"\n"))
		let successfull'	= successfull & sortOn weight 
		return $ head successfull'

<commit_msg>Proofthats on relation: input variables check<commit_after>module ParseTreeInterpreter.RuleInterpreter where

{-
This module defines an evaluator for rules
-}


import TypeSystem
import Utils.ToString
import Utils.Utils

import ParseTreeInterpreter.FunctionInterpreter

import Data.Map (Map, empty, findWithDefault)
import Data.Either
import Data.List

import Data.Bifunctor (first)

import Control.Monad
import Control.Arrow ((&&&))

import Lens.Micro hiding ((&))


proofThat	:: TypeSystem -> Relation -> [ParseTree] -> Either String Proof
proofThat ts
	= proofThat' ts . get relSymbol

-- tries to deduce a proof for application to a given relation, might return multiple (top level) proofs
proofThats'	:: TypeSystem -> Symbol -> [ParseTree] -> Either String [Proof]
proofThats' ts symbol args
	= inMsg ("While trying to proof that ("++symbol++") is applicable to \""++ toParsable' ", " args ++"\"") $
	  do	relation	<- checkRelationExists ts symbol
		let inArgs	= relation & relTypesWith In
		unless (length inArgs == length args) $ Left $ "Expected "++show (length inArgs)++" arguments to relation "++symbol++", but only got "++show (length args)
		let typesFaulty	= zip (args |> typeOf)  inArgs
					|> (id &&& uncurry (alwaysIsA (get tsSyntax ts)))
					& mapi
					& filter (not . snd . snd) ||>> fst	:: [(Int, (TypeName, TypeName))]
		let typesFaultyMsg
				= typesFaulty |> (\(i, (tpExp, tpAct)) -> inMsg ("In argument "++show i) $
					 Left $ "Expected argument of type "++show tpExp++", but got a "++show tpAct++" instead")
		typesFaultyMsg & allRight'
		
		rules	<- checkRelationRulesExists ts symbol
		let results	= rules |> flip (interpretRule ts) args
		let successfull	= rights results
		assert Left (not $ null successfull) $ "Not a single rule matched:\n"++unlines (lefts results)
		return successfull

-- For a given relation + input, deduces all possible proofs, and takes the proof with the smallest size (if all proofs give the same conclusion)
proofThat'	:: TypeSystem -> Symbol -> [ParseTree] -> Either String Proof
proofThat' ts symbol args
	= do	successfull	<- proofThats' ts symbol args
		let conclusions	= successfull & filter isProof |> _proofConcl & nub
		assert Left (1 == length conclusions) $ "Multiple rules provided a proof with different conclusions:\n"++ 
				(successfull |> toParsable |> lines ||>> ("#  "++) |> unlines >>= (++"\n"))
		let successfull'	= successfull & sortOn weight 
		return $ head successfull'


{'fraction_non_alphanumeric': 0.11431248535956899, 'fraction_numerical': 0.00046849379245724994, 'mean_word_length': 4.304347826086956, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5890010', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1243, 'n_words': 480}