A Simple Java Code Generator for ACL2 Based on a Deep Embedding of - - PowerPoint PPT Presentation

a simple java code generator for acl2 based on a deep
SMART_READER_LITE
LIVE PREVIEW

A Simple Java Code Generator for ACL2 Based on a Deep Embedding of - - PowerPoint PPT Presentation

A Simple Java Code Generator for ACL2 Based on a Deep Embedding of ACL2 in Java Alessandro Coglio Kestrel Institute Workshop 2018 ATJ Java code generator for ACL2 (ACL2 To Java) based on AIJ deep embedding of ACL2 in Java (ACL2 In Java)


slide-1
SLIDE 1

A Simple Java Code Generator for ACL2 Based on a Deep Embedding of ACL2 in Java

Alessandro Coglio

Kestrel Institute

Workshop 2018

slide-2
SLIDE 2

AIJ (ACL2 In Java) ATJ (ACL2 To Java) Java code generator for ACL2 deep embedding of ACL2 in Java

based on

slide-3
SLIDE 3

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards.

slide-4
SLIDE 4

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values.
slide-5
SLIDE 5

AIJ includes a Java representation of all the constructible ACL2 values.

all the constructible ACL2 values characters complex rationals ratios integers strings symbols cons pairs rationals numbers

slide-6
SLIDE 6

complex rationals

AIJ includes a Java representation of all the constructible ACL2 values.

characters strings symbols cons pairs ratios integers rationals numbers values

⊂ ⊂ ⊂ ⊂ ⊂ ⊂ ⊂ ⊂ ⊂

This hierarchy of sets...

slide-7
SLIDE 7

AIJ includes a Java representation of all the constructible ACL2 values. This hierarchy of sets... ... is represented as a hierarchy of Java classes.

Symbol Character Cons String Number Rational ComplexRational Integer Ratio Value

slide-8
SLIDE 8

public final class Integer extends Rational { // internal representation: private final java.math.BigInteger bigInt; // factory methods (to build values): public static Integer make(int ...) {...} public static Integer make(long ...) {...} public static Integer make(java.math.BigInteger ...) {...} // getter methods (to unbuild values): public int getJavaInt() {...} public long getJavaLong() {...} public java.math.BigInteger getJavaBigInteger() {...} ... }

AIJ includes a Java representation of all the constructible ACL2 values.

slide-9
SLIDE 9

AIJ includes a Java representation of all the constructible ACL2 values.

Symbol Character Cons String Number Rational ComplexRational Integer Ratio Value

These classes have:

  • Private internal representations.
  • Public factory methods for building.
  • Public getter method for unbuilding.
slide-10
SLIDE 10

AIJ includes a Java representation of all the constructible ACL2 values.

public final class Ratio extends Rational { // internal representation: private final Integer numerator; private final Integer denominator; ... }

slide-11
SLIDE 11

car cdr packageName name numerator denominator

AIJ includes a Java representation of all the constructible ACL2 values.

Cons Number Rational Integer Value Ratio realPart imaginaryPart ComplexRational Symbol String Character PackageName

slide-12
SLIDE 12

AIJ includes:

