<commit_before># Extract a domain from an expression
export Extract := module ()
  uses Domain_Type;
  export ModuleApply := proc(e, kb, $) :: [ HDomain, anything ];
    local b, eb, s, es;
    b, eb := op(Bound(e));
    s, es := op(Shape(eb, kb ));
    [ DOMAIN(b, s), es ];
  end proc;

  # Extract a domain bound from an expression
  # This pops off the integration constructors recursively, keeping
  # track of the bounds in a KB (which literally become the DBound).
  export Bound := module ()
    export ModuleApply := proc(e, $) :: [ DomBound, anything ];
      local arg, vars, kb;
      arg, vars := do_extract(e)[];
      vars := Domain:-Bound:-withVarsIxs(DBound(vars));
      [ vars , arg ];
    end proc;

    local do_extract_arg := proc(kind, arg_)
      local x0, x, vars, arg := arg_, rng;
      x0  := ExtBound[kind]:-ExtractVar(_rest);   # variable name
      rng := ExtBound[kind]:-ExtractRange(_rest); # variable range
      x   := DInto(x0, rng, kind);                # the variable spec
      arg, vars := do_extract(arg)[];
      [ arg, [ op(vars), x ] ];
    end proc;

    local do_extract := proc(arg, $)
      local sub, prod, svars;
      if arg :: `*` then
        sub := map(do_extract, [op(arg)]);
        prod, svars := selectremove(x->op(2,x)=[],sub);
        if nops(svars) = 1 then
          [ `*`(op([1,1],svars),op(map2(op,1,prod)))
            , op([1,2], svars) ];
        else
          [ arg, [] ];
        end if;
      elif Domain:-Has:-Bound(arg) then
        do_extract_arg(op(0,arg), op(arg));
      else
        [ arg, [] ]
      end if;
    end proc;
  end module;

  # Extract a domain shape from an expression<commit_msg>Extract a DomBound from a sum of DomBounds<commit_after># Extract a domain from an expression
export Extract := module ()
  uses Domain_Type;
  export ModuleApply := proc(e, kb, $) :: [ HDomain, anything ];
    local b, eb, s, es;
    b, eb := op(Bound(e));
    s, es := op(Shape(eb, kb ));
    [ DOMAIN(b, s), es ];
  end proc;

  # Extract a domain bound from an expression
  # This pops off the integration constructors recursively, keeping
  # track of the bounds in a KB (which literally become the DBound).
  export Bound := module ()
    export ModuleApply := proc(e, $) :: [ DomBound, anything ];
      local arg, vars, kb;
      arg, vars := do_extract(e)[];
      vars := Domain:-Bound:-withVarsIxs(DBound(vars));
      [ vars , arg ];
    end proc;

    local do_extract_arg := proc(kind, arg_)
      local x0, x, vars, arg := arg_, rng;
      x0  := ExtBound[kind]:-ExtractVar(_rest);   # variable name
      rng := ExtBound[kind]:-ExtractRange(_rest); # variable range
      x   := DInto(x0, rng, kind);                # the variable spec
      arg, vars := do_extract(arg)[];
      [ arg, [ op(vars), x ] ];
    end proc;

    local do_extract := proc(arg, $)
      local sub, prod, svars, vs;
      if arg :: `*` then
        sub := map(do_extract, [op(arg)]);
        prod, svars := selectremove(x->op(2,x)=[],sub);
        if nops(svars) = 1 then
          [ `*`(op([1,1],svars),op(map2(op,1,prod)))
            , op([1,2], svars) ];
        else
          [ arg, [] ];
        end if;
      elif arg :: `+` then
        sub := map(do_extract, [op(arg)]);
        svars := map2(op,2,sub);
        if the(map(nops,svars)) then
          vs[0] := map(sv->map2(op,1,sv), svars);      # a list of lists of variables
          vs[1] := map2(op,1, op(1,svars));            # a list of 'fresh' variables
          vs[2] := map(v->zip(`=`,v,vs[1]), vs[0]);    # variable renamings
          svars := zip(subs,vs[2], svars);             # dombounds renamed
          if the(svars) then
            [ `+`( op(zip(subs,vs[2], map2(op,1, sub))) ),
              op(1, svars) ];
          else
            [ arg, [] ]
          end if;
        else
          [ arg, [] ]
        end if;
      elif Domain:-Has:-Bound(arg) then
        do_extract_arg(op(0,arg), op(arg));
      else
        [ arg, [] ]
      end if;
    end proc;
  end module;

  # Extract a domain shape from an expression
{'fraction_non_alphanumeric': 0.15933120236046225, 'fraction_numerical': 0.010081140890090975, 'mean_word_length': 2.233704292527822, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '84983', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1364, 'n_words': 389}