<commit_before>module Rules where

import Types
import Data.Tagged

import qualified Data.Map as M
import qualified Data.Set as S

deriveRule :: Context -> Proof -> M.Map (Key Connection) ConnLabel -> Rule
deriveRule ctxt proof labels = Rule {ports = rulePorts, localVars = [], freeVars = []}
  where
    portNames = map (Tagged . ("in"++) . show) [1::Integer ..]

    connectedPorts = S.fromList $ concat
      [ [connFrom c, connTo c] | (_, c) <- M.toList $ connections proof ]

    rules = ctxtRules ctxt

    openPorts = S.toList $ S.fromList $
      [ (blockKey, portKey)
      | (blockKey, block) <- M.toList $ blocks proof
      , let rule = rules M.! blockRule block
      , (portKey, blockPort) <- M.toList (ports rule)
      , BlockPort blockKey portKey `S.notMember` connectedPorts
      ] ++
      [ (b, p)
      | (cKey, (Connection from to)) <- M.toList $ connections proof
      , (NoPort, BlockPort b p) <- [(from, to), (to, from)] ]

    relabeledPorts = -- TODO relabel!
      [ port
      | (bKey, pKey) <- openPorts
      , let blockId = (blocks proof M.! bKey)
      , let block = rules M.! blockRule blockId
      , let port = (ports block) M.! pKey ]

    rulePorts = M.fromList $ zip portNames relabeledPorts<commit_msg>Collect labels (= port props after unification) for blocks with surface contact<commit_after>module Rules where

import Types
import Data.Tagged

import qualified Data.Map as M
import qualified Data.Set as S

deriveRule :: Context -> Proof -> M.Map (Key Connection) ConnLabel -> Rule
deriveRule ctxt proof labels = Rule {ports = rulePorts, localVars = [], freeVars = []}
  where
    portNames = map (Tagged . ("in"++) . show) [1::Integer ..]

    connectedPorts = S.fromList $ concat
      [ [connFrom c, connTo c] | (_, c) <- M.toList $ connections proof ]

    rules = ctxtRules ctxt

    openPorts = S.toList $ S.fromList $
      [ (bKey, pKey)
      | (bKey, block) <- M.toList $ blocks proof
      , let rule = rules M.! blockRule block
      , (pKey, _) <- M.toList (ports rule)
      , BlockPort bKey pKey `S.notMember` connectedPorts
      ] ++
      [ (bKey, pKey)
      | (_, (Connection from to)) <- M.toList $ connections proof
      , (NoPort, BlockPort bKey pKey) <- [(from, to), (to, from)] ]

    surfaceBlocks = S.fromList $ map fst openPorts

    blockLabels = M.fromList
      [ (bKey, (pKey, cProp))
      | (cKey, (Connection from@(BlockPort fromB _) to@(BlockPort toB _))) <- M.toList $ connections proof
      , fromB `S.member` surfaceBlocks || toB `S.member` surfaceBlocks
      , let (BlockPort bKey pKey) = if fromB `S.member` surfaceBlocks then from else to
      , let Ok cProp = labels M.! cKey
      ]

    relabeledPorts = -- TODO relabel!
      [ port
      | (bKey, pKey) <- openPorts
      , let blockId = (blocks proof M.! bKey)
      , let block = rules M.! blockRule blockId
      , let port = (ports block) M.! pKey ]

    rulePorts = M.fromList $ zip portNames relabeledPorts
{'fraction_non_alphanumeric': 0.12639405204460966, 'fraction_numerical': 0.0006759040216289286, 'mean_word_length': 2.667905824039653, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 9, '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': '2075873', 'n_tokens_mistral': 991, 'n_tokens_neox': 966, 'n_words': 306}