  • A Java representation of the ACL2 values.
  • A Java representation of the ACL2 terms.

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards.

slide-13
SLIDE 13

body arguments * * parameters name NamedFunction LambdaExpression function value name Variable Constant FunctionApplication

AIJ includes a Java representation of the ACL2 (translated) terms.

Term Symbol Value Function

These classes are structured analogously to the classes for values.

slide-14
SLIDE 14

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values.
  • A Java representation of the ACL2 terms.
  • A Java representation of the ACL2 environment.
slide-15
SLIDE 15

AIJ includes a Java representation of (part of) the ACL2 environment.

public final class Environment { // package definitions: private static final java.util.Map<PackageName, Symbol[]> packageDefs; // function definitions: private static final java.util.Map<Symbol, LambdaExpression> functionDefs; // methods to build the environment: public static void addPackageDef(...) {...} public static void addFunctionDef(...) {...} ... }

slide-16
SLIDE 16

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values.
  • A Java representation of the ACL2 terms.
  • A Java representation of the ACL2 environment.
  • A Java implementation of the ACL2 primitives.
slide-17
SLIDE 17

AIJ includes a Java implementation of all the ACL2 primitive functions.

ACL2 !>(strip-cars *primitive-formals-and-guards*) (ACL2-NUMBERP BAD-ATOM<= BINARY-* BINARY-+ UNARY-- UNARY-/ < CAR CDR CHAR-CODE CHARACTERP CODE-CHAR COMPLEX COMPLEX-RATIONALP COERCE CONS CONSP DENOMINATOR EQUAL IF IMAGPART INTEGERP INTERN-IN-PACKAGE-OF-SYMBOL NUMERATOR PKG-IMPORTS PKG-WITNESS RATIONALP REALPART STRINGP SYMBOL-NAME SYMBOL-PACKAGE-NAME SYMBOLP) ACL2 !> primitive ⇒ built-in ⇍ these functions have no ACL2 definitions

slide-18
SLIDE 18

class Primitive { private static Value execBinaryPlus(Value x, Value y) {...} private static Value execUnaryMinus(Value x) {...} private static Value execCar(Value x) {...} private static Value execComplex(Value x, Value y) {...} private static Value execConsp(Value x) {...} private static Value execEqual(Value x) {...} private static Value execPkgImports(Value x) {...} ... }

AIJ includes a Java implementation of all the ACL2 primitive functions.

no execIf method (if is treated specially)

slide-19
SLIDE 19

class Primitive { private static Value execBinaryPlus(Value x, Value y) {...} private static Value execUnaryMinus(Value x) {...} private static Value execCar(Value x) {...} private static Value execComplex(Value x, Value y) {...} private static Value execConsp(Value x) {...} private static Value execEqual(Value x) {...} private static Value execPkgImports(Value x) {...} ... static Value call(Symbol function, Value[] values) { if (function.equals(Symbol.BINARY_PLUS)) { // call execBinaryPlus } else if (function.equals(Symbol.UNARY_MINUS)) { // call execUnaryMinus } ... } }

AIJ includes a Java implementation of all the ACL2 primitive functions.

slide-20
SLIDE 20

class Primitive { private static Value execUnaryMinus(Value x) { assert x != null; return x.negate(); } ... }

AIJ includes a Java implementation of all the ACL2 primitive functions.

slide-21
SLIDE 21

public abstract class Value { Number negate() { return Integer.ZERO; } ... }

AIJ includes a Java implementation of all the ACL2 primitive functions.

default implementation

slide-22
SLIDE 22

Symbol Character Cons String Integer Ratio Rational ComplexRational Value Number Value.negate inherited implementation Integer.negate Ratio.negate ComplexRational.negate

AIJ includes a Java implementation of all the ACL2 primitive functions.

Value.negate default implementation

  • verridding

implementations

slide-23
SLIDE 23

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values.
  • A Java representation of the ACL2 terms.
  • A Java representation of the ACL2 environment.
  • A Java implementation of the ACL2 primitives.
  • A Java implementation of the ACL2 evaluator.
slide-24
SLIDE 24

AIJ includes a recursive Java implementation of the ACL2 evaluator “in the logic”, i.e. with (set-guard-checking :none).

Term Variable Constant FunctionApplication Variable.eval Constant.eval FunctionApplication.eval function applied evaluate non-strictly i f

  • t

h e r call Function.apply

  • n the argument values

abstract Value eval(java.util.Map<Symbol, Value> bindings);

slide-25
SLIDE 25

NamedFunction LambdaExpression Function abstract Value apply(Value[] values); NamedFunction.apply LambdaExpression.apply named function call Term.eval on the body

  • btained from Environment

d e f i n e d p r i m i t i v e call Primitive.call with the function name

AIJ includes a recursive Java implementation of the ACL2 evaluator “in the logic”, i.e. with (set-guard-checking :none).

slide-26
SLIDE 26

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values, terms, and environment.
  • A Java implementation of the ACL2 primitives and evaluator.
slide-27
SLIDE 27

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values, terms, and environment.
  • A Java implementation of the ACL2 primitives and evaluator.

AIJ provides a Java API to:

