aboutsummaryrefslogtreecommitdiff
path: root/rego-proofs/compiler-rewrite/lib/lean.ml
blob: 975b5630038603c4b574ee10ff4e11fae55eb6b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
open Core

type lean_expression =
  | Identifier of string
  | Bool of bool
  | String of string
  | Number of float
  | List of lean_expression list
  | Tuple of lean_expression list
  | Application of lean_expression * lean_expression list
  | Define of string * lean_expression
  | Inductive of string * judgement list * (string * lean_expression) list
  | ForAll of judgement * lean_expression
  | Exists of judgement * lean_expression

and judgement = string list * string

let fmt_judgement : judgement -> string = function
  | vars, t -> Printf.sprintf "(%s : %s)" (String.concat ~sep:" " vars) t

let rec fmt_lean_expression : lean_expression -> string = function
  | Identifier s -> s
  | Bool b -> if b then "True" else "False"
  | String s -> Printf.sprintf "\"%s\"" s
  | Number n -> Float.to_string n
  | List xs ->
      Printf.sprintf "[%s]"
        (String.concat ~sep:", " (List.map ~f:fmt_lean_expression xs))
  | Tuple xs ->
      Printf.sprintf "(%s)"
        (String.concat ~sep:", " (List.map ~f:fmt_lean_expression xs))
  | Application (head, args) ->
      Printf.sprintf "(%s %s)" (fmt_lean_expression head)
        (String.concat ~sep:" " (List.map ~f:fmt_lean_expression args))
  | Define (ident, expr) ->
      Printf.sprintf "def %s := (%s)" ident (fmt_lean_expression expr)
  | ForAll (j, expr) ->
      Printf.sprintf "∀ %s, %s" (fmt_judgement j) (fmt_lean_expression expr)
  | Exists (j, expr) ->
      Printf.sprintf "∃ %s, %s" (fmt_judgement j) (fmt_lean_expression expr)
  | Inductive (name, judgements, arms) ->
      let fmt_arm (name, expr) =
        let head =
          Printf.sprintf "%s %s" name
            (List.map ~f:fst judgements |> List.concat |> String.concat ~sep:" ")
        in
        Printf.sprintf "  | %s -> %s : %s" name (fmt_lean_expression expr) head
      in
      Printf.sprintf "inductive %s %s where\n%s" name
        (List.map ~f:fmt_judgement judgements |> String.concat ~sep:" ")
        (List.map ~f:fmt_arm arms |> String.concat ~sep:"\n")

© 2015 - 2026 Jakob L. Kreuze