Theory Repair_Tests

(* Author: Basil Fürer *)

theory Repair_Tests
  imports Recover_Tests
begin

section ‹Repair sequences›

datatype p_tok = PLP | PRP | PNum | PPlus | PSemi

datatype p_exp = PN | PAdd p_exp p_exp

grammar (relation) rp =
  tokens ‹p_tok›
  productions
    Pr :: ‹p_exp list› = E PSemi Pr ⇒ ‹λe _ p. e # p› | E ⇒ ‹λe. [e]›
  and
    E :: ‹p_exp› = E PPlus T ⇒ ‹λa _ b. PAdd a b› | T
  and
    T :: ‹p_exp› = PNum ⇒ ‹λ_. PN› | PLP E PRP ⇒ ‹λ_ e _. e›

parser (no_diagnostics) rp_plain for rp

parser (diagnostics) rp_parse for rp repair


ML ‹same_code context "rp_plain" "rp_parse" ["_coverage", "_diag", "_info", "_probe", "_recover", "_render", "_repair", "_terminals"]›

definition rrep :: ‹('n, 't, unit, unit) perr list × 'x ⇒ (nat × 't rop list) list × 'x› where
  ‹rrep r = (map (λe. (pe_index e, pe_repair e)) (fst r), snd r)›

subsection ‹The simple driver›

text ‹A good word, a missing operand, a missing separator, an unclosed bracket, the
  end of the input, two errors apart, and the empty word, which has no neighbour.›

parser_check ‹rrep (rp_parse_recover (toks 0 [PNum, PPlus, PNum])) =
    ([], Some [PAdd PN PN])›

parser_check ‹rrep (rp_parse_recover (toks 0 [PNum, PPlus, PPlus, PNum])) =
    ([(2, [RIns PNum 4])], Some [PAdd (PAdd PN PN) PN])›

parser_check ‹rrep (rp_parse_recover (toks 0 [PNum, PNum, PPlus, PNum])) =
    ([(1, [RIns PSemi 2])], Some [PN, PAdd PN PN])›

parser_check ‹rrep (rp_parse_recover (toks 0 [PLP, PNum, PSemi, PNum, PSemi, PNum, PPlus, PNum])) =
    ([(2, [RIns PRP 4])], Some [PN, PN, PAdd PN PN])›

parser_check ‹rrep (rp_parse_recover (toks 0 [PNum, PPlus])) =
    ([(2, [RIns PNum 3])], Some [PAdd PN PN])›

parser_check ‹rrep (rp_parse_recover (toks 0 [PNum, PPlus, PPlus, PNum, PSemi, PNum, PSemi, PNum, PNum, PPlus, PNum, PSemi, PNum])) =
    ([(2, [RIns PNum 4]), (8, [RIns PSemi 16])], Some [PAdd (PAdd PN PN) PN, PN, PN, PAdd PN PN, PN])›

parser_check ‹rrep (rp_parse_recover ([] :: (p_tok, unit) token list)) =
    ([(0, [])], None)›


subsection ‹The other drivers, and error productions first›

text ‹The vector driver with its sparse fallback, the dense one with a history, and
  layout: ‹LId› takes the column of the next token. ‹rc_both› repairs first, and
  takes the error token only when the search finds nothing.›

parser (diagnostics) cf_fix for cf repair
parser (diagnostics) dc_fix for dc repair
parser (diagnostics) ly_fix for ly repair avoid LErr
parser (diagnostics) rc_both for rc recover RErr repair

parser_check ‹rrep (cf_fix_recover (toks 0 [CA])) =
    ([(1, [RIns CB 1])], Some 0)›

parser_check ‹rrep (cf_fix_recover (toks 0 [CB, CB])) =
    ([(0, [RIns CA 0, RDel CB])], Some 0)›

parser_check ‹rrep (dc_fix_recover (toks 0 [DA])) =
    ([(1, [RIns DB 1])], Some 0)›

parser_check ‹rrep (dc_fix_recover (toks 0 [DB, DB])) =
    ([(0, [RIns DC 0, RDel DB])], Some 2)›

parser_check ‹rrep (ly_fix_recover (lw [(0, 1, LWhile), (0, 7, LId), (0, 8, LColon)])) =
    ([(3, [RIns LId 9])], Some [LS])›

parser_check ‹rrep (ly_fix_recover (lw [(0, 1, LWhile), (0, 8, LColon), (1, 3, LId)])) =
    ([(1, [RIns LId 8])], Some [LS])›

parser_check ‹rrep (ly_fix_recover (lw [(0, 1, LWhile), (0, 7, LId), (0, 8, LColon), (1, 3, LId), (2, 4, LId), (3, 3, LId)])) =
    ([(4, [RDel LId])], Some [LS, LS])›

text ‹The report of ‹on_error_reduce› keeps the repair.›

parser (diagnostics) ly_oer for ly repair avoid LErr on_error_reduce St

parser_check ‹map snd (fst (rrep (ly_oer_recover (lw [(0, 1, LWhile), (0, 8, LColon), (1, 3, LId)])))) =
    [[RIns LId 8]]›

parser_check ‹rtoks (ly_fix_repair (lw [(0, 1, LWhile), (0, 8, LColon), (1, 3, LId)])) =
    ([1], [(LWhile, 1, 1), (LId, 8, 0), (LColon, 8, 1), (LId, 3, 1)])›