  • Build the ACL2 environment.
slide-28
SLIDE 28

AIJ provides a Java API to build the ACL2 environment: package and function definitions, and their constituents. AIJ

call AIJ's public methods code external to AIJ A P I // build the package definitions: ... PackageName.make(...) ... Symbol.make(...) Environment.addPackageDef(...); Environment.addPackageDef(...); ... // build the function definitions: ... Variable.make(...) ... NamedFunction.make(...) ... FunctionApplication.make(...) Environment.addFunctionDef(...); Environment.addFunctionDef(...); ...

slide-29
SLIDE 29

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values, terms, and environment.
  • A Java implementation of the ACL2 primitives and evaluator.

AIJ provides a Java API to:

  • Build the ACL2 environment.
  • Evaluate ACL2 function calls:
  • Build the argument values.
  • Call the function on the arguments, obtaining a result.
  • Unbuild the result value.
slide-30
SLIDE 30

AIJ provides a Java API to evaluate ACL2 function calls. AIJ

A P I // build the argument values: Value arg = Integer.make(100); Value[] args = new Value[]{arg}; // call the function // on the arguments, // obtaining a result: Symbol fun = Symbol.make("ACL2", "FACTORIAL"); Value result = Environment.call(fun, args); // unbuild the result value: java.math.BigInteger bigInt = result.getJavaBigInteger(); code external to AIJ call AIJ's public methods

slide-31
SLIDE 31

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values, terms, and environment.
  • A Java implementation of the ACL2 primitives and evaluator.

AIJ provides a Java API to:

  • Build the ACL2 environment.
  • Evaluate ACL2 function calls.

This API enables external Java code to “run” ACL2 code in Java.

slide-32
SLIDE 32

AIJ is a deep embedding in Java of an executable, side-effect-free, non-stobj-accessing subset of the ACL2 language without guards. AIJ includes:

  • A Java representation of the ACL2 values, terms, and environment.
  • A Java implementation of the ACL2 primitives and evaluator.

AIJ provides a Java API to:

  • Build the ACL2 environment.
  • Evaluate ACL2 function calls.

This API enables external Java code to “run” ACL2 code in Java. By construction, the ACL2 code run via AIJ is:

  • Executable – only primitive and defined functions.
  • Side-effect-free – no special Java code to carry out side effects.
  • Non-stobj-accessing – only values are passed to function calls.
  • Without guards – evaluation “in the logic”.
slide-33
SLIDE 33

build the ACL2 environment evaluate ACL2 function calls AIJ

A P I

Java code external to AIJ:

  • Builds the ACL2 environment.
  • Evaluates ACL2 function calls.
slide-34
SLIDE 34

generate

ATJ generates the Java code to build an ACL2 environment. AIJ build the ACL2 environment evaluate ACL2 function calls

A P I

ATJ

A P I hand-written code; it directly calls AIJ to build/unbuild values

slide-35
SLIDE 35

ATJ is written in ACL2. It provides a macro atj to generate Java files. (atj fn1 ... fnp options)

  • ne or more function names

customize Java package name,

  • utput dir., etc.

fnp+1 fnp+2 ... fnp+3 ... cons binary-+ equal ... ... ATJ calculates the call graph of {fn1, ..., fnp} ATJ checks that each function in the graph:

  • Is primitive or defined.
  • Has no raw Lisp code,

unless whitelisted. ATJ generates Java code to build these functions and supporting packages.

slide-36
SLIDE 36

public class ... { ... // API method to build the ACL2 environment: public static void initialize() {...} // API method to evaluate ACL2 function calls: public static Value call(Symbol function, Value[] arguments) {...} }

ATJ generates a single Java class in a Java file.

builds the non-primitive functions in the call graph of {fn1, ..., fnp} and the supporting packages thin wrapper of Environment.call

slide-37
SLIDE 37

How does evaluation time in AIJ compare to evaluation time in ACL2?

ACL2 with guard checking t ACL2 with guard checking :none AIJ factorial 1 0.8–1x 0.4–3.2x Fibonacci 1 8–10x 17–30x ABNF parser 1 16–87x 19–22x tests baseline slowdown compared to previous column slowdown compared to previous column

The current speed may be adequate to some applications. There are many opportunities for further optimization.

slide-38
SLIDE 38

Future work includes:

  • Optimization of AIJ.
  • Evaluation with ACL2’s other guard checking settings.
  • Side effects – native Java.
  • Stobjs (user-defined).
  • State – side effects.
  • Generated code that calls hand-written code.
  • Shallowly embedded representations of ACL2 functions in Java.
  • Formal verification of the Java code.
  • Generation of code in other programming languages.