package syguslib-utils

  1. Overview
  2. Docs

Source file Sygus.ml

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
(**
   This implementation is based on the SyGuS Language Standard Version 2.0.
   Documentation can be found at https://sygus.org/language/
*)
open Base

let use_v1 = ref false

(* ============================================================================================= *)
(*                               TYPES FOR SYGUS SYNTAX                                          *)
(* ============================================================================================= *)

type symbol = string

type attribute =
  | Attr of symbol
  | AttrVal of symbol * string

type literal =
  | LitNum of int
  | LitDec of float
  | LitBool of bool
  | LitHex of string
  | LitBin of bool list
  | LitString of string

type index =
  | INum of int
  | ISym of symbol

type identifier =
  | IdSimple of symbol
  | IdIndexed of symbol * index list
  | IdQual of symbol * sygus_sort

and sygus_sort =
  | SId of identifier
  | SApp of identifier * sygus_sort list

type sygus_term =
  | SyId of identifier
  | SyLit of literal
  | SyApp of identifier * sygus_term list
  | SyExists of sorted_var list * sygus_term
  | SyForall of sorted_var list * sygus_term
  | SyLet of binding list * sygus_term

and sorted_var = symbol * sygus_sort

and binding = symbol * sygus_term

type feature =
  | FGrammar
  | FFwdDecls
  | FRecursion
  | FOracles
  | FWeights

type command =
  | CCheckSynth
  | CAssume of sygus_term
  | CConstraint of sygus_term
  | CChcConstraint of sorted_var list * sygus_term * sygus_term
  | CDeclareVar of symbol * sygus_sort
  | CDeclareWeight of symbol * attribute list
  | CInvConstraint of symbol * symbol * symbol * symbol
  | CSetFeature of feature * bool
  | CSynthFun of symbol * sorted_var list * sygus_sort * grammar_def option
  | CSynthInv of symbol * sorted_var list * grammar_def option
  | COptimizeSynth of sygus_term list * attribute list
  | CDeclareDataType of symbol * dt_cons_dec list
  | CDeclareDataTypes of sygus_sort_decl list * dt_cons_dec list list
  | CDeclareSort of symbol * int
  | CDefineFun of symbol * sorted_var list * sygus_sort * sygus_term
  | CDefineSort of symbol * sygus_sort
  | CSetInfo of symbol * literal
  | CSetLogic of symbol
  | CSetOption of symbol * literal
  | COracle of oracle_command

and oracle_command =
  | OAssume of sorted_var list * sorted_var list * sygus_term * symbol
  | OConstraint of sorted_var list * sorted_var list * sygus_term * symbol
  | ODeclareFun of symbol * sygus_sort list * sygus_sort * symbol
  | OConstraintIO of symbol * symbol
  | OConstraintCex of symbol * symbol
  | OConstraintMem of symbol * symbol
  | OConstraintPosw of symbol * symbol
  | OConstraintNegw of symbol * symbol
  | OCorrectness of symbol * symbol
  | OCorrectnessCex of symbol * symbol

and sygus_sort_decl = symbol * int

and dt_cons_dec = symbol * sorted_var list

and grammar_def = (sorted_var * grouped_rule_list) list

and grouped_rule_list = sygus_gsterm list

and sygus_gsterm =
  | GConstant of sygus_sort
  | GTerm of sygus_term
  | GVar of sygus_sort

type program = command list

let special_chars : char list =
  [ '_'; '+'; '-'; '*'; '&'; '|'; '!'; '~'; '<'; '>'; '='; '/'; '%'; '?'; '.'; '$'; '^' ]
;;

let reserved_words : string list =
  [ "check-synth"
  ; "Constant"
  ; "constraint"
  ; "declare-datatype"
  ; "declare-datatypes"
  ; "declare-sort"
  ; "declare-var"
  ; "define-fun"
  ; "define-sort"
  ; "exists"
  ; "forall"
  ; "inv-constraint"
  ; "let"
  ; "set-feature"
  ; "set-info"
  ; "set-logic"
  ; "set-option"
  ; "synth-fun"
  ; "synth-inv"
  ; "Variable"
  ]
;;

let digits = [ '0'; '1'; '2'; '3'; '4'; '5'; '6'; '7'; '8'; '9' ]

(**
   `valid_ident name` is `true` whenever `name` is not a reserved word, and does not contain
   any special character.
*)
let valid_ident (name : string) =
  (not (List.mem ~equal:String.equal reserved_words name))
  &&
  match String.to_list name with
  | hd :: _ -> not (List.mem ~equal:Char.equal digits hd)
  | [] -> false
;;

let char_to_bool (c : char) = if Char.(c = '0') then false else true

let has_standard_extension (s : string) =
  try
    let ext = Caml.Filename.extension s in
    String.equal ext ".sl"
  with
  | _ -> false
;;

(* ============================================================================================= *)
(*                                      SOLVER RESPONSES                                         *)
(* ============================================================================================= *)

type solver_response =
  | RSuccess of (symbol * sorted_var list * sygus_sort * sygus_term) list
  | RInfeasible
  | RFail
  | RUnknown

let is_sat = function
  | RSuccess _ -> true
  | _ -> false
;;

let is_failed = function
  | RFail -> true
  | _ -> false
;;

let is_infeasible = function
  | RInfeasible -> true
  | _ -> false
;;
OCaml

Innovation. Community. Security.