Coder Social home page Coder Social logo

rhennigan / codeequivalenceutilities Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 1.0 3.25 MB

Utilities for testing code equivalence

Home Page: https://paclets.com/Wolfram/CodeEquivalenceUtilities

License: MIT License

Mathematica 99.98% Shell 0.02%
wolfram-language metaprogramming canonical-forms code-comparison code-transformations education equality equivalence type-theory intension

codeequivalenceutilities's Introduction

CodeEquivalenceUtilities

CodeEquivalenceUtilities is a collection of Wolfram Language functions that can be used to test if different pieces of code are equivalent without the need for evaluation.

This allows comparison of unevaluated expressions that may have non-deterministic outputs (e.g. random values, dates, etc).

This Paclet represents the underlying technology that powers several automated code grading systems, such as the online exercises for EIWL and Wolfram Challenges.

View notebooks Check Release

Installing CodeEquivalenceUtilities

Using Wolfram Language version 13.0 or later:

PacletInstall["Wolfram/CodeEquivalenceUtilities"]

Using Wolfram Language version 12.0 or later:

ResourceFunction["GitHubInstall"]["rhennigan", "CodeEquivalenceUtilities"]

From Github

The CodeEquivalenceUtilities release comes in the form of a .paclet file, which contains the entire package and its documentation. Download the latest release from the GitHub repo's releases page. To install, run the following command in the Wolfram Language:

PacletInstall["/full/path/to/CodeEquivalenceUtilities.paclet"]

This will permanently install the CodeEquivalenceUtilities paclet. The Wolfram Language will always use the latest installed version of CodeEquivalenceUtilities. Installed versions can be enumerated using the command:

PacletFind["Wolfram/CodeEquivalenceUtilities"]

And all versions can be uninstalled using the command:

PacletUninstall["Wolfram/CodeEquivalenceUtilities"]

Paclet Guide

Equivalence for Wolfram Language code can be defined in many ways. The methods used by CodeEquivalenceUtilities attempt to determine intensional equivalence by transforming expressions into a canonical representation.

Equivalence Testing

CodeEquivalentQ - test if two unevaluated expressions are equivalent

EquivalenceTestData - get additional information about the equivalence test performed by CodeEquivalentQ

Code Transformation

ToCanonicalForm - convert an expression into a canonical representation for direct comparison

MakeCanonicalForm - convert to canonical form without evaluating the input

FromCanonicalForm - convert a canonical representation back into a normal evaluatable expression

Examples

Basic Examples

Check if two expressions are equivalent:

CodeEquivalentQ[RandomInteger /@ Range[5], Array[RandomInteger, 5]]


View the canonical representations of expressions:

MakeCanonicalForm[RandomInteger /@ Range[5]]

MakeCanonicalForm[Array[RandomInteger, 5]]

These are directly comparable:

% === %%

Scope

Get additional information about the equivalence test:

EquivalenceTestData[
    First[Rest[Range /@ Range[2^100]]],
    Part[Table[Table[j, {j, i}], {i, 2^100}], 2]
]

View the sequence of transformations used to convert an expression to its canonical form:

MakeCanonicalForm[Array[RandomInteger, 5], "Trace" -> True] // Column

Convert a canonical representation to a normal expression:

MakeCanonicalForm[Array[RandomInteger, 5]]
FromCanonicalForm[%]

Evaluate:

ReleaseHold[%]

Neat Examples

Here is a list of expressions, some of which are equivalent to others:

expressions = {
    HoldForm[Table[i, {i, 5}, {j, i + 2}]],
    HoldForm[Array[Range, 5, 3]],
    HoldForm[Table[ConstantArray[i, i + 2], {i, 5}]],
    HoldForm[First[Rest[Range /@ Range[10]]]],
    HoldForm[Range /@ Range[3, 7]],
    HoldForm[Part[Table[Table[j, {j, i}], {i, 10}], 2]]
};

Find the sequence of transformations for each expression:

Short[traces = Most@ToCanonicalForm[#, "Trace" -> True] & /@ expressions]

Generate a graph for each sequence:

paths = Graph[DirectedEdge @@@ Partition[#, 2, 1]] & /@ traces

Combine the graphs:

graph = Graph[GraphUnion @@ paths, Sequence[
   VertexLabels -> Placed["Name", Tooltip], 
    GraphLayout -> "LayeredDigraphEmbedding"]];

Equivalent expressions converge to the same connected component:

HighlightGraph[graph, paths]

Group the expressions into their corresponding equivalence class:

grouped = GroupBy[expressions, ToCanonicalForm]
TableForm[KeyValueMap[Reverse@*List, grouped]]

License

This project is licensed under the terms of the MIT license. See the LICENSE file in the root directory of this source tree for details.

codeequivalenceutilities's People

Contributors

rhennigan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rohitn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.