parser_check ‹rrep (rc_both_recover (toks 0 [RX, REq, RRP, RSemi, RX, REq, RNum])) =
    ([(2, [RIns RNum 4, RDel RRP])], Some [SLet ENum, SLet ENum])›

parser_check ‹rrep (rc_both_recover (toks 0 [RSemi])) =
    ([(0, [RIns RX 0, RIns REq 0, RIns RNum 0, RDel RSemi])], Some [SLet ENum])›

text ‹Past the budget the error token still gives a tree.›

parser_check ‹(case rrep (rc_both_recover (toks 0 ([RX, REq] @ replicate 12 RLP @
      [RNum, RSemi, RX, REq, RNum, RSemi, RX, REq, RNum]))) of
    (es, t) ⇒ es ≠ [] ∧ (∀e ∈ set es. snd e = []) ∧ t ≠ None)›


subsection ‹A token function, and terminals to avoid›

definition mk_tok :: ‹p_tok ⇒ unit span ⇒ (p_tok, unit) token› where
  ‹mk_tok t s = ⦇tok = t, span = s⦇s_txt := [()]⦈⦈›

parser (diagnostics) rp_tok for rp repair token mk_tok
parser (diagnostics) rp_av for rp repair avoid PNum

parser_check ‹map (λx. (tok x, length (s_txt (span x)))) (snd (rp_tok_repair (toks 0 [PNum, PPlus]))) =
    [(PNum, 0), (PPlus, 0), (PNum, 1)]›

parser_check ‹rrep (rp_av_recover (toks 0 [PNum, PPlus, PPlus, PNum])) =
    ([(2, [RDel PPlus])], Some [PAdd PN PN])›


subsection ‹The message›

parser_check ‹map rp_parse_render (fst (rp_parse_recover (toks 0 [PNum, PPlus, PPlus, PNum]))) =
    [''0:4: syntax error at '' @ squote ''PPlus'' @ [CHR 0x0A] @
     ''  expected: '' @ squote ''PNum'' @ '' or '' @ squote ''PLP'' @ [CHR 0x0A] @
     ''  repaired by: insert '' @ squote ''PNum'']›

subsection ‹Unmatched brackets›

text ‹The nodes the search takes on a word with ‹n› unmatched brackets, in a budget
  ‹b›, and how many repairs it finds: about 1.7 times as many nodes per bracket with
  merging, 2.4 without it, the exponential of cite‹‹Sect.~5.4› in "diekmann2020-dont-panic"›. const‹rnodes› stops it at
  6 brackets, with no repair and the report standing.›

definition brackets :: ‹nat ⇒ p_tok list› where
  ‹brackets n = replicate n PLP @ [PNum, PSemi, PNum, PSemi, PNum]›

definition bnodes :: ‹nat ⇒ nat ⇒ (nat × nat) option› where
  ‹bnodes b n = (let w = toks 0 (brackets n); P = rp_parse_ptabs; f = rp_parse_tnum in
     case pdiag_run P rp_parse_i0 f w of
       Some (DStuck NoAction _ sh) ⇒
         let i = length w - length (snd (fst sh));
             r = rcost (Suc b) (pops P f) (map fst rp_parse_terminals) [] 0 b
                   [(fst (fst sh), tcols (drop i w), 0, [])] []
         in Some (b - fst r, length (snd r))
     | _ ⇒ None)›

parser_check ‹map (bnodes 30000) [1, 2, 3, 4, 5, 6, 7, 8] =
    map Some [(7, 1), (15, 1), (33, 1), (69, 1), (135, 1), (247, 1), (427, 1), (709, 1)]›

parser_check ‹map (bnodes rnodes) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] =
    map Some ([(7, 1), (15, 1), (33, 1), (69, 1), (135, 1)] @ replicate 7 (240, 0))›

parser_check ‹map (λn. rrep (rp_parse_recover (toks 0 (brackets n)))) [4, 5, 6, 12] =
    [([(5, replicate 4 (RIns PRP 10))], Some [PN, PN, PN]), ([(6, replicate 5 (RIns PRP 12))], Some [PN, PN, PN]),
     ([(7, [])], None), ([(13, [])], None)]›

subsection ‹The budget of a word›

text ‹The stops of a word share one budget.›

definition rword :: ‹nat ⇒ (p_tok, unit) token list ⇒ _› where
  ‹rword b w = rloop (prstop rp_parse_ptabs rp_parse_i0 rp_parse_tnum (map fst rp_parse_terminals)
     ⦇rm_err = None, rm_repair = Some ([], rtoken)⦈)
     (Suc (length w)) (Suc (length w)) 0 (length w) b w []›

definition two_errs :: ‹(p_tok, unit) token list› where
  ‹two_errs = toks 0 [PNum, PPlus, PPlus, PNum, PSemi, PNum, PSemi, PNum, PNum, PPlus, PNum, PSemi, PNum]›

text ‹Too little for the first stop, enough for the first but not what is left for the
  second, and enough for both.›

parser_check ‹map (λb. map (length ∘ pe_repair) (fst (rword b two_errs))) [4, 14, 16] = [[0], [1, 0], [1, 1]]›


subsection ‹Target languages›

export_code rp_parse_recover rc_both_recover ly_fix_recover cf_fix_recover dc_fix_recover
  rp_tok_recover
  checking SML

export_code rp_parse_recover ly_fix_recover in Haskell

end