logo
down
shadow

Generalizing traversal of expressions with changes on specific nodes


Generalizing traversal of expressions with changes on specific nodes

By : JohnC
Date : November 17 2020, 04:28 AM
wish of those help Your transform function is a very good start at handling general transformations of ASTs. I'm going to show you something closely related that's a little bit more general.
The uniplate library defines the following class for describing simple abstract syntax trees. An instance of the class needs only provide a definition for uniplate which should perform a step of transformation, possibly with side-effects, to the immediate decedents of the node.
code :
import Control.Applicative
import Control.Monad.Identity

class Uniplate a where
    uniplate :: Applicative m => a -> (a -> m a) -> m a

    descend :: (a -> a) -> a -> a
    descend f x = runIdentity $ descendM (pure . f) x

    descendM :: Applicative m => (a -> m a) -> a -> m a
    descendM = flip uniplate
transform :: Uniplate a => (a -> a) -> a -> a
transform f = f . descend (transform f)
data RingExpr a
    = AddId
    | MulId
    | Lit a
    | AddInv (RingExpr a)
    | Add (RingExpr a) (RingExpr a)
    | Mul (RingExpr a) (RingExpr a)
  deriving Show

class Ring a where
    addId  :: a
    mulId  :: a
    addInv :: a -> a
    add    :: a -> a -> a
    mul    :: a -> a -> a
instance Uniplate (RingExpr a) where
    uniplate e p = case e of
        AddInv x   -> AddInv <$> p x
        Add    x y -> Add    <$> p x <*> p y
        Mul    x y -> Mul    <$> p x <*> p y
        _          -> pure e
replaceIds :: (Eq a, Ring a) => RingExpr a -> RingExpr a
replaceIds (Lit n) | n == addId = AddId
replaceIds (Lit n) | n == mulId = MulId
replaceIds e                    = e

simplifyMul :: RingExpr a -> RingExpr a
simplifyMul (Mul x     MulId) = x
simplifyMul (Mul MulId x    ) = x
simplifyMul e                 = e

squashMulId :: (Eq a, Ring a) => RingExpr a -> RingExpr a
squashMulId = transform (simplifyMul . replaceIds)
> squashMulId . Mul (Lit True) . Add (Lit False) $ Lit True
Add AddId MulId


Share : facebook icon twitter icon
Generalizing objects with Parent Class Vs Generalizing classes implementing a common interface

Generalizing objects with Parent Class Vs Generalizing classes implementing a common interface


By : Tomás Gaete
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , Well you example could have been a good one as well. All your objects could have implemented IDisplayable with it's method Display.
And DisplayContainer could have been a collection of IDisplayable.
Generalizing actions for a binary tree traversal?

Generalizing actions for a binary tree traversal?


By : Robert Dustin
Date : March 29 2020, 07:55 AM
seems to work fine One way to do it would be:
action could be an instance of a class that receives a Node and applies the Action to it:
code :
public interface Action{
  public void apply(Node node){
  // To be done in the classes that will implement 
  // this interface. If it's a graphic-display of the nodes, the apply 
  // method will call draw(node.value), for example
  }
}
/* do something with "actions" here */
action.apply(node);
public void inOrderTraversalFrom(BinaryTreeNode node, Action action)
inOrderTraversalFrom(node.left, action);
ArangoDB GRAPH TRAVERSAL through specific nodes

ArangoDB GRAPH TRAVERSAL through specific nodes


By : user3521948
Date : March 29 2020, 07:55 AM
wish of those help the feature is actually there but was somehow not documented. I added it to our documentation which should be updated soon. Sorry for the inconvenience.
filterVertices takes a list of vertex examples.
generalizing expressions under binders

generalizing expressions under binders


By : user7381832
Date : March 29 2020, 07:55 AM
Does that help There are two keys to this answer: the change tactic, which replaces one term with a convertible one, and generalizing c so that you deal not with g _ _ c but fun z => g _ _ z; this second key allows you to deal with g rather than with g applied to its arguments. Here, I use the pose tactic to control what function applications get β reduced:
code :
Axiom A : Type.
Axiom f : A -> A.
Axiom g : A -> A -> A -> A.
Goal forall c, (fun a b => g a b c) = (fun a b => f (g a b c)).
Proof.
  intro c.
  pose (fun z x y => g x y z) as g'.
  change g with (fun x y z => g' z x y).
  (*   (fun a b : A => (fun x y z : A => g' z x y) a b c) =
       (fun a b : A => f ((fun x y z : A => g' z x y) a b c)) *)
  cbv beta.
  (* (fun a b : A => g' c a b) = (fun a b : A => f (g' c a b)) *)
  generalize (g' c); intro somefun; clear g'.
  (* (fun a b : A => somefun a b) = (fun a b : A => f (somefun a b)) *)
Axiom A : Type.
Axiom f : A -> A.
Axiom g : A -> A -> A -> A.
Goal forall c, (fun a b => g a b c) = (fun a b => f (g a b c)).
Proof.
  intro c.
  change g with (fun a' b' c' => (fun z => id (fun x y => g x y z)) c' a' b').
  (*   (fun a b : A =>
        (fun a' b' c' : A => (fun z : A => id (fun x y : A => g x y z)) c' a' b') a b c) =
       (fun a b : A =>
        f
          ((fun a' b' c' : A => (fun z : A => id (fun x y : A => g x y z)) c' a' b') a
             b c)) *)
  cbv beta.
  (* (fun a b : A => id (fun x y : A => g x y c) a b) =
     (fun a b : A => f (id (fun x y : A => g x y c) a b)) *)
  generalize (id (fun x y : A => g x y c)); intro somefun.
  (* (fun a b : A => somefun a b) = (fun a b : A => f (somefun a b)) *)
generalizing the pumping lemma for UNIX-style regular expressions

generalizing the pumping lemma for UNIX-style regular expressions


By : amit kumar
Date : March 29 2020, 07:55 AM
seems to work fine You're probably looking for
Benjamin Carle and Paliath Narendran "On Extended Regular Expressions" LNCS 5457 DOI:10.1007/978-3-642-00982-2_24 PDF Extended Abstract at http://hal.archives-ouvertes.fr/docs/00/17/60/43/PDF/notes_on_extended_regexp.pdf C. Campeanu, K. Salomaa, S. Yu: A formal study of practical regular expressions, International Journal of Foundations of Computer Science, Vol. 14 (2003) 1007 - 1018. DOI:10.1142/S012905410300214X
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org