Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1114699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:02:41+00:00 2026-05-17T03:02:41+00:00

In kiama a generic dup method is defined which copies Product objects and applies

  • 0

In kiama a generic “dup” method is defined which copies Product objects and applies a given function to each of the elements of the Product in order to support rewriting of Terms:

/**
 * General product duplication function.  Returns a product that applies
 * the same constructor as the product t, but with the given children            
 * instead of t's children.  Fails if a constructor cannot be found or
 * if one of the children is not of the appropriate type.
 */
private def dup (t : Product, children : Array[AnyRef]) : Product = {

    val ctor = (t.getClass.getConstructors())(0)
    try {
        val ret = ctor.newInstance (children : _*).asInstanceOf[Product]
        ret
                                                                                    } catch {
        case e : java.lang.ClassCastException =>
            error ("dup cast failed: " + t)
        case e : IllegalArgumentException =>
            error ("dup illegal arguments: " + ctor + " (" +
                   children.deep.mkString (",") + "), expects " +
                   ctor.getParameterTypes.length)
    }            
}

Used like this:

private def childProduct (p : Product, i : Int, s : => Strategy) : Option[Term] = {
    val numchildren = p.productArity
    val ct = p.productElement (i-1)
    val children = new Array[AnyRef](numchildren)
    for (j <- 0 until numchildren)
       children (j) = makechild (p.productElement (j))
       s (ct) match {
                case Some (ti) =>
                    children (i-1) = makechild (ti)
                case None      =>
                    return None
           }
    val ret = dup (p, children)
    Some (ret)

This gives some limitations on the usage.
I know they are working on replacing this mechanism (Issue 2, Issue 31) but I think it might be interesting in hearing how you guys would do this?

For example will the Positional var(or any other var!) of scala parser combinators not be copied. In fact everything not part of the explict first constructor (and thereby also the default compiler generated unapply method).

I have been experimenting with providing some kind of trait or similar I could add to my terms that the dup/childProduct/etc methods could use, but to no luck yet. (I don’t see how case class copy could be used)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:02:41+00:00Added an answer on May 17, 2026 at 3:02 am

    I have created a small GitHub repository, RSchulz/sbtr, containing my modified Kiama Rewriter.scala module. In it you will find these files:

    • src/main/scala/rrs/sbtr/SBTR.scala — My “Strategy-Based Term Rewriting” module
    • src/main/scala/rrs/sbtr/RewriterFmt.alacs — The original code after formatting for my style conventions
    • src/main/scala/rrs/sbtr/Rewriter-0.8.alacs — The slightly updated 0.8 version
    • src/main/scala/rrs/sbtr/Rewriter.alacs — The original version from which I started

    Diff-ing the first two will disclose my modifications, which should be easily back-ported into the current Kiama Rewriter.scala code.

    The primary difference between the original and the 0.8 versions of the Rewriter.scala source are the addition of queryf, which I did carry over into SBTR. Changes subsequent to 0.8 have not been incorporated.

    The suffix .alacs (scala spelled backwards) is used to keep those files from being compiled. To my knowledge, build tools and IDEs cannot be directed to ignore specific files (at least not easily).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a JS Confirm Below leaving the page function The issue
I want to item's subcatory selected when editing category: <?php function categoryFormEdit() { $ID
I've got a page where I load screen.css which contains an evil rule, which
I'm searching forward in an array of strings with a regex, like this: for
I am modifying a skin for the CKEdit component so that the toolbar is
Im writing a compiler for university project, and I would like to transform my
I am using CKEditor with PHP. Using the sample PHP where the $code variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.