Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to integrate lean-gym with python? #26

Open
venom12138 opened this issue Sep 8, 2022 · 1 comment
Open

How to integrate lean-gym with python? #26

venom12138 opened this issue Sep 8, 2022 · 1 comment

Comments

@venom12138
Copy link

Great job! I want to use lean-gym as an engine to train my AI just like RL gym, but I don't know how to integrate lean-gym with python. Could you please provide an example code? Thanks!

@Purewhite2019
Copy link

The following is the code I`m using. Hope it can help you.

import subprocess
import json
from typing import Dict, List, Any
import os


class LeanGymEnvironment:
    def __init__(self, verbose: bool=False, lean_gym_base: str=None) -> None:
        self.p = subprocess.Popen(['lean', '--run', 'src/repl.lean'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=lean_gym_base)
        out = self.p.stdout.read1().decode()
        if verbose:
            print(out)
        
    def write(self, line: str) -> None:
        if line[-1] != '\n':
            line += '\n'
        self.p.stdin.write(bytes(line, 'utf-8'))
        self.p.stdin.flush()
    
    def read(self) -> Dict[str, Any]:
        out = self.p.stdout.read1().decode()
        return json.loads(out)
    
    def init_search(self, decl_name: str, namespaces: List[str]=[]):
        if len(namespaces) == 0:
            self.write(f'["init_search", ["{decl_name}", ""]]')
        else:
            cmd = f'["init_search", ["{decl_name}", "'
            for n in namespaces[:-1]:
                cmd += n + '", "'
            cmd += namespaces[-1] + '"]]'
            self.write(cmd)
            
        return self.read()
    
    def run_tac(self, search_id, tactic_state_id, tactic):
        self.write(f'["run_tac",["{search_id}","{tactic_state_id}","{tactic}"]]')
        return self.read()
    
    def clear_search(self, search_id):
        self.write(f'["self", "{search_id}"]')
        return self.read()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants