Search in sources :

Example 1 with FunctionDefinition

use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.

the class FunctionDefinitionVisitor method visitFunc.

@Override
public FunctionDefinition visitFunc(SplayParser.FuncContext ctx) {
    List<String> args = new ArrayList<>(ctx.args.size());
    for (Token t : ctx.args) {
        String arg = t.getText();
        args.add(arg);
    }
    FunctionSignature functionSignature = null;
    if (ctx.signature() != null) {
        if (!ctx.signature().name.getText().equals(ctx.name.getText())) {
            throw new RuntimeException("mismatching name in type annotation and definition (\"" + ctx.signature().name.getText() + "\" vs. \"" + ctx.name.getText() + "\"");
        }
        functionSignature = functionSignatureVisitor.visitSignature(ctx.signature());
    }
    return new FunctionDefinition(getModuleName(), ctx.name.getText(), args, expressionVisitor.visit(ctx.body), functionSignature);
}
Also used : ArrayList(java.util.ArrayList) Token(org.antlr.v4.runtime.Token) FunctionDefinition(xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition) FunctionSignature(xyz.leutgeb.lorenz.atlas.typing.simple.FunctionSignature)

Example 2 with FunctionDefinition

use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.

the class Java method run.

@Override
public void run() {
    final var loader = Loader.atDefaultHome();
    try {
        loader.autoload();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Program program;
    try {
        program = loader.loadMatching(pattern);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    program.normalize();
    if (!program.infer()) {
        return;
    }
    Multimap<String, FunctionDefinition> output = ArrayListMultimap.create();
    Multimap<String, String> imports = HashMultimap.create();
    for (var entry : program.getFunctionDefinitions().entrySet()) {
        var fd = entry.getValue();
        if (pattern.asMatchPredicate().test(fd.getFullyQualifiedName())) {
            output.put(fd.getModuleName(), fd);
            imports.putAll(fd.getModuleName(), fd.importedFunctions().stream().map(Loader::moduleName).collect(Collectors.toSet()));
        }
    }
    for (var e : output.keySet()) {
        var path = Loader.path(e, this.path.resolve(Path.of("xyz", "leutgeb", "lorenz", "atlas")), ".java");
        var lastModulePart = e.substring(Math.max(0, e.lastIndexOf(".")));
        try {
            java.nio.file.Files.createDirectories(path.getParent());
        } catch (IOException ioException) {
            throw new RuntimeException(ioException);
        }
        final var sink = Files.asCharSink(path.toFile(), StandardCharsets.UTF_8);
        try (var stream = new PrintStream(output(path))) {
            stream.println("// This file was generated automatically.");
            stream.println();
            stream.println("package xyz.leutgeb.lorenz.atlas;");
            stream.println();
            stream.println("import java.util.Objects;");
            stream.println("import xyz.leutgeb.lorenz.atlas.Tree;");
            stream.println("import static xyz.leutgeb.lorenz.atlas.Tree.node;");
            stream.println("import static xyz.leutgeb.lorenz.atlas.Tree.leaf;");
            stream.println();
            for (String imp : imports.get(e)) {
                stream.println("import xyz.leutgeb.lorenz.atlas." + imp + ";");
            }
            stream.println("public final class " + lastModulePart + " {");
            stream.println();
            for (var fd : output.get(e)) {
                fd.printJavaTo(stream, true);
                System.out.println(fd.getFullyQualifiedName());
            }
            stream.println("}");
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        log.info("{}", path);
    }
}
Also used : PrintStream(java.io.PrintStream) Program(xyz.leutgeb.lorenz.atlas.ast.Program) Loader(xyz.leutgeb.lorenz.atlas.module.Loader) FunctionDefinition(xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition) IOException(java.io.IOException)

Example 3 with FunctionDefinition

use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.

the class Run method run.

@Override
public void run() {
    final var start = Instant.now();
    Loader loader = Loader.atDefaultHome();
    /*
    try {
      loader.autoload();
    } catch (IOException e) {
      throw new RuntimeException(e);
    }*/
    Program program;
    try {
        program = loader.load(fqns);
    // program = loader.loadMatching(pattern);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    if (name != null && !name.isBlank()) {
        program.setName(name);
    }
    if (program.isEmpty()) {
        System.out.println("Program to analyze is empty, nothing to do!");
        System.exit(3);
    }
    program.normalize();
    if (!program.infer()) {
        return;
    }
    program.unshare(true);
    program.analyzeSizes();
    // log.info("Loaded definitions:");
    // program.printAllSimpleSignaturesInOrder(System.out);
    System.out.println("Output Directory: " + program.getBasePath().toAbsolutePath());
    System.out.println();
    Map<String, Path> tacticsMap = new HashMap<>();
    // log.info(infer ? "Given for comparison:" : "Will check following types:");
    log.info("Manual tactics disabled. All tactics will be automatically generated.");
    System.out.println("Function Definitions:");
    for (int i = 0; i < program.getOrder().size(); i++) {
        final var stratum = program.getOrder().get(i);
        for (var fqn : stratum) {
            FunctionDefinition fd = program.getFunctionDefinitions().get(fqn);
            if (tactics != null) {
                var path = tactics.resolve(fd.getModuleName().replace(".", "/") + "/" + fd.getName() + ".txt");
                if (Files.exists(path) && Files.isReadable(path)) {
                    tacticsMap.put(fd.getFullyQualifiedName(), path);
                }
            }
            System.out.println(fd.getAnnotatedSignatureString());
            // log.info("\tDependencies: " + fd.getOcurringFunctionsNonRecursive());
            System.out.println("\tSource:       " + fd.getBody().getSource().getRoot());
            System.out.println("\tBound:        " + fd.getAnnotatedSignature().getAnnotation().map(a -> a.getBounds(fd.treeLikeArguments().stream().map(IdentifierExpression::getName).toList())).orElse("?"));
            if (tactics != null) {
                final var p = tacticsMap.get(fd.getFullyQualifiedName());
                if (p == null) {
                    System.out.println("\tTactic:       n/a (will be automatically generated)");
                } else {
                    System.out.println("\tTactic:       " + p.toAbsolutePath());
                }
            }
        }
    }
    System.out.println();
    System.setProperty(Util.getPropertyName(Prover.class, "tickAst"), useTickAst.toString());
    final var result = program.solve(new HashMap<>(), tacticsMap, infer, consistentModules, equalRanks, simpleAnnotations, !infer, Collections.emptySet());
    final var stop = Instant.now();
    System.out.println("Elapsed Walltime: " + Duration.between(start, stop));
    System.out.println();
    if (Status.UNSATISFIABLE.equals(result.getStatus())) {
        System.out.println("UNSAT");
        System.exit(1);
    } else if (Status.UNKNOWN.equals(result.getStatus())) {
        System.out.println("UNKNOWN");
        System.exit(2);
    } else if (Status.SATISFIABLE.equals(result.getStatus())) {
        if (!infer) {
            System.out.println("SAT");
            System.out.println();
        } else {
            System.out.println("Results:");
            program.printAllInferredAnnotationsAndBoundsInOrder(System.out);
            System.out.println();
        }
    } else {
        throw new RuntimeException("encountered unknown status");
    }
    /*
    if (json != null) {
      JsonObjectBuilder builder = Json.createObjectBuilder();

      builder.add("result", program.inferredSignaturesToJson());
      builder.add("duration", Json.createValue(Duration.between(start, stop).toString()));

      JsonObjectBuilder z3ObjectBuilder = Json.createObjectBuilder();
      z3ObjectBuilder.add("status", Json.createValue(result.getStatus().toString()));

      JsonObjectBuilder z3StatisticsBuilder = Json.createObjectBuilder();
      result.getStatistics().forEach(z3StatisticsBuilder::add);
      z3ObjectBuilder.add("statistics", z3StatisticsBuilder.build());

      if (result.getSmtFile().isPresent()) {
        z3ObjectBuilder.add("file", Json.createValue(result.getSmtFile().get().toString()));
      }

      builder.add("z3", z3ObjectBuilder.build());

      log.info("Writing JSON output to {}", json);
      try (final var out = output(json)) {
        Json.createWriter(out).writeObject(builder.build());
      } catch (IOException ioException) {
        log.error("Failed to write JSON output.", ioException);
      }
    }
     */
    // System.exit(result.toExitCode());
    System.exit(0);
}
Also used : Path(java.nio.file.Path) Program(xyz.leutgeb.lorenz.atlas.ast.Program) Files(java.nio.file.Files) FunctionDefinition(xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition) Loader(xyz.leutgeb.lorenz.atlas.module.Loader) Util(xyz.leutgeb.lorenz.atlas.util.Util) Set(java.util.Set) IOException(java.io.IOException) HashMap(java.util.HashMap) Instant(java.time.Instant) IdentifierExpression(xyz.leutgeb.lorenz.atlas.ast.expressions.IdentifierExpression) Slf4j(lombok.extern.slf4j.Slf4j) Prover(xyz.leutgeb.lorenz.atlas.typing.resources.proving.Prover) Duration(java.time.Duration) Map(java.util.Map) Status(com.microsoft.z3.Status) Path(java.nio.file.Path) Collections(java.util.Collections) CommandLine(picocli.CommandLine) Program(xyz.leutgeb.lorenz.atlas.ast.Program) HashMap(java.util.HashMap) Prover(xyz.leutgeb.lorenz.atlas.typing.resources.proving.Prover) Loader(xyz.leutgeb.lorenz.atlas.module.Loader) IOException(java.io.IOException) IdentifierExpression(xyz.leutgeb.lorenz.atlas.ast.expressions.IdentifierExpression) FunctionDefinition(xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition)

Example 4 with FunctionDefinition

use of xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition in project atlas by lorenzleutgeb.

the class Haskell method run.

@Override
public void run() {
    Loader loader = Loader.atDefaultHome();
    try {
        loader.autoload();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Program program;
    try {
        program = loader.loadMatching(pattern);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    program.normalize();
    if (!program.infer()) {
        return;
    }
    Multimap<String, FunctionDefinition> output = ArrayListMultimap.create();
    Multimap<String, String> imports = HashMultimap.create();
    for (var entry : program.getFunctionDefinitions().entrySet()) {
        var fd = entry.getValue();
        if (pattern.asMatchPredicate().test(fd.getFullyQualifiedName())) {
            output.put(fd.getModuleName(), fd);
            imports.putAll(fd.getModuleName(), fd.importedFunctions().stream().map(Loader::moduleName).collect(Collectors.toSet()));
        }
    }
    for (var e : output.keySet()) {
        var path = Loader.path(e, this.path, ".hs");
        var lastModulePart = e.substring(Math.max(0, e.lastIndexOf(".")));
        try (var stream = new PrintStream(new FileOutputStream(path.toFile()))) {
            stream.println("-- This file was generated automatically.");
            stream.println();
            stream.println("module " + lastModulePart + " where");
            stream.println("import InterpreterPrelude (Tree(Node, Leaf))");
            stream.println();
            for (String imp : imports.get(e)) {
                stream.println("import qualified " + imp);
            }
            for (var fd : output.get(e)) {
                fd.printHaskellTo(stream);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        log.info("{}", path);
    }
}
Also used : PrintStream(java.io.PrintStream) Program(xyz.leutgeb.lorenz.atlas.ast.Program) FileOutputStream(java.io.FileOutputStream) Loader(xyz.leutgeb.lorenz.atlas.module.Loader) FunctionDefinition(xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition) IOException(java.io.IOException)

Aggregations

FunctionDefinition (xyz.leutgeb.lorenz.atlas.ast.FunctionDefinition)4 IOException (java.io.IOException)3 Program (xyz.leutgeb.lorenz.atlas.ast.Program)3 Loader (xyz.leutgeb.lorenz.atlas.module.Loader)3 PrintStream (java.io.PrintStream)2 Status (com.microsoft.z3.Status)1 FileOutputStream (java.io.FileOutputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Duration (java.time.Duration)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Token (org.antlr.v4.runtime.Token)1 CommandLine (picocli.CommandLine)1 IdentifierExpression (xyz.leutgeb.lorenz.atlas.ast.expressions.IdentifierExpression